Fundamentals 7 min read

The Most Stupid C Bug Ever: How a Backslash Turned Code Into a Comment

A seemingly harmless C bug caused a Windows build to ignore a ternary file‑creation expression because a stray backslash in a comment turned the following code into a comment, illustrating how tiny syntax errors can cripple cross‑platform programs.

Liangxu Linux
Liangxu Linux
Liangxu Linux
The Most Stupid C Bug Ever: How a Backslash Turned Code Into a Comment

The article recounts a puzzling C bug that even experienced programmers can fall into when writing cross‑platform code that creates a file using tmpfile() on Windows.

Original code intended to download a file and either open a user‑specified filename or create a temporary file:

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

On Unix/Linux this works, but Microsoft’s implementation of tmpfile() places the temporary file in C:\, which fails for users without sufficient permissions, especially on Windows 7.

To handle the Windows case, the author added a FIXME comment and tried to provide a custom implementation:

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();
}

He then wrote a wrapper function and a macro to rename it on Windows:

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

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

Although the program compiled, the custom w32_tmpfile() was never called. Debugging showed that the ternary expression was being ignored, likely because the macro did not affect it.

Replacing the ternary with an explicit if‑else fixed the issue:

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

The article then compares two versions of the code. The working version uses the macro and the if‑else construct, while the non‑working version keeps the ternary expression:

#ifdef _WIN32
  #define tmpfile w32_tmpfile
#endif

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

else if (code == 200) { /* … */
    // FIXME …
    // 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) { /* … */
    // FIXME …
    g = fname ? fopen(fname, "w+") : tmpfile();
}

The root cause turned out to be a stray backslash at the end of a comment line: /* Write new file (plus allow reading once we finish) */ followed by

// FIXME Win32 native version fails here because Microsoft's version of tmpfile() creates the file in C:

In C, the backslash escapes the newline, so the comment actually continues onto the next line, commenting out the ternary expression entirely. Consequently, the code that should have executed never did.

The author adds a personal anecdote about a similar bug caused by missing whitespace in a comment, which merged code with comment text and produced nonsensical compilation results.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

debuggingCross-platformC++bugPreprocessortmpfilecomment
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.