Ray's Knowledge Base
Unlock

A test that calls libc functions with null arguments crashes at -O2 because glibc inlines getc and putc, and its asserts vanish in Release

PitfallVerified 7 Oct 2026Holds anywhere
Pitfall. The symptom, what causes it, and the fix that was run and seen to work.

Symptom#

brahma test_posix passed in a Release build and failed in Debug. With asserts forced on in Release it failed too: Assertion 'stdio->num_bindings == stdio->api_count' failed, 70 bindings against 31 calls, and the log stopped after 27 Captured getc call with 53 Captured exit with code 11 (SIGSEGV).

Cause#

Two things. Release defines NDEBUG, so the test's asserts were compiled out and the check was silently off. And at -O2 glibc's extern inline getc, putc, getchar and putc_unlocked read the FILE directly, so a call with a null FILE* crashes before any interposed function is reached. The unmodified upstream commit fails the same way.

Fix#

Build the test unoptimized and keep its asserts: target_compile_options(test_posix PRIVATE -O0 -UNDEBUG) in the test's CMakeLists. Check any test of this kind by building it in Release as well as Debug, and look for the assert's message in the output, not only the ctest verdict.

Evidence#

After the change, STDIO 78 of 78 and POSIX 102 of 102 in Release and Debug on glibc 2.28, and 111 of 111 POSIX on 2.34 and 2.39 (Ubuntu STDIO 84 of 84). Upstream commit c490be8 with only -UNDEBUG added reproduced STDIO 70 against 31.

Also found by searching

  • Assertion `stdio->num_bindings == stdio->api_count' failed only in Release
  • test passes in Release but fails in Debug with asserts
  • Error: signal 11 after getc call in an interposition test
  • ctest passes but the assert is compiled out with NDEBUG