我正在开发一个 linux 驱动程序,我收到了这条警告消息:
/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
,显然比较总是会失败,所以返回值不会影响比较.那部分是有道理的,但是为什么 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';
...
}
在 64 位 Red Hat 上使用 gcc 4.4.1(在公司服务器上,我真的没有升级的选择)。
最佳答案
这似乎是一个编译器选项 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.
关于c - 奇怪的 gcc 警告行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17371893/