Skip to content

vp8: avoid int32 overflow in inverseDCT4 - #38

Open
ahmadwyne wants to merge 2 commits into
golang:masterfrom
ahmadwyne:vp8-idct-overflow
Open

ahmadwyne wants to merge 2 commits into
golang:masterfrom
ahmadwyne:vp8-idct-overflow

Conversation

@ahmadwyne

@ahmadwyne ahmadwyne commented Sep 23, 2026 •

Copy link
Copy Markdown

inverseDCT4 multiplied coefficients by 65536*cos(pi/8)sqrt(2), or 85627,
and by 65536
sin(pi/8)*sqrt(2), or 35468, in int32. Both products can
overflow, which flips the sign of a term and corrupts the decoded block.

Dequantized coefficients are stored as an int16, so the first pass wraps
once a coefficient's magnitude exceeds math.MaxInt32/85627, or 25079. A
crafted bitstream can reach that: an AC token value of 89 with a
dequantization factor of 284 gives 25276.

The second pass multiplies the first pass's results, which range up to
about 126000, so it wraps at much smaller coefficients. Multiplying by
35468 overflows once its input exceeds 60548, and two maximal coefficients
in one row already produce 75577.

Widen both multiplications to an int64. That is exact for every int16
input in both passes, and gives the same results as the int32 arithmetic
wherever that did not overflow.

The added test covers both passes. Its want values were computed with the
same transform in int64 arithmetic, and every case fails before this
change.

Fixes golang/go#81659

inverseDCT4 multiplied coefficients by 65536*cos(pi/8)*sqrt(2), or 85627,
in int32. The product overflows once a coefficient's magnitude exceeds
math.MaxInt32/85627, or 25079, flipping the sign of the term and
corrupting the decoded block. Dequantized coefficients are stored as an
int16, so a crafted bitstream can reach that magnitude: an AC token value
of 89 with a dequantization factor of 284 gives 25276.

Split the multiplication into 20091 + 65536, as libwebp's TransformOne
does. That gives the same result whenever the original does not overflow,
and keeps the intermediate product within int32 for any int16 input.

The second pass multiplies the first pass's results, which can still
exceed int32 for extreme inputs. libwebp has the same behaviour there.

Fixes golang/go#81659

Change-Id: I5e5a9fee76e9f8ec61dfebae1af8b2747d94479c
@gopherbot

Copy link
Copy Markdown
Contributor

This PR (HEAD: b3c8262) has been imported to Gerrit for code review.

Please visit Gerrit at https://go-review.googlesource.com/c/image/+/837286.

Important tips:

  • Don't comment on this PR. All discussion takes place in Gerrit.
  • You need a Gmail or other Google account to log in to Gerrit.
  • To change your code in response to feedback:
    • Push a new commit to the branch used by your GitHub PR.
    • A new "patch set" will then appear in Gerrit.
    • Respond to each comment by marking as Done in Gerrit if implemented as suggested. You can alternatively write a reply.
    • Critical: you must click the blue Reply button near the top to publish your Gerrit responses.
    • Multiple commits in the PR will be squashed by GerritBot.
  • The title and description of the GitHub PR are used to construct the final commit message.
    • Edit these as needed via the GitHub web interface (not via Gerrit or git).
    • You should word wrap the PR description at ~76 characters unless you need longer lines (e.g., for tables or URLs).
  • See the Sending a change via GitHub and Reviews sections of the Contribution Guide as well as the FAQ for details.

@gopherbot

Copy link
Copy Markdown
Contributor

Message from Gopher Robot:

Patch Set 1:

Congratulations on opening your first change. Thank you for your contribution!

Next steps:
A maintainer will review your change and provide feedback. See
https://go.dev/doc/contribute#review for more info and tips to get your
patch through code review.

Most changes in the Go project go through a few rounds of revision. This can be
surprising to people new to the project. The careful, iterative review process
is our way of helping mentor contributors and ensuring that their contributions
have a lasting impact.


Please don’t reply on this GitHub thread. Visit golang.org/cl/837286.
After addressing review feedback, remember to publish your drafts!

The previous patch set split the multiplication by 85627 into 20091 + 65536,
which keeps the first pass inside an int32 but leaves the second pass, which
multiplies the first pass's results, still overflowing. Multiplying by 35468
wraps once its input exceeds 60548, and two maximal coefficients in one row
already produce 75577.

Widen both multiplications to an int64 instead, which is exact for every
int16 input in both passes, and add a test case for the second pass.
@ahmadwyne ahmadwyne changed the title vp8: avoid int32 overflow when multiplying by 85627 in inverseDCT4 vp8: avoid int32 overflow in inverseDCT4 Sep 23, 2026
@gopherbot

Copy link
Copy Markdown
Contributor

This PR (HEAD: eb1be93) has been imported to Gerrit for code review.

Please visit Gerrit at https://go-review.googlesource.com/c/image/+/837286.

Important tips:

  • Don't comment on this PR. All discussion takes place in Gerrit.
  • You need a Gmail or other Google account to log in to Gerrit.
  • To change your code in response to feedback:
    • Push a new commit to the branch used by your GitHub PR.
    • A new "patch set" will then appear in Gerrit.
    • Respond to each comment by marking as Done in Gerrit if implemented as suggested. You can alternatively write a reply.
    • Critical: you must click the blue Reply button near the top to publish your Gerrit responses.
    • Multiple commits in the PR will be squashed by GerritBot.
  • The title and description of the GitHub PR are used to construct the final commit message.
    • Edit these as needed via the GitHub web interface (not via Gerrit or git).
    • You should word wrap the PR description at ~76 characters unless you need longer lines (e.g., for tables or URLs).
  • See the Sending a change via GitHub and Reviews sections of the Contribution Guide as well as the FAQ for details.

@gopherbot

Copy link
Copy Markdown
Contributor

Message from Ahmad Wyne:

Patch Set 2:

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/837286.
After addressing review feedback, remember to publish your drafts!

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.

x/image/vp8: int32 overflow in inverseDCT4 produces incorrect decoded pixels

2 participants