我正在研究一个linux驱动程序,我收到了这条警告消息:
/home/andrewm/pivot3_scsif/pivot3_scsif.c:1090: warning: ignoring return value of ‘copy_from_user’, declared with attribute warn_unused_result
Run Code Online (Sandbox Code Playgroud)
违规行是:
if (copy_from_user(tmp, buf, count) < 0)
Run Code Online (Sandbox Code Playgroud)
在检查声明之后copy_from_user,我发现它返回一个unsigned long,所以显然比较总是会失败,所以返回值不会影响比较.那部分是有道理的,但为什么gcc也没有警告它是签名/未签名的比较?这仅仅是编译器的特点吗?或者它是否避免同一表达式两次警告?
包含该行的函数是:
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';
...
}
Run Code Online (Sandbox Code Playgroud)
在64位Red Hat上使用gcc 4.4.1(在公司服务器上,我真的没有升级选择).
看来这是一个编译器选项http://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html:
\n\n-Wno-unused-result\n 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. \n\n....\n\n-Wtype-limits\n 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 \xe2\x80\x98<\xe2\x80\x99 or \xe2\x80\x98>=\xe2\x80\x99. This warning is also enabled by -Wextra. \nRun Code Online (Sandbox Code Playgroud)\n