gcc (GCC) 4.6.1 Copyright (C) 2011 Free Software Foundation, Inc.
用 GCC 4.6.1 編譯 linux-3.0 時,屏幕上會滾動大量警告。它們並非錯誤,中斷才會導致失敗;但值得學會分類閱讀。
drivers/ata/sata_sil.c:453:14: 警告:此函數中的‘serror’在使用前可能未初始化 arch/x86/kvm/emulate.c:2054:9: 警告:此函數中的‘ss_sel’在使用前可能未初始化 drivers/media/common/tuners/max2165.c:203:11: 警告:此函數中的‘fraction’在使用前可能未初始化
這類最多,通常是某些分支下變量未賦值就被讀取。多數情況下現實路徑不會觸發,但屬於真實隱患。
drbd_main.c:4187:19: 警告:the comparison will always evaluate as 'true' for the address of '__this_module' will never be NULL
取地址永遠不為 NULL,因此判空多餘,屬於代碼冗餘。
DAC960.c:5095:30: 警告:在‘DAC960_V2_IOCTL_Opcode_T’和‘enum <匿名>’間比較 drivers/scsi/libsas/sas_ata.c:80:3: 警告:case 值‘2’不在枚舉類型‘enum exec_status’內
drivers/isdn/hardware/eicon/message.c:11308:54: 警告:數組下標大於數組邊界 drivers/net/wireless/airo.c:1421:15: 警告:數組下標大於數組邊界
這類值得認真看待,可能確實存在越界訪問。
警告:調用‘copy_from_user_overflow’,聲明有警告屬性: copy_from_user() buffer size is not provably correct
出現於 fs/binfmt_misc.c、drivers/isdn 等多處,表示傳入的目標緩衝區大小無法在編譯期證明是安全的。
drivers/misc/lkdtm.c:276:1: 警告:棧幀有 1028 字節,超過了 1024 字節 drivers/net/cnic.c:4556:1: 警告:棧幀有 1168 字節,超過了 1024 字節
內核線程棧有限,內核代碼中出現 1KB 以上的棧使用是明確的風險信號。
WARNING: drivers/eisa/built-in.o(.data+0xd0): Section mismatch in reference from the variable pci_eisa_driver to the function .init.text:pci_eisa_init() If the reference is valid then annotate the variable with __init* or __refdata (see linux/init.h) or name the variable: *_template, *_timer, *_sht, *_ops, *_probe, *_probe_one, *_console
這類不是編譯器警告而是內核自己的 modpost 檢查:初始化階段才用的函數被標了 __init,卻被一個長期存在的變量引用,內存釋放後再訪問就會崩。修正方式是把變量改名為白名單後綴,或補上 __refdata 註釋。
警告:不建議使用‘pcmcia_request_exclusive_irq’ [-Wdeprecated-declarations] include/linux/mca-legacy.h:12:2: 警告:#warning "MCA legacy - please move your driver to the new sysfs api" drivers/isdn/icn/icn.c:721:4: 警告:#warning TODO test headroom ...
編譯 3.0 這類較老版本時,上述警告幾乎必然出現,不影響生成可用的內核鏡像,可以忽略。真正需要警惕的只有三類:數組越界、棧幀超標、Section mismatch——它們指向真實的內存安全問題。若只是想乾淨地編譯,可在配置中降低 CONFIG_DEBUG_* 相關選項,或用 make W=0 抑制額外檢查,但不建議在自研代碼中這麼做。