我正在编写Linux内核模块并遇到一些问题: 检查这些指针值
后,我的模块就会出现问题static struct area_control {
struct list_head head;
unsigned long addr;
unsigned long jiffies;
struct area_part *part;
} *const_areas = NULL, *var_areas = NULL;
static struct area_control *Find_Area(unsigned long addr, struct area_control *first_area)
{
if (first_area)
{
struct area_control *cur_area = first_area;
while ( 1 )
{
if (!cur_area) return NULL;
if (cur_area->addr == addr)
{
cur_area->jiffies = jiffies;
return cur_area;
}
cur_area = list_entry(cur_area->head.next, struct area_control, head);
if (cur_area == first_area) return NULL;
}
}
return NULL;
}
点
cur_area->head.next
模块因为cur_area为NULL而下降! - 我从调用跟踪和反汇编程序得到的这个事实。适用于Linux 2.6.34 x86_64多处理器体系结构的模块
答案 0 :(得分:0)
cur_area = list_entry(cur_area->head.next, struct area_control, head);
您还需要检查cur_area->head.next
是否也是NULL
。
使用宏定义:
#define list_entry(ptr, type, member) \
((type *)((char *)(ptr) – (unsigned long)(&((type *)0)->member)))
代码将扩展为:
cur_area = ((struct area_control *)((char *)(cur_area->head.next) - (unsigned long(&((struct area_control *)0)->head)))
如您所见,如果cur_area->head.next
为空,此代码将失败。