Skip to content

Shorten: avoid signed integer overflow in getRiceGolombCode - #1496

Merged
ufleisch merged 1 commit into
taglib:masterfrom
Leythertag:shorten-rice-golomb-overflow
Nov 20, 2026
Merged

ufleisch merged 1 commit into
taglib:masterfrom
Leythertag:shorten-rice-golomb-overflow

Conversation

@Leythertag

Copy link
Copy Markdown
Contributor

Fixes the signed-integer overflow reported in #1495.

VariableLengthInput::getRiceGolombCode builds the Rice-Golomb quotient in a signed int32_t (result) and then left-shifts it (result << k). The quotient comes from the unary prefix of the bitstream, which is attacker-controlled, so a long prefix makes result large and the left shift overflows a signed int — undefined behaviour (UBSan aborts at shortenfile.cpp:135). It is reachable from the public FileRef API on an untrusted file.

This changes result to an unsigned uint32_t so the shifts are well-defined (unsigned overflow is defined in C++), and casts back to int32_t on return. Behaviour is unchanged for valid input; only the undefined behaviour on malformed input is removed.

Verification

Found via local libFuzzer + ASan/UBSan fuzzing.

VariableLengthInput::getRiceGolombCode accumulated the Rice-Golomb
quotient in a signed int32_t and then shifted it left. The quotient
comes from the unary prefix of the bitstream, which is attacker-
controlled, so a long prefix makes the value large and the left shift
overflows a signed int (undefined behaviour).

Accumulate the quotient in an unsigned uint32_t so the shifts are
well-defined, and convert back to int32_t on return. Behaviour is
unchanged for valid input.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@ufleisch

Copy link
Copy Markdown
Contributor

Thanks!

@ufleisch
ufleisch merged commit 7333be1 into taglib:master Nov 20, 2026
5 checks passed
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.

2 participants