fix: replace unsafe dprintf macro with safe DPRINTF variadic macro#320
Merged
spe-ciellt merged 2 commits intogerbv:developfrom Mar 3, 2026
Merged
Conversation
Replace `#define dprintf if(DEBUG) printf` with a safe variadic macro:
#define DPRINTF(...) do { if (DEBUG) printf(__VA_ARGS__); } while (0)
The old macro has two problems:
1. It shadows POSIX dprintf(3)
2. The unbraced `if(DEBUG)` causes a dangling-else bug when used
inside an `if` without braces — the `else` binds to `if(DEBUG)`
instead of the outer `if`.
Also:
- Add braces to all single-statement if/else bodies (SEI CERT EXP19-C)
- Remove all goto statements, replacing with structured control flow:
- `goto drill_parse_end` → `parsing_done` flag checked in while loop
- `goto header_again` → `for(;;)` loop with `continue`
- `goto header_junk` → `found_junk` flag checked after switch
- Add [[fallthrough]] annotation for DRILL_M_END → DRILL_M_ENDREWIND
Addresses issues gerbv#220 and gerbv#216.
Co-Authored-By: Henry Gabryjelski <74016805+henrygab@users.noreply.github.com>
Extend the drill.c fix to the rest of the codebase. All 16 files used
the same unsafe pattern: #define dprintf if(DEBUG) printf — which
causes dangling-else bugs and shadows POSIX dprintf(3).
main.c had an even more dangerous variant using two unbraced
statements: printf("%s(): ", __FUNCTION__); printf
Replaced all with the safe variadic macro:
#define DPRINTF(...) do { if (DEBUG) printf(__VA_ARGS__); } while (0)
Addresses issue gerbv#220 (POSIX dprintf shadow / dangling-else hazard).
This was referenced Mar 1, 2026
Contributor
|
Verdict is "good to merge". FYI you could use |
This was referenced Mar 5, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
#define dprintf if(DEBUG) printfmacro with#define DPRINTF(...) do { if (DEBUG) printf(__VA_ARGS__); } while (0)across all 17 source filesif/elsebodies indrill.c(SEI CERT EXP19-C)gotopatterns indrill.c, replacing with structured control flowProblem
The
dprintfmacro defined in 16 files as#define dprintf if(DEBUG) printfhas two issues:Dangling-else bug — the unbraced
ifcauseselseto bind incorrectly:Shadows POSIX
dprintf(3)—dprintfis a standard function for writing to file descriptorsmain.chad an even more dangerous variant with two unbraced statements:Changes
Commit 1 —
drill.c:DPRINTF(...)variadic macrodprintf()toDPRINTF()if/elsebodiesgotopatterns, replacing with structured control flow (parsing_doneflag,for(;;)loop,found_junkflag)[[fallthrough]]annotation for pre-existing intentional fallthroughCommit 2 — remaining 15 files +
main.c:gerber.c,gerbv.c,gerb_file.c,gerb_stats.c,project.c,interface.c,callbacks.c,render.c,attribute.c,draw.c,draw-gdk.c,export-drill.c,export-isel-drill.c,export-rs274x.c,drill_stats.c,main.cAcknowledgment
The unsafe macro problem was originally identified by @henrygab in PR #226 (and earlier PR #210), which proposed fixes for
drill.c. Those PRs were closed without merging. This PR applies the same category of fix using C99 variadic macros (appropriate for the project's C23 standard) and extends it to the full codebase.Addresses
drill.c-- unsafe macros are bad #220 — POSIXdprintfshadow / dangling-else hazarddrill.c-- Avoid floating control statements #216 — Missing braces (SEI CERT EXP19-C) indrill.cTest plan
cmake .. && make -j$(nproc))develop)grep -r '#define dprintf' src/returns zero results