GCC obscenity

Today, while debugging some of my code, I accidentaly noticed that in certain cases gcc "optimizes" printf() calls into calls to puts(). This is plainly unacceptable for at least two reasons:

  1. printf() might have side-effects that puts() doesn't (f.ex. see this bug report).

  2. It makes LD_PRELOAD interception of certain functions ineffective.

This "optimization" takes place even when I compile my program with the -ansi switch. I haven't been able to find a switch to turn off this "optimization". In addition, the generated code in this case is plain wrong. No optimization should change the semantics of the program (which it does in this case).

C has always been the language in the lines of do what I say. How many other such surprises are hidden in gcc?

What they have done with this "optimization" is on the level with (if not worse than) MS's adaptation of C++ for CIL. Strangely, so many people bark at MS for their "wrong-doings", but the bug report cited above never even got a response. Well, at least I'm going to say this: shame on you gcc developers!



Anonymous said...

The generated code is not wrong -- for example, the compiler is quite free to open-code a call to a libc function. For example, gcc has open-coded calls to memset, memcpy, and string functions for years -- where have you been?

That in this case open-coding occurs by calling the (faster) puts function makes no difference. While it may be problematic to you and there should be a way to turn off that optimization, it's still not "non-standard" by any stretch of the imagination. The cases you mention, such as redefining printf or using LD_PRELOAD, are clearly outside the scope of ISO C. The bug report you mention actually exposes a bug in a system's C library.

(For that matter, I don't know for a fact that what Microsoft is doing with C++ is non-standard either, so don't take me as a knee-jerk Microsoft-basher either.)

Anonymous said...

Also note that you can disable this optimization using the -fno-builtin-printf flag.