Skip to content

Commit 83b3306

Browse files
committed
Improve diagnostics on empty stream in compactsize unpacking
1 parent dab44b1 commit 83b3306

3 files changed

Lines changed: 11 additions & 0 deletions

File tree

Changes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Revision history for Perl extension Bitcoin::Crypto.
1212

1313
[Fixes and improvements]
1414
- fixed variable-time cryptographic operation in taproot key tweaking
15+
- improved diagnostics on trying to unpack an empty stream as a compactsize
1516

1617
4.005 Thu Apr 23, 2026
1718
[Public interface changes]

lib/Bitcoin/Crypto/Util/Internal.pm

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,10 @@ sub unpack_compactsize
293293
my $partial = !!$pos_ref;
294294
my $pos = $partial ? $$pos_ref : 0;
295295

296+
Bitcoin::Crypto::Exception->raise(
297+
"cannot unpack CompactSize: no data in stream"
298+
) unless $pos < length $stream;
299+
296300
# if the first byte is 0xfd, 0xfe or 0xff, then CompactSize contains 2, 4 or 8
297301
# bytes respectively
298302
my $value = ord substr $stream, $pos++, 1;

t/Util.t

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,12 @@ subtest 'testing unpack_compactsize with leftover data' => sub {
260260
}, 'Bitcoin::Crypto::Exception';
261261
};
262262

263+
subtest 'testing compactsize with no data' => sub {
264+
isa_ok dies {
265+
unpack_compactsize('');
266+
}, 'Bitcoin::Crypto::Exception';
267+
};
268+
263269
subtest 'testing merkle_root' => sub {
264270
my $block_100022 = [
265271
[

0 commit comments

Comments
 (0)