0

我正在编写 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 多处理器架构的模块

4

1 回答 1

0
        cur_area = list_entry(cur_area->head.next, struct area_control, head);

您还需要检查是否 cur_area->head.nextNULL

使用宏定义:

#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为空,此代码将失败。

于 2013-01-14T13:14:21.407 回答