c - 奇怪的 gcc 警告行为

标签 c linux gcc

我正在开发一个 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/

相关文章:

c - 组装,画图

c语言数组问题

C:如何将用户输入限制为由空格分隔的 2 个参数?

linux - 如何计算目录中压缩文件的行数并打印每个文件的行数?

linux - EC2 ssh-add 身份不是 "stick"

c - 将库中的函数标记为已弃用

c - 帮助在Windows上编译seek-bzip2

c - 为 C/FFI 库调用分配对象

linux - 如何监视文件以找出将来哪个进程将写入或删除它

linux - 无法运行使用 gcc 构建的可执行文件