Skip to content

[CP-stable]Check for overflow when computing the pixel buffer size for an animated PNG frame#185621

Merged
auto-submit[bot] merged 1 commit into
flutter:flutter-3.41-candidate.0from
flutteractionsbot:cp-stable-79ab0aabbc8452461f115d18731138d403cad987
Apr 28, 2026
Merged

[CP-stable]Check for overflow when computing the pixel buffer size for an animated PNG frame#185621
auto-submit[bot] merged 1 commit into
flutter:flutter-3.41-candidate.0from
flutteractionsbot:cp-stable-79ab0aabbc8452461f115d18731138d403cad987

Conversation

@flutteractionsbot

@flutteractionsbot flutteractionsbot commented Apr 27, 2026

Copy link
Copy Markdown
Contributor

Issue Link:

What is the link to the issue this cherry-pick is addressing?

http://b/489180577

Impact Description:

Reliability improvement in the animated PNG decoder

Changelog Description:

Fixes a potential integer overflow that could happen when handling some animated PNG files.

Workaround:

Is there a workaround for this issue?

No

Risk:

What is the risk level of this cherry-pick?

  • Low
  • Medium
  • High

Test Coverage:

Are you confident that your fix is well-tested by automated tests?

  • Yes
  • No

Validation Steps:

What are the steps to validate that this fix works?

See http://b/489180577

@flutteractionsbot flutteractionsbot added the cp: review Cherry-picks in the review queue label Apr 27, 2026
@flutteractionsbot

Copy link
Copy Markdown
Contributor Author

@jason-simmons please fill out the PR description above, afterwards the release team will review this request.

@flutter-dashboard

Copy link
Copy Markdown

This pull request was opened from and to a release candidate branch. This should only be done as part of the official Flutter release process. If you are attempting to make a regular contribution to the Flutter project, please close this PR and follow the instructions at Tree Hygiene for detailed instructions on contributing to Flutter.

Reviewers: Use caution before merging pull requests to release branches. Ensure the proper procedure has been followed.

@github-actions github-actions Bot added the engine flutter/engine related. See also e: labels. label Apr 27, 2026

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request introduces a SafeMath utility class to the fml library, providing overflow-checked multiplication for size_t, uint32_t, and uint64_t types using Abseil's uint128 for 64-bit checks. The utility is applied within the APNG image generator to prevent overflows when calculating row bytes and pixel buffer sizes. Review feedback suggests improving documentation by using /// for all members as per the style guide, adding [[nodiscard]] to methods, and refactoring the mul implementation to use if constexpr for better type safety and branch optimization.

Comment on lines +13 to +19
// Math operations that check for overflow.
// Based on Skia's SkSafeMath.
class SafeMath {
public:
bool overflow_detected() const { return overflow_detected_; }

size_t mul(size_t x, size_t y);

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.

medium

The Flutter style guide requires all public members to have documentation using ///. Additionally, it is good practice to mark methods that return a value that should not be ignored with [[nodiscard]].

Suggested change
// Math operations that check for overflow.
// Based on Skia's SkSafeMath.
class SafeMath {
public:
bool overflow_detected() const { return overflow_detected_; }
size_t mul(size_t x, size_t y);
/// Math operations that check for overflow.
/// Based on Skia's SkSafeMath.
class SafeMath {
public:
/// Returns true if any operation performed by this instance has overflowed.
[[nodiscard]] bool overflow_detected() const { return overflow_detected_; }
/// Multiplies two size_t values and returns the result.
///
/// If the multiplication overflows, the internal overflow state is set to true.
[[nodiscard]] size_t mul(size_t x, size_t y);
References
  1. All public members should have documentation. Use /// for public-quality documentation. (link)

Comment on lines +22 to +23
uint32_t mul32(uint32_t x, uint32_t y);
uint64_t mul64(uint64_t x, uint64_t y);

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.

medium

The style guide recommends using /// for documentation even on private members.

Suggested change
uint32_t mul32(uint32_t x, uint32_t y);
uint64_t mul64(uint64_t x, uint64_t y);
/// Multiplies two uint32_t values and checks for overflow.
uint32_t mul32(uint32_t x, uint32_t y);
/// Multiplies two uint64_t values and checks for overflow.
uint64_t mul64(uint64_t x, uint64_t y);
References
  1. Use /// for public-quality documentation, even on private members. (link)

namespace fml {

size_t SafeMath::mul(size_t x, size_t y) {
return sizeof(size_t) == sizeof(uint64_t) ? mul64(x, y) : mul32(x, y);

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.

medium

Using if constexpr with explicit casts is preferred to avoid potential narrowing conversion warnings and to ensure that only the relevant branch is instantiated for the target architecture's size_t size.

Suggested change
return sizeof(size_t) == sizeof(uint64_t) ? mul64(x, y) : mul32(x, y);
if constexpr (sizeof(size_t) == sizeof(uint64_t)) {
return static_cast<size_t>(mul64(x, y));
} else {
return static_cast<size_t>(mul32(x, y));
}

@camsim99 camsim99 added the CICD Run CI/CD label Apr 27, 2026
@camsim99 camsim99 added the autosubmit Merge PR when tree becomes green via auto submit App label Apr 28, 2026
@auto-submit auto-submit Bot merged commit 42d3d75 into flutter:flutter-3.41-candidate.0 Apr 28, 2026
155 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

autosubmit Merge PR when tree becomes green via auto submit App CICD Run CI/CD cp: review Cherry-picks in the review queue engine flutter/engine related. See also e: labels.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants