C語言中史上最愚蠢的Bug
文章很有意思,分享給大家。我相信這樣的bug,就算你是高手你也會犯的。你來看看作者犯的這個Bug吧。。
首先,作者想用一段程序來創(chuàng)建一個文件,如果有文件名的話,就創(chuàng)建真正的文件,如果沒有的話,就調(diào)用tmpfile()?創(chuàng)建臨時文件。他這段程序就是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()的實現(xiàn)?居然選擇了 C:\ 作為臨時文件的存放目錄,這對于那些沒有管理員權(quán)限的人來說就出大問題了,在Windows 7下,就算你有管理員權(quán)限也會有問題。所以,上面的程序在Windows平臺下需要用不同的方式來處理,不能直接使用Windows的tmpfile()函數(shù)。
于是作者就先把這個問題記下來,在注釋中寫下了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
- }
然后,作者覺得這樣實現(xiàn)很不好,會發(fā)現(xiàn)名字沖突,因為這樣一來這個函數(shù)太難看了。于是他重構(gòu)了一下他的代碼——寫一個自己實現(xiàn)的tmpfile() – w32_tmpfile,然后,在Windows 下用宏定義來重命名這個函數(shù)為tmpfile()。(陳皓注:這種用法是比較標(biāo)準(zhǔn)的跨平臺代碼的寫法)
- #ifdef _WIN32
- #define tmpfile w32_tmpfile
- #endif
- FILE * w32_tmpfile ( void ) {
- //code for Windows;
- }
搞定!編譯程序,運行。靠!居然沒有調(diào)用到我的w32_tmpfile(),什么問題?調(diào)試,單步跟蹤,果然沒有調(diào)用到!難道是問號表達(dá)式有問題?改成if – else 語句,好了!
- if(NULL != fname) {
- g = fopen(fname, "w+");
- } else {
- g = tmpfile();
- }
問號表達(dá)式不應(yīng)該有問題吧,難道我們的宏對問號表達(dá)式不直作用,這難道是編譯器的預(yù)編譯的一個bug?作者懷疑到。
現(xiàn)在我們把所有的代碼連在一起看,并比較一下:
能正常工作的代碼
能工作的代碼
- #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中,“\” 代表此行沒有結(jié)束,于是,后面的代碼也成了注釋。這就是這個bug的真正原因!
而之所以改成if-else能工作的原因是因為作者注釋了老的問號表達(dá)式的代碼,所以,那段能工作的代碼成了:
- /* 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();
- }
我相信,當(dāng)作者找到這個問題的原因后,一定會罵一句“媽的”!我也相信,這個bug花費了作者很多時間!
最后,我也share一個我以前犯的一個錯。
我有一個小函數(shù),需要傳入一個int* pInt的類型,然后我需要在我的代碼里 把這個int* pInt作除數(shù)。于是我的代碼成了下面的這個樣子:
- float result = num/*pInt;
- ….
- /* some comments */
- -x<10 ? f(result):f(-result);
因為我在我當(dāng)時用vi編寫代碼,所以沒有語法高亮,而我的程序都編譯通過了,但是卻出現(xiàn)了很奇怪的事。我也不知道,用gdb調(diào)式的時候,發(fā)現(xiàn)有些語句直接就過了。這個問題讓我花了很多時間,最后發(fā)現(xiàn)問題原來是沒有空格導(dǎo)致的,TNND,下面我用代碼高亮的插件來顯示上面的代碼,
- float result = num/*pInt;
- ....
- /* some comments */
- -x<10 ? f(result):f(-result);
我的代碼成了:
- float result = num-x<10 ? f(result):f(-result);
我的這個錯誤在愚蠢程度上和上面那個作者出的錯誤有一拼。
文章出自:http://coolshell.cn/articles/5388.html


2010-01-15 19:17:48
2011-07-14 10:23:33




