【问题标题】:Odd gcc warning behavior奇怪的 gcc 警告行为
【发布时间】:2013-06-28 19:05:45
【问题描述】:

我正在开发一个 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(在公司服务器上,我真的没有升级的选择)。

【问题讨论】:

  • 您可以考虑通过重新编译来升级您的编译器(到 GCC 4.8.1)(例如使用../gcc-4.8.1/configure --prefix=$HOME/pub);你不需要root权限。
  • 我忘了建议--program-suffix=-4.8 为那个../gcc-4.8.1/configure ...

标签: c linux gcc


【解决方案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. 

【讨论】:

  • 啊,原来-Wall 没有启用-Wtype-limits,所以这就是我没有看到消息的原因。谢谢
【解决方案2】:

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

您似乎自己找到了正确的解决方法。

【讨论】:

    猜你喜欢
    • 2012-03-04
    • 1970-01-01
    • 2019-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-01
    • 2011-12-08
    相关资源
    最近更新 更多