Skip to content

fix(core): blank statically-dead C preprocessor branches before counting (#2814) - #2950

Merged
squid-protocol merged 2 commits into
mainfrom
fix/2814-dead-preproc-branch-counts
Sep 11, 2026
Merged

squid-protocol merged 2 commits into
mainfrom
fix/2814-dead-preproc-branch-counts

Conversation

@squid-protocol

@squid-protocol squid-protocol commented Sep 11, 2026 •

Copy link
Copy Markdown
Owner

What & why

Closes #2814 (stream-layer child of #2812). A rule hit inside a statically-dead C #if 0 branch was counted at file level (equations), in threat_locations, and in each function's hit_vector — even though the engine already treats #if 0 as dead when it slices function boundaries.

The two paths disagreed:

  • Boundary path — _build_brace_safe_stream's #1720 macro shield blanks #if 0 / the dead side of #if 1, so a function inside a dead branch is never sliced.
  • Count path — coding_analysis() ran every rule regex against the raw segment, so the dead-branch hit was counted anyway.

#2813 (raw recorded counts) made this visible: the old test passed for the wrong reason — a same-function strncpy dampener netted the dead strcpy to 0 in place.

Change

  • New _blank_dead_preproc_branches, applied C-family-gated in coding_analysis before the rule loop. It blanks only the body of statically-dead branches (length-preserving → all spatial_map/threat_locations offsets stay valid, so _calculate_block_metrics needs no change).
  • It deliberately leaves the branch's own #if/#elif/#else/#endif markers and every live directive intact, so rules that match directives keep working: cpp import (#include), csharp safety_bypasses (#pragma warning disable), objective-c import (#import).
  • Unknown conditions (#if FOO, #ifdef X, #if defined(X)) keep both branches counted, matching the #1720 boundary semantics — the engine can't decide them without a macro table.
  • Both paths now share _C_FAMILY_MACRO_LANGS so the gate can't drift.
  • STREAM_CONTRACT gains corollary 4 (rendered into docs/signal_contracts.md); the dead-branch test is rewritten to pin #if 0 → 0 and #if defined(X) → 1, plus a new nesting/#else/header-guard test.

Golden master regeneration (why the crucible output changed)

Recorded counts for C-family files with dead branches now drop, so the golden crucible drifted on 27 corpus files carrying #if 0 / dead-#if 1 regions — e.g. cosmopolitan/loader.c (two unused static helpers, StrRChr/BaseName, behind #if 0), cpython/dictobject.c, sqlite, godot, gnucobol_internals/*.c, and the embedded .c files in scheme/racket. Every drifted value is a reduction, and every drifted file is C-family (or a directory-group average that contains a C file); no .scm/.rkt/.py/.js etc. entry changed. Both golden_master_audit.json and golden_master_zero_dep_audit.json were regenerated in isolated venvs matching each CI leg and the crucible test passes green against both.

Verification

  • tests/core_engine/ — full suite green (incl. #1720/#1764/static-truth/silencer siblings and the new nesting test).
  • Golden crucible: passes locally in both legs (full-precision + zero-dependency) against the regenerated fixtures.
  • Signal-contract-sheet + dead-key audits pass; signal_contract_audit.py --ci gate green.
  • Behavioural spot-check on the real c/cpp registry: system() in #if 0 → not counted; live #include still counted.
  • ruff (0 findings on changed source) + mypy (no new errors) clean on the touched files.

Opened as draft so the pull_request-only mypy/ruff audits gate before merge.

🤖 Generated with Claude Code

https://claude.ai/code/session_01WrjQkxP2mdzRzaJvkDS8be

…ing -- closes #2814

The count path and the boundary path disagreed about dead branches. The
#1720 macro shield in `_build_brace_safe_stream` prunes `#if 0` / dead-side
`#if 1` branches for function-boundary detection, but `coding_analysis` ran
every rule regex against the raw segment -- so a `strcpy` inside `#if 0` was
counted at file level (`equations`), in `threat_locations`, and (via the raw
`spatial_map` offsets) in each function's `hit_vector`. #2813 (raw counts)
made this visible: the old test read 0 only because a same-function `strncpy`
dampener was subtracted in place.

Add `_blank_dead_preproc_branches`, applied C-family-gated in `coding_analysis`
before the rule loop. It blanks only the *body* of statically-dead branches
(length-preserving, so all offsets stay valid), leaving the branch's own
`#if/#elif/#else/#endif` markers and every live directive intact -- so rules
that legitimately match directives (cpp `import` on `#include`, csharp
`safety_bypasses` on `#pragma`, objective-c `import` on `#import`) are
unaffected. Unknown conditions (`#if FOO`, `#ifdef`, `#if defined(X)`) keep
both branches counted, matching the #1720 boundary semantics. Both paths now
share `_C_FAMILY_MACRO_LANGS` so they cannot drift.

STREAM_CONTRACT gains corollary 4 documenting this; the dead-branch test is
rewritten to pin `#if 0` -> 0, `#if defined(X)` -> 1.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Claude-Session: https://claude.ai/code/session_01WrjQkxP2mdzRzaJvkDS8be
@github-actions

Copy link
Copy Markdown
Contributor

🐦‍⬛ Muninn Security Scan

✅ No security issues found.

🐦‍⬛ Powered by Muninn · Skald Lab

…sarial test

The #2814 change drops the counts of code inside statically-dead C
preprocessor branches, so the golden crucible drifted on 27 C-family corpus
files that carry `#if 0` / dead-`#if 1` regions (e.g. cosmopolitan/loader.c's
two unused static helpers behind `#if 0`, cpython/dictobject.c, sqlite,
godot, gnucobol_internals/*.c, and the embedded `.c` in scheme/racket). Every
drifted value is a reduction and every drifted file is C-family (or a
directory-group average that contains a C file); no `.scm/.rkt/.py/.js` etc.
entry changed. Regenerated both fixtures in isolated venvs matching each CI
leg (full-precision: networkx+tiktoken+pandas+xgboost; zero-dependency:
PyYAML only) and confirmed the crucible test passes green against both.

Also add `test_detector_c_macro_dead_branch_nesting_and_else` pinning the
tricky shapes: a dead `#if 0` nested in a live `#if 1`, the dead side of
`#if 1`/`#else`, and an `#ifndef` header guard (unknown -> both sides live).

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Claude-Session: https://claude.ai/code/session_01WrjQkxP2mdzRzaJvkDS8be
@squid-protocol
squid-protocol marked this pull request as ready for review September 11, 2026 02:28
@squid-protocol
squid-protocol merged commit fa44268 into main Sep 11, 2026
32 checks passed
@squid-protocol
squid-protocol deleted the fix/2814-dead-preproc-branch-counts branch September 11, 2026 02:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Dead C preprocessor branches (#if 0) are counted at file and function level; the macro shield only serves brace matching

1 participant