Linux内核编程:防御性编程学习

本文探讨了Linux内核编程中的防御性编程,强调了在使用malloc时检查返回值的重要性,并提出使用sizeof(*mod)替代sizeof(struct module)以实现代码自适应。此外,还鼓励根据项目需求进行特定的优化。
摘要由CSDN通过智能技术生成
/*
 *Kernel  : Linux2.6.32.63 
 *File    : \scripts\mod\modpost.h 
            \scripts\mod\modpost.c  
 *Author  : DavidLin       
 *Date    : 2014-12-25pm       
 *Email   : linpeng1577@163.com or linpeng1577@gmail.com       
 *world   : the city of SZ, in China       
 *Ver     : 000.000.001       
 *history :     editor      time            do       
 *          1)LinPeng       2014-12-25      created this file!       
 *          2)       
 */    
  
/* Linux kernel code : modpost.h & modpost.c , author is someone, not me */
/* modpost.h */

#define NOFAIL(ptr)    do_nofail((ptr), #ptr) 
void* do_nofail (void* ptr, const char* expr);

/* end of modpost.h */


/* modpost.c */

void* do_nofail(void* ptr, const char* expr)
{
    if(!ptr)
        fatal("modpost: Memory allocation failure:%s.\n", expr);
    return ptr;
}

static struct module* new_module(char* modname)
{
    struct module*
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值