Skip to content

I Put a Quality Gate on 24,000 Lines of C. Two Crashes Hid Behind Pragmas

I ran clang, GCC, cppcheck, clang-tidy and CodeQL on a 24,000-line C tree. A green -Werror build took 10 pragmas, and 7 silenced sites hid real defects.

· · 10 min read
Bar chart of findings per tool run on mawk, from 0 for -Wall -Wextra to 2,692 for clang-tidy style checks

Does a clean small file say anything about a 24,000-line tree?

My earlier legacy C posts measure files of 55 to 88 lines, and so does the one on three bugs -Wall let through. That's honest, and it's also a toy. Nobody's gate ever failed on 55 lines. So I went and found a tree big enough to argue with.

I picked mawk 1.3.4, snapshot 20260302 (commit cbefe06), because it builds with ./configure && make and nothing else, it was written in 1991, and a maintainer is still touching it. It's 59 files: 35 .c and 24 .h, 24,433 physical lines, 19,235 once blanks and comments are gone. About 4,250 of those lines are checked-in parser generator output, which I left in because that's what a real gate sees. Twenty-eight translation units compile into the binary; the regex files are #included into one of them.

Tools, all on one Mac (arm64, macOS 26): Apple clang 17.0.0, Homebrew LLVM 23.1.3 (clang and clang-tidy), Homebrew GCC 16.2.0, cppcheck 2.22.0, CodeQL CLI 2.27.3 with the cpp-security-and-quality suite. Everything is in the examples/legacy-c-gate-real-tree-suppressions-vs-real-bugs/ folder, including a script that reproduces the counts.

How many warnings does each flag level produce?

I built with ./configure CC=clang --enable-warnings and compiled every unit with -O2 -c. A warning counts once per file, line and column, because headers get reported by every unit that includes them.

Run Unique warnings
clang 17 -Wall -Wextra 0
gcc 16 -Wall -Wextra 0
the 16 options mawk's own --enable-warnings adds 0
clang 17, 20-flag set 59
LLVM clang 23, same 20 flags 66
gcc 16, same flags plus -Wc++-compat and friends 142
clang 17 -Weverything 286

The 20 flags are -Wall -Wextra -Wshadow -Wconversion -Wsign-conversion -Wcast-qual -Wcast-align -Wpointer-arith -Wwrite-strings -Wundef -Wformat=2 -Wold-style-definition -Wstrict-prototypes -Wmissing-prototypes -Wredundant-decls -Wnested-externs -Wdouble-promotion -Wfloat-equal -Wswitch-enum -Wimplicit-fallthrough. I'd have added -Wold-style-cast, but that's C++ only, so for C the nearest thing I could run was GCC's -Wc++-compat.

Zero at -Wall -Wextra is the part worth sitting with. Is this tree clean, or is it hiding something? Both. I removed 341 explicit integer casts from a scratch copy and recompiled with -Wall -Wextra -Wconversion -Wsign-conversion: 1 warning became 283. The wall had already been climbed, one (int) and (size_t) at a time, years ago. Those casts are suppressions that nobody counted.

Two more things moved the numbers without touching the code. Moving from clang 17 to clang 23 added seven -Wimplicit-int-float-conversion warnings. Switching compiler family changed everything else: GCC honours the /* FALLTHRU */ comments mawk is full of, so it reported no fall-through warnings at all, while clang ignores those comments and reported 17. In my opinion that's the strongest argument against picking one compiler's warning list as your gate.

What did the analyzers add?

Tool Findings
clang 17 --analyze 14
clang 23 --analyze 17
gcc 16 -fanalyzer 6
cppcheck (warning, style, performance, portability, inconclusive) 331
clang-tidy, clang-analyzer-*,bugprone-*,cert-* 897
clang-tidy, readability-*,misc-*,performance-*,portability-* 2,692
CodeQL, security-and-quality, files in the build 125

Cppcheck's 331 contains 260 copies of a single style rule about unnamed prototype parameters. Clang-tidy's 897 contains 28 security.ArrayBound findings, 25 of which are one idiom: the pre-C99 struct hack, a char str[2] at the end of a string struct that gets over-allocated and indexed past its declared size. That's what "legacy" looks like to an analyzer.

One build detail cost me an afternoon. A plain ./configure writes the GCC_NORETURN, GCC_PRINTFLIKE and GCC_UNUSED macros as nothing, and only --enable-warnings turns them into real attributes. With them off, clang's analyzer reported 19 findings instead of 14, because it couldn't know that bozo() never returns. Five findings vanished the moment the build told the analyzer the truth. If your gate runs on a default configure, you're measuring a different program.

I read all 28 distinct findings from the three analyzers (clang's 17, GCC's 6 and cppcheck's 5 on one line). One is real: jmp.c:122 subtracts a pointer from a null pointer, which the C standard calls undefined and which does nothing visible. Three are dead stores, two of them in generated parser code. The other 24 are false positives; the analyzer can't see that ast_cnt * PF_last + pf_type is injective, or that a grammar rule only ever hands back a non-null symbol.

For the 749 clang-tidy findings outside the analyzer checks I drew a random sample of 40, seed 20261023, from the sorted list. Thirty-five were style (signed bitwise operands, ignored fprintf return values, reserved identifiers like _ADD_ASG), 4 were false positives, and 1 was a real but harmless sscanf("%d") that has undefined overflow behaviour. One in 40 is a 95% interval of roughly 0.4% to 13% for the whole pool. I read each of the 40 myself, so there's one classifier and no second opinion.

How many suppressions does a green -Werror build take?

I took the 20-flag set on clang 17 and added -Werror. The 59 warning sites broke down like this, and so did the fix:

Warning Sites What I did Lines changed
-Wimplicit-fallthrough 17 comment respelled as __attribute__((fallthrough)) 17
-Wformat-nonliteral 29 file-level pragma in 4 files 4
-Wfloat-equal 11 file-level pragma in 5 files 5
-Wswitch-enum 1 file-level pragma 1
-Wsign-conversion 1 cast was (char), now (SYM_TYPE) 1

The fall-through sites were already annotated, in the spelling GCC reads. Sixteen use /* FALLTHRU */ and one uses /* fall thru */, and my first search missed that one. The total diff is 29 added and 18 removed lines across 11 files, make check printed the same 51 OK lines before and after, and the rest of its output was identical.

Ten pragma lines silenced 41 sites. That's the part I'd flag in code review: a file-level pragma doesn't suppress a warning, it suppresses a category for everything the file will ever contain. Did anyone read those 41 sites first? I did, and it changes the picture.

Waffle chart of 59 clang warnings on mawk: 22 correct formats, 17 intended fall-throughs, 11 float tests, 7 hiding defects
The 59 warning sites in the 20-flag gate, hand-classified. Orange squares are the seven that sat on a real defect.

Which silenced warnings were hiding a real bug?

All 17 fall-through sites were intentional, and all 11 float comparisons are integer round-trip tests like (double) ival == d. The 29 -Wformat-nonliteral sites are where it gets interesting. Twenty-two are awk's printf, whose format string is a runtime value by definition. Seven are not.

The first is field.c:463. When a program assigns CONVFMT, mawk checks it by formatting a sample number into a 512-byte stack buffer using the new value as the format. Two one-line awk programs show what that probe does:

$ mawk 'BEGIN{CONVFMT="%s"; x=3.14159; y = x ""; print y}'; echo $?
139
$ mawk 'BEGIN{CONVFMT="%600.0f"; x=3.14159; y = x ""; print length(y)}'; echo $?
133

Exit 139 is a segfault: the probe reads the double's bit pattern as a char * and the crash report puts strlen under __sprintf_chk under field_assign, field.c:463. The faulting address, 0x40092acd9e83e420, sits within six bytes of the bit pattern of 3.1459, 0x40092acd9e83e426. Exit 133 is the macOS SDK's checked sprintf aborting on a width of 512 or more. Widths from 257 to 511 are rejected with "CONVFMT assigned unusable value", so someone thought about the limit. The %s case slipped through anyway.

The second group is six sites in cast.c, array.c and field.c:558, which format a double with whatever CONVFMT holds. OFMT goes through a type check before use; CONVFMT doesn't. Set it to %d and 3.14159 prints as -266631570, %x gives f01b866e, %ld gives 4614256650576692846. That last number is 0x400921f9f01b866e, the raw bits of the double. It's undefined behaviour, but on this platform it fails by printing nonsense, not by crashing.

The third defect wasn't behind any suppression, which is why I nearly missed it. One of my 40 sampled findings was bugprone-unhandled-code-paths at print.c:1066, a switch with no default. That's a false positive. But two lines above it sits p = strcpy(xbuff, string(OFMT)->str), copying a user-settable string into a 256-byte buffer, and the same line exists at print.c:680. An OFMT of 256 characters or more followed by printf "%d", 1e30 aborts:

$ mawk 'BEGIN{s=sprintf("%400s","x"); gsub(/ /,"a",s); OFMT="%.3f" s; printf "%d\n", 1e30}'; echo $?
133

I bisected the length: 255 characters passes, 256 aborts. The sprintf variant at line 1064 aborts the same way. On those exact lines clang-tidy reported bugprone-signed-bitwise, which is true of every macro in the file and tells you nothing. Clang's analyzer, GCC's -fanalyzer and cppcheck said nothing at all. CodeQL's cpp/unbounded-write was the only rule across all seven tools that pointed at it, as two of its four hits. The other two, hash.c:111 and memory.c:78, are strcpy into a strlen-sized allocation, which is fine.

Both crashes need control of the awk program or a -v assignment, so this is a reliability bug in an interpreter, not a remote hole. Without the SDK's fortification the same writes would land on the stack instead of stopping the process. I haven't reported them upstream yet.

What would I do with this gate?

Keep -Wformat-nonliteral on, and fix instead of pragma. Wrap the few real runtime-format calls in one function with a format attribute and suppress at that one line. A per-file pragma bought me a green build and hid seven problems, which is a bad trade.

Respell the annotations for the compiler you gate on. The 17 comments were correct for GCC and invisible to clang. Treat casts as inventory: 341 of them were holding back 283 warnings, and the next person to refactor will drop one and meet the wall.

Add CodeQL to a nightly job, not to the pull request, and read the CodeQL 2.27.2 recheck list before you trust a version bump. It found the one defect nothing else did, and it also produced 135 results, 16 of them a type-confusion rule objecting to a zmalloc helper that returns a union. That's the tax. If you want a ratchet for the noise, the per-flag approach from the first post above works per rule, and the problems with static analysis under -Werror apply here too. For the other side of the same coin, I counted disable comments in JavaScript in the eslint-disable census, and the shape was similar: mostly explainable, with a few that shouldn't be there.

What I didn't measure

  • One tree. mawk is a well-kept project, and -Wall -Wextra is already clean on it. A neglected tree would score higher on every count here and tell you less about the tools.
  • No CodeQL build trace. codeql database create --command=make hung under macOS SIP, so I used --build-mode=none. It extracts source without compiling it, and configure-generated macros may not resolve the way a real build does.
  • GCC on Linux. GCC 16.2 here is Homebrew's, on macOS. glibc's fortification would behave differently on the two crashes.
  • No second reader. The 40-finding sample and the 28 analyzer findings are my judgment. The 59 gate sites were each read, but the seven I call defects are the ones I could reproduce.
  • No fix upstream. I didn't patch the three defects, so I can't say what the right fix costs.
  • Time. Compilers took 3 seconds per flag set, clang's analyzer 46 seconds, cppcheck 4, and clang-tidy's bug profile 19. I haven't timed CodeQL end to end.

Frequently Asked Questions

How many suppressions does a legacy C tree need to pass -Werror?
On mawk, a 1991 awk with about 24,400 lines, a 20-flag clang set produced 59 warning sites. Ten pragma lines silenced 41 of them, 17 fall-through comments were respelled as attributes and 1 cast was fixed. The count depends on the flag set, since -Wall -Wextra alone produced zero.
Is -Wformat-nonliteral worth turning on for old C code?
Yes, but don't suppress it per file. On mawk it fired 29 times. Twenty-two were correct runtime formats, seven sat on a stack-buffer overflow and a format that expects an integer but receives a double. A file-level pragma hides all 29 and every future one.
Which tool found a strcpy overflow that compilers and clang-tidy missed?
CodeQL 2.27.3 with the cpp-security-and-quality suite flagged the unbounded strcpy of an OFMT string into a 256-byte buffer. GCC 16 and clang 17 with 20 warning flags, clang's analyzer, cppcheck and clang-tidy did not.