Created attachment 33086 [details]
"All functions that reference (FILE *) objects, except those with names ending in _unlocked, shall behave as if they use flockfile() and funlockfile() internally to obtain ownership of these (FILE *) objects."
GCC's transformation that eliminates printf("") causes this requirement to be ignored; the calling thread is allowed to proceed even if a lock on stdout held by another thread should block printf.
The included test-case demonstrates the issue. It should deadlock, and indeed it does with -fno-builtin. But with the default settings, the program terminates normally.
Additional thoughts on the above POSIX requirement are on a blog post I wrote a while back, http://ewontfix.com/8/
Since printf("") does not need to obtain ownership of stdout there is no conflict here. There is no requirement that it *must* obtain ownership even if not needed.
Yes it does need to obtain ownership of the file: to set the orientation to byte mode. This is required even by ISO C (without POSIX). A simpler test case:
Sorry, that should be:
printf("%d\n", fwide(stdout, 0));
I reopened this, but now that I'm reporting a new symptom that's independent of the original symptom, I'm wondering whether I should file a separate bug report for it, or continue the issue under this original report. Which way is best? Both have the same fix (removing the invalid optimization or replacing it with a valid one), but the user-visible erroneous behaviors are distinct.