C語言中史上最愚蠢的Bug

2014-08-06    分類:C/C++開發、程式設計開發、首頁精華2人評論發表於2014-08-06

本文來自“The most stupid C bug ever”,很有意思,分享給大家。我相信這樣的bug,就算你是高手你也會犯的。你來看看作者犯的這個Bug吧。

首先,作者想用一段程式來建立一個檔案,如果有檔名的話,就建立真正的檔案,如果沒有的話,就呼叫?tmpfile()?建立臨時檔案。他這段程式就是HTTP下載的C程式。code==200就是HTTP的返回碼。

else if (code == 200) {     // Downloading whole file
    /* Write new file (plus allow reading once we finish) */
    g = fname ? fopen(fname, "w+") : tmpfile();
}

但是這個程式,只能在Unix/Linux下工作,因為 Microsoft 的?tmpfile()的實現?居然選擇了 C:\ 作為臨時檔案的存放目錄,這對於那些沒有管理員許可權的人來說就出大問題了,在Windows 7下,就算你有管理員許可權也會有問題。所以,上面的程式在Windows平臺下需要用不同的方式來處理,不能直接使用Windows的tmpfile()函式。

於是作者就先把這個問題記下來,在註釋中寫下了FIXME:

else if (code == 200) {     // Downloading whole file
    /* Write new file (plus allow reading once we finish) */

    // FIXME Win32 native version fails here because
    //   Microsoft's version of tmpfile() creates the file in C:\
    g = fname ? fopen(fname, "w+") : tmpfile();
}

然後,作者覺得需要寫一個跨平臺的編譯:

FILE * tmpfile ( void ) {
#ifndef _WIN32
    return tmpfile();
#else
    //code for Windows;
#endif
}

然後,作者覺得這樣實現很不好,會發現名字衝突,因為這樣一來這個函式太難看了。於是他重構了一下他的程式碼——寫一個自己實現的tmpfile() – w32_tmpfile,然後,在Windows 下用巨集定義來重新命名這個函式為tmpfile()。(這種用法是比較標準的跨平臺程式碼的寫法)

#ifdef _WIN32
  #define tmpfile w32_tmpfile
#endif

FILE * w32_tmpfile ( void ) {
    //code for Windows;
}

搞定!編譯程式,執行。靠!居然沒有呼叫到我的w32_tmpfile(),什麼問題?除錯,單步跟蹤,果然沒有呼叫到!難道是問號表示式有問題?改成if – else 語句,好了!

if(NULL != fname) {
    g = fopen(fname, "w+");
} else {
    g = tmpfile();
}

問號表示式不應該有問題吧,難道我們的巨集對問號表示式不起作用,這難道是編譯器的預編譯的一個bug?作者懷疑到。

現在我們把所有的程式碼連在一起看,並比較一下:

能正常工作的程式碼

#ifdef _WIN32
#  define tmpfile w32_tmpfile
#endif

FILE * w32_tmpfile ( void ) {
    code for Windows;
}

else if (code == 200) {     // Downloading whole file
    /* Write new file (plus allow reading once we finish) */
    // FIXME Win32 native version fails here because
    //     Microsoft's version of tmpfile() creates the file in C:\
    //g = fname ? fopen(fname, "w+") : tmpfile();
    if(NULL != fname) {
        g = fopen(fname, "w+");
    } else {
        g = tmpfile();
    }
}

不能正常工作的程式碼

#ifdef _WIN32
#  define tmpfile w32_tmpfile
#endif

FILE * w32_tmpfile ( void ) {
    code for Windows;
}

else if (code == 200) {     // Downloading whole file
    /* Write new file (plus allow reading once we finish) */
    // FIXME Win32 native version fails here because
    //    Microsoft's version of tmpfile() creates the file in C:\
    g = fname ? fopen(fname, "w+") : tmpfile();
}

也許你在一開始就看到了這個bug,但是作者沒有。所有的問題都出在註釋上:

/* Write new file (plus allow reading once we finish) */
// FIXME Win32 native version fails here because
//     Microsoft's version of tmpfile() creates the file in C:\

你看到了最後那個C:\嗎?在C中,“\” 代表此行沒有結束,於是,後面的程式碼也成了註釋。這就是這個bug的真正原因

而之所以改成if-else能工作的原因是因為作者註釋了老的問號表示式的程式碼,所以,那段能工作的程式碼成了:

/* Write new file (plus allow reading once we finish) */
// FIXME Win32 native version fails here because Microsoft's version of tmpfile() creates the file in C:    //g = fname ? fopen(fname, "w+") : tmpfile();
if(NULL != fname) {
    g = fopen(fname, "w+");
} else {
    g = tmpfile();
}

我相信,當作者找到這個問題的原因後,一定會罵一句“媽的”!我也相信,這個bug花費了作者很多時間!

最後,我也share一個我以前犯的一個錯。

我有一個小函式,需要傳入一個int* pInt的型別,然後我需要在我的程式碼裡 把這個int* pInt作除數。於是我的程式碼成了下面的這個樣子:

float result = num/*pInt;
….

/* some comments */

-x<10 ? f(result):f(-result);

因為我在我當時用vi編寫程式碼,所以沒有語法高亮,而我的程式都編譯通過了,但是卻出現了很奇怪的事。我也不知道,用gdb調式的時候,發現有些語句直接就過了。這個問題讓我花了很多時間,最後發現問題原來是沒有空格導致的,TNND,下面我用程式碼高亮的外掛來顯示上面的程式碼,

float result = num/*pInt;
....

/*  some comments */

-x<10 ? f(result):f(-result);

Holly Shit! 我的程式碼成了:

float result = num-x<10 ? f(result):f(-result);

媽的!我的這個錯誤在愚蠢程度上和上面那個作者出的錯誤有一拼。

來自:酷殼

相關文章