C語言中史上最愚蠢的Bug 2011年8月26日 陳皓 發表評論 閱讀評論 38,650 人閱讀
本文來自“The most stupid C bug ever”,很有意思,分享給大家。我相信這樣的bug,就算你是高手你也會犯的。你來看看作者犯的這個Bug吧。。
首先,作者想用一段程式來建立一個檔案,如果有檔案名稱的話,就建立真正的檔案,如果沒有的話,就調用?tmpfile()?建立臨時檔案。他這段程式就是HTTP下載的C程式。code==200就是HTTP的返回碼。
| 1 2 3 4 |
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:
| 1 2 3 4 5 6 7 |
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 (); } |
然後,作者覺得需要寫一個跨平台的編譯:
| 1 2 3 4 5 6 7 |
FILE * tmpfile ( void ) { #ifndef _WIN32 return tmpfile (); #else //code for Windows; #endif } |
然後,作者覺得這樣實現很不好,會發現名字衝突,因為這樣一來這個函數太難看了。於是他重構了一下他的代碼——寫一個自己實現的tmpfile() – w32_tmpfile,然後,在Windows 下用宏定義來重新命名這個函數為tmpfile()。(陳皓註:這種用法是比較標準的跨平台代碼的寫法)
| 1 2 3 4 5 6 7 |
#ifdef _WIN32 #define tmpfile w32_tmpfile #endif FILE * w32_tmpfile ( void ) { //code for Windows; } |
搞定。編譯器,運行。靠。居然沒有調用到我的w32_tmpfile(),什麼問題。調試,單步跟蹤,果然沒有調用到。難道是問號運算式有問題。改成if – else 語句,好了。
| 1 2 3 4 5 |
if (NULL != fname) { g = fopen (fname, "w+" ); } else { g = tmpfile (); } |
問號運算式不應該有問題吧,難道我們的宏對問號運算式不起作用,這難道是編譯器的先行編譯的一個bug。作者懷疑到。
現在我們把所有的代碼連在一起看,並比較一下:
能正常工作的代碼
能工作的代碼
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#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 (); } } |
不能正常工作的代碼
不能工作的代碼
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#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,但是作者沒有。所有的問題都出在注釋上:
| 1 2 3 |
/* 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能工作的原因是因為作者注釋了老的問號運算式的代碼,所以,那段能工作的代碼成了:
| 1 2 3 4 5 6 7 |
/* 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的類型,然後我需要在My Code裡 把這個int* pInt作除數。於是My Code成了下面的這個樣子:
float result = num/*pInt;
….
/* some comments */
-x<10 ? f(result):f(-result);
因為我在我當時用vi編寫代碼,所以沒有文法高亮,而我的程式都編譯通過了,但是卻出現了很奇怪的事。我也不知道,用gdb調式的時候,發現有些語句直接就過了。這個問題讓我花了很多時間,最後發現問題原來是沒有空格導致的,TNND,下面我用代碼高亮的外掛程式來顯示上面的代碼,
| 1 2 3 4 5 6 |
float result = num /*pInt; .... /* some comments */ -x<10 ? f(result):f(-result); |
Holly Shit! My Code成了:
| 1 |
float result = num-x<10 ? f(result):f(-result); |
媽的。我的這個錯誤在愚蠢程度上和上面那個作者出的錯誤有一拼。
(全文完)