Skip to content

perl: bodyless forward declaration can accidentally grab an unrelated nearby { as its body #1609

Closed

Description

@squid-protocol

Summary

Confirmed via language-crucible/data/perl/exiftool/exiftool (corpus commit 12fc7af): sub GetASCII($); (line 90, a bodyless forward declaration with NO real definition anywhere later in the file -- unusual but real, the corpus never actually implements it with a body) gets incorrectly reported by GitGalaxy as a real function spanning line 90-92, with a bogus 3-line body.

Root cause

_slice_by_braces's generic Mode-B fallback (gitgalaxy/core/detector.py, the final else: brace_idx = safe_code.find(opener, start_idx, search_limit) branch) searches forward from a func_start match for the next { within a bounded window (search_limit = min(next_match_start, start_idx + 2000)). GetASCII's own declaration line has no { (it ends in ;), but the window before the NEXT real sub/method keyword happens to contain an unrelated END { Cleanup(); } block a few lines later:

sub GetASCII($);

$SIG{INT}  = 'SigInt';  # do cleanup on Ctrl-C
$SIG{CONT} = 'SigCont'; # (allows break-out of delays)
END {
    Cleanup();
}

The brace search finds END's own { and wrongly attributes its 3-line body ({\n Cleanup();\n}) to GetASCII, giving it a completely bogus body/LOC/args, and reporting a function that doesn't actually exist with real code.

Why this usually doesn't happen

Most perl forward declarations in this same file (dozens of them, e.g. sub AbsPath($);) are packed one-per-line immediately followed by MORE forward declarations with no { between them and the next real sub keyword close by -- so search_limit (bounded by next_match_start) is narrow enough that no unrelated { falls inside the window, and the declaration is correctly skipped (confirmed: AbsPath's bodyless declaration produces no entry at all, only its real later definition does). GetASCII is the unlucky case where unrelated non-sub code with a real { (the END block) happens to sit inside the window before the next sub keyword.

Precedent

This is the same general bug shape #1336 diagnosed for objective-c ("bodyless C-style prototypes are only found by accident, with a wrong body span") and #1357 fixed by explicitly detecting the bodyless-; case and rejecting it outright rather than falling through to a blind forward { search. Perl's generic Mode-B fallback (the final else branch used by many languages, not just perl) doesn't have this bodyless-prototype detection at all -- reference #1336/#1357 for the shape of fix that worked there, though perl's own func_start regex/lookahead differs from objc's so the exact implementation won't be a direct copy.

Scope

Discovered incidentally while fixing #1608 (which excludes bodyless perl forward declarations from the tree-sitter-accuracy ground truth's real_functions count) -- #1608's fix was ground-truth-side only and correctly does NOT touch this engine-side bug, which was previously masked in the accuracy measurement (the ground truth used to ALSO count GetASCII's bodyless declaration as if it were real, so GitGalaxy's bogus match happened to "pair" against that phantom entry instead of showing up as a distinct extra_functions false positive). Now that #1608 fixed the ground truth, this bug is visible as a real extra_functions count increase (1 -> 2 in the current perl baseline).

Activity

  1. added
    bug
    testing
    core-engine
    threat: false-positive
    on Aug 14, 2026
  2. added this to the v2.4.8 milestone on Aug 16, 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-enginetestingthreat: false-positive

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions