Skip to content

csharp func_start: return-type loop swallows call-expression fragments as fake return types #2035

Closed

Description

@squid-protocol

Summary

func_start's "IRON WALL RETURN TYPE" loop (Branches A/B, gitgalaxy/standards/language_standards.py
~line 2092/2116) uses a per-token character class that permits (, ), and , with no balance
requirement: [a-zA-Z0-9_<>\[\]?,.()*]+. This is intentional for legitimate tuple return types
((int, string) Foo(...)) and dotted namespace types, but because parens in a token don't have to
be balanced, the loop can also swallow an unbalanced opening paren from a real call expression
and keep consuming subsequent "tokens" (including nested call args across a comma) until it
reaches some later bare identifier immediately followed by ( — which then gets captured as the
"function name," even though it's actually a nested call, not a declaration.

Evidence (confirmed via direct regex testing, not just observed in ledger output)

Found investigating tri_comparison_ledger.json's
csharp/function/existence/agree[gitgalaxy]_vs[ctags,tree_sitter] shape (48 occurrences) against
language-crucible/data/csharp/roslyn/{CSharpCompilation,LanguageParser}.cs — 46 of the 48 are
genuine local functions inside a known, already-documented tree-sitter parse-error cascade
(Claim 3 in docs/why_gitgalaxy_beats_ast_here.md, unrelated to this bug), but 2 are real
GitGalaxy false positives with this identical root cause:

Case 1 — roslyn/CSharpCompilation.cs:2282:

if (!(TypeSymbol.Equals(namedType.ConstructedFrom, GetWellKnownType(WellKnownType.System_Threading_Tasks_Task), TypeCompareKind.ConsiderEverything2) ||
      TypeSymbol.Equals(namedType.ConstructedFrom, GetWellKnownType(WellKnownType.System_Threading_Tasks_Task_T), TypeCompareKind.ConsiderEverything2)))

GetWellKnownType has no declaration anywhere in the file (confirmed via
grep -n GetWellKnownType) — every occurrence is a call. Branch B's return-type loop consumes
TypeSymbol.Equals(namedType.ConstructedFrom, as a single "return type token" (the char class
allows the unbalanced ( and the trailing ,), then captures GetWellKnownType( as the
function.

Case 2 — roslyn/LanguageParser.cs:2338, inside a switch expression:

: _syntaxFactory.TypeConstraint(AddTrailingSkippedSyntax(
    this.AddError(this.CreateMissingIdentifierName(), ErrorCode.ERR_NoDelegateConstraint),
    this.EatToken())),

_ => parseTypeOrAllowsConstraint(),

Branch B's loop consumes the this.AddError(this.CreateMissingIdentifierName(), ...), fragment as
"return type," then captures this.EatToken as the function name (group 2 in a direct regex
test, confirming Branch B not Branch C).

Reproduced standalone (isolated from the pipeline) against both snippets — func_start.finditer()
matches GetWellKnownType and this.EatToken respectively in each case, confirming this is a
regex defect, not a prism.py/detector.py interaction.

Scope in the sampled corpus

2 false positives out of 461 gg_funcs in LanguageParser.cs and 197 in CSharpCompilation.cs
(99.8%+ precision) — rare, but a real, reproducible defect, not noise. Likely also reproducible in
any C# source with a call expression spanning a line boundary inside a boolean/nested-call
condition, immediately preceding an unrelated bare-identifier call.

Suggested fix direction (not yet attempted — flagging for design + full corpus verification)

Bound each return-type-loop "token"'s paren usage to a single balanced, non-nested group instead
of allowing bare unmatched (/): e.g. (?:[a-zA-Z0-9_<>\[\]?.*]+|\([^()]{0,80}\))+ in place of
the current flat class, for both Branch A (~line 2092) and Branch B (~line 2116). This should still
support real tuple return types ((int, string)) and dotted/generic types, while refusing to
absorb an unbalanced ( that opens a real call. Needs verification against the full ~80-repo
crucible corpus
(tests/tools/crucible_check.py) before landing — this is a heavily-hardened
ReDoS-safe "IRON WALL" regex with a long fix history (#1314 and others), so any change here needs
the full differential-scan protocol (extraction gauntlet + strict tests, both golden masters
re-blessed) per CLAUDE.md, not just a local spot check.

Found via the tri-comparison-ledger-sweep skill's csharp pass.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugcore-enginethreat: false-positive

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions