Skip to content

fortran args regex undercounts when a CPP-guarded parameter's #if expression contains ')' before the real signature closes #2077

Closed

Description

@squid-protocol

Summary

GitGalaxy's fortran args regex (gitgalaxy/standards/language_standards.py) badly
undercounts a subroutine's parameter count when the parameter list itself contains an embedded
C-preprocessor conditional (#if (...)/#ifdef) whose own guard expression contains a )
before the real signature's closing paren is reached. Confirmed via
tri-comparison-ledger-sweep's fortran pass, ledger shape
fortran/function/args/agree[none]_vs[ctags,gitgalaxy,tree_sitter].

Repro

language-crucible/data/fortran/wrf/module_physics_init.F, phy_init (line 31): a real,
677-parameter subroutine signature (confirmed via ctags --fields=+S, which correctly reports
677 comma-separated dummy arguments). The parameter list itself spans ~245 continuation lines and
is interleaved with several #if ( EM_CORE == 1 ) / #endif guards for optional WRF-CAM
parameters (real-world WRF style: a param name list continued with &, occasionally interrupted
by a CPP conditional wrapping a handful of optional names).

GitGalaxy reports 40 args for phy_init; ctags correctly reports 677.

Root cause

The fortran args rule's first alternative is:

\b(?:SUBROUTINE|FUNCTION|ENTRY)\s+([A-Za-z_]\w*)\s*(\([^)]*\)|)

[^)]* is a negated-character-class scan that matches everything up to the first literal )
it finds, greedily, with no paren-depth awareness. In this file, the first ) encountered after
SUBROUTINE phy_init ( is not the signature's real closing paren -- it's the ) that closes
the embedded #if ( EM_CORE == 1 ) guard's own condition, ~12 lines into the parameter list. The
regex match ends there, capturing only the ~40 real parameter names that appear before that
#if line. _calculate_block_metrics (gitgalaxy/core/detector.py) then derives the args count
from that single, truncated match via .search() (first match only) -- there is no fallback to
keep scanning past a ) that turns out not to be the real terminator.

This is a different bug from #1973 (_mode_a_args_window_end, already fixed) -- that fix
correctly bounds where the search window ends (confirmed via direct trace: for this exact
file, the window correctly extends through the real closing paren at line 276, well under the
300-line cap, treating #if/#endif/blank/!comment lines as transparent). The bug here is
downstream: once the window is right, the args regex itself still can't count past a )
that appears inside a nested/CPP-guarded sub-expression before the signature's own close.

Tree-sitter (39) is also wrong on this same case, but for an unrelated, already-documented
reason (#1709: tree-sitter-fortran's grammar cascades into an ERROR/preproc_* node at the
same embedded #if, corrupting its own parse of everything after it). ctags is the only tool
that gets this right, because its signature accumulation isn't a single bounded regex.

Why this isn't a one-line fix

A depth-aware paren scanner (matching the style of _cpp_class_has_body's bounded scan or
dart's _count_top_level_args elsewhere in detector.py) would need real paren-depth tracking
across a #if/#endif-interleaved, multi-hundred-line continuation span -- more than a regex
tweak, and needs its own ReDoS/boundary-correctness pass per
gitgalaxy/standards/how_to_add_a_language.md's rules given fortran's existing positional_anchored

  • Mode A quirks. Filing rather than fixing inline.

Suggested fix direction

Give Mode A's fortran args derivation (_slice_by_labels / _calculate_block_metrics in
gitgalaxy/core/detector.py) an args_count_override path the same way ABAP/assembly/agc_assembly
already get one (see #1918) -- a small depth-aware paren scanner over args_search_text (which
_mode_a_args_window_end already correctly bounds) that counts top-level commas while tracking
paren depth, rather than relying on the generic single-regex-match derivation in
_calculate_block_metrics.

Evidence

  • ctags --fields=+S -o - wrf/module_physics_init.F | grep phy_init -- signature field lists
    all 677 real comma-separated dummy arguments.
  • Direct regex trace (LANGUAGE_DEFINITIONS["fortran"]["rules"]["args"].finditer(window)) against
    the correctly-bounded window text: exactly 1 match, whose captured group ends immediately
    before the #if ( EM_CORE == 1 ) guard's own closing paren.

Found via the tri-comparison-ledger-sweep skill's fortran pass (2026-08-21).

Activity

  1. added
    bug
    core-engine
    threat: false-negative
    on Aug 21, 2026
  2. added this to the v2.4.9 milestone on Aug 31, 2026
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-enginepriority: mediumthreat: false-negative

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions