Skip to content

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

Closed

Description

@squid-protocol

Found while moving the proximity weights out of the recorded counts (#2813, roadmap Phase 2).

What the test claimed

tests/core_engine/test_detector.py::test_detector_c_macro_dead_branch_shield asserted that a strcpy inside a dead preprocessor branch is "scrubbed by the preprocessor shield before the regex engine even sees it" (high_risk_execution == 0).

What actually happens

The recorded count was 0 only because the strncpy in the #else branch, 500 chars away in the same function, tallied one mitigated_danger and the Silencer Region subtracted it from the count in place. With the count raw (#2813) the truth is visible:

#if 0                     | equations: {'safety': 1, 'high_risk_execution': 1} | tally: {'mitigated_danger': 1}
#if defined(DEBUG_MODE)   | equations: {'safety': 1, 'high_risk_execution': 1} | tally: {'mitigated_danger': 1}
func hit_vectors (both):  [('system_init', {'func_start': 1, 'high_risk_execution': 1, 'safety': 1})]
threat_locations (both):  {'high_risk_execution': [3], 'safety': [5]}

_build_brace_safe_stream's macro shield (the #if 1 / #if 0 policy stack, #1720) only produces the brace-safe stream used to find function boundaries. Neither coding_analysis()'s file-level counts nor _calculate_block_metrics' per-function hit_vector read that stream, so a statically dead #if 0 branch is counted exactly like live code, at both levels.

Why it matters

This is a stream-contract question (gitgalaxy/standards/signal_contracts.py STREAM_CONTRACT): does a rule see text the compiler never compiles? Today's answer is yes and it is written down nowhere. Either the stream contract states it (and the test pins it as such), or the dead branch is blanked in the code stream for the C family before counting. Not decided here; the test now pins the current behaviour and points at this issue.

Part of #2812 (stream layer).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugcore-engine

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions