2013-06-28 81 views
5

我工作的Linux驱动程序,而我得到这个警告消息:奇GCC警告行为

/home/andrewm/pivot3_scsif/pivot3_scsif.c:1090: warning: ignoring return value of ‘copy_from_user’, declared with attribute warn_unused_result 

的违规行为:

if (copy_from_user(tmp, buf, count) < 0) 

检查copy_from_user的声明之后,我发现它返回一个unsigned long,所以显然比较总是失败,所以返回值不会影响比较。这部分是有道理的,但为什么海湾合作委员会也不警告这是一个签名/无符号比较的事实?这仅仅是一个编译器的特点吗?或者它是否为相同的表情避免两次警告?

包含该行的作用是:

int proc_write(struct file *f, const char __user *buf, unsigned long count, void *data) 
{ 
    char tmp[64]; 
    long value; 
    struct proc_entry *entry; 

    if (count >= 64) 
    count = 64; 
    if (copy_from_user(tmp, buf, count) < 0) 
    { 
    printk(KERN_WARNING "pivot3_scsif: failed to read from user buffer %p\n", buf); 
    return (int)count; 
    } 
    tmp[count - 1] = '\0'; 
    if (tmp[count - 2] == '\n') 
    tmp[count - 2] = '\0'; 
    ... 
} 

对64位使用gcc 4.4.1的Red Hat(一家公司的服务器上,我真的没有在升级选择)。

+0

你可以考虑重新编译(例如用'../gcc-4.8.1/configure --prefix = $ HOME/pub')编译器升级(到GCC 4.8.1);你不需要root权限。 –

+0

我忘了建议也​​'--program后缀= -4.8'为'../ GCC-4.8.1/configure' ... –

回答

4

看来这是一个编译器选项http://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html

-Wno-unused-result 
    Do not warn if a caller of a function marked with attribute warn_unused_result (see Function Attributes) does not use its return value. The default is -Wunused-result. 

.... 

-Wtype-limits 
    Warn if a comparison is always true or always false due to the limited range of the data type, but do not warn for constant expressions. For example, warn if an unsigned variable is compared against zero with ‘<’ or ‘>=’. This warning is also enabled by -Wextra. 
+0

啊,原来'-Wall'不启用'-Wtype-限制“,所以这就是为什么我没有看到消息。谢谢 –

3

是的,它可能只是一个编译器的怪癖。该警告大概是在语法优化的几个步骤之后生成的,其中if()表达式被消除(因为条件总是为真),留下了裸函数调用。找到这种行为是相当常见的。同样有一些警告条件仅在编译优化启用时才会发出。

你似乎已经想通了适当的修正自己。