2013-06-28 83 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()表達式被消除(因爲條件總是爲真),留下了裸函數調用。找到這種行爲是相當常見的。同樣有一些警告條件僅在編譯優化啓用時纔會發出。

你似乎已經想通了適當的修正自己。