Skip to content

txscript: restore tokenizer test coverage and add issue #1508 regression tests - #2670

Open
parthod0x wants to merge 2 commits into
btcsuite:masterfrom
parthod0x:test/txscript-tokenizer-and-sigop-1508
Open

parthod0x wants to merge 2 commits into
btcsuite:masterfrom
parthod0x:test/txscript-tokenizer-and-sigop-1508

Conversation

@parthod0x

Copy link
Copy Markdown

Two test-only improvements to txscript — no production or consensus code is changed.

1. Re-enable TestScriptTokenizer

TestScriptTokenizer began with an unconditional t.Skip(), so the script
tokenizer — a core untrusted-input parser — had no coverage. The skip was there
because the test (ported from Decred) uses the 0xHH{N} repeat short-form,
which parseShortForm's parseHex helper didn't support. This extends
parseHex to handle the {N} repeat suffix and removes the t.Skip(). The
tokenizer itself was never the issue — only the test helper — and all cases now
pass.

2. Regression tests for issue #1508

Issue #1508 ("slice bounds out of range [1:0] in GetWitnessSigOpCount",
reachable from the mempool) is already fixed on master — both
GetWitnessSigOpCount and getWitnessSigOps now guard their length checks
before indexing. This adds two table cases to TestGetWitnessSigOpCount that
exercise the exact historical inputs (empty sigScript + P2SH pkScript + no
witness; and a P2WSH pkScript + empty witness), locking the fix against
regression.

Verification

  • TestScriptTokenizer: skipped/panicked before, passes after.
  • TestGetWitnessSigOpCount: passes on master; temporarily removing the guards
    makes the new cases panic, and restoring them passes — confirming they
    genuinely lock the fix.
  • cd txscript && go test ./... green; go vet ./... clean.

TestScriptTokenizer began with an unconditional t.Skip(), leaving the
core script tokenizer parser without direct coverage.

The test is ported from Decred and uses the "0xHH{N}" short-form repeat
syntax (e.g. "0x01{76}" for 76 bytes of 0x01), which parseShortForm did
not support, so simply removing the skip panicked in mustParseShortForm
with 'bad token "0x01{76}"'. The tokenizer itself was never the problem;
the test helper was.

Extend parseHex to recognise the optional "{N}" repeat suffix and repeat
the decoded bytes N times, then drop the t.Skip() so the tokenizer test
actually runs.

@fametrano fametrano left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tACK 2739f15

go test and go vet pass on txscript at the head. I applied these tests to 8b70634, just before PR #1769 added the empty sigScript check. There the P2SH case panics with the slice bounds out of range [1:0] from ISS #1508, and with the fix it passes. This supports closing ISS #1508. At the head, finalOpcodeData already handles an empty script, so the length check is redundant; the test guards against the old sigScript[1:] coming back. With only the skip removed, TestScriptTokenizer panics on master because parseHex cannot read 0x01{76}. Commands and output: https://gist.github.com/fametrano/f572a177d7aaa389a7a0aca35d58de05/13224e8c58cfc5fd6e7f7ef16a68095d62ebb0b7

Not blocking: the P2WSH empty witness case also passes before the fix, because getWitnessSigOps already checked len(witness) > 0 in v0.20.1-beta, so it is not a variant of ISS #1508 as its comment says. Three added lines run past 80 columns.

@parthod0x

Copy link
Copy Markdown
Author

Thanks for the careful review and tACK — and for independently reproducing the issue #1508 panic against 8b70634 before the fix. That's exactly the confirmation this was meant to lock in.

Both of your non-blocking notes are addressed in 12655e2:

  • P2WSH empty-witness comment. You're right — that case is not a variant of issue panic: runtime error: slice bounds out of range | github.com/btcsuite/btcd/txscript.GetWitnessSigOpCount #1508. getWitnessSigOps already checked len(witness) > 0 before indexing (back to v0.20.1-beta), so it never panicked. The comment now states that explicitly; the case simply pins the expected sig op count of 0 as companion coverage.
  • 80-column overruns. Wrapped the three offending lines (the repeat-count error in parseHex and two lines in the P2SH regression case).

No functional change — go vet and the full txscript module tests still pass.

@fametrano fametrano left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tACK 12655e2

The new commit changes only comments, a test name and line wraps, and no added line now runs past 80 columns. I ran the same script against this head, with only its sha and the pattern for the renamed P2SH case changed: on 8b70634 the P2SH case still panics with slice bounds out of range [1:0], with PR #1769 it passes, and go test and go vet pass on txscript. Commands and output: https://gist.github.com/fametrano/f572a177d7aaa389a7a0aca35d58de05/a77dcdf8fb8e90f465e7ad2064d0b7a0313ce204

Not blocking: the new P2WSH comment and the commit message say GetWitnessSigOpCount guards the empty witness, but the len(witness) > 0 check is in getWitnessSigOps, which it calls.

@parthod0x

Copy link
Copy Markdown
Author

Good catch, and you're right — the len(witness) > 0 guard is in getWitnessSigOps, which GetWitnessSigOpCount calls, not in GetWitnessSigOpCount itself. It's comment- and commit-message wording only, and you've already tACK'd 12655e2c off a fresh reproduction, so I'd rather not reset the approval with another push purely for this. Happy to correct both lines if anything else comes up before merge, or if a maintainer would prefer it fixed first. Thanks again for the thorough re-review and the independent repro.

@allocz allocz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Last two commits can be squashed, right? Code looks good, tests passing.

…anic

Issue btcsuite#1508 reported a "slice bounds out of range [1:0]" panic in
GetWitnessSigOpCount, reachable from the mempool during transaction
acceptance, when counting signature operations for a spend whose
signature script was empty on the nested-p2sh branch.

Current master already guards that path (len(sigScript) > 0 in the
nested-p2sh branch). Add table cases to TestGetWitnessSigOpCount that
reproduce the historical input - an empty signature script against a
P2SH pkScript with no witness - so the panic cannot silently regress;
it now counts 0 sig ops without panicking.

Also add a bare P2WSH pkScript with an empty witness stack. That path
was never a btcsuite#1508 crash variant - getWitnessSigOps guards for a
non-empty witness before indexing, so it never panicked - and the case
only pins its expected sig op count of 0.
@parthod0x
parthod0x force-pushed the test/txscript-tokenizer-and-sigop-1508 branch from 12655e2 to 149a632 Compare November 12, 2026 14:22
@parthod0x

Copy link
Copy Markdown
Author

Done — squashed the two #1508 test commits into one (149a632); the tokenizer re-enable stays its own commit. Content is unchanged (the squash is byte-identical to the previous tip), so the green CI carries over. Thanks for the review.

@allocz allocz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

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.

3 participants