Safeguard against very large Bitmap creation#2662
Merged
allenchen1154 merged 2 commits intomasterfrom Oct 31, 2025
Merged
Conversation
Because we rely on the transform `Matrix` returned from the `Canvas` to size a `Bitmap` when using software rendering, an erroneous matrix value can cause a very large Bitmap to be allocated, triggering an OutOfMemory exception. We've encountered such a situation when using `layoutlib` for screenshot tests, which uses a `NopCanvas` [on initial render](https://cs.android.com/android/_/android/platform/frameworks/layoutlib/+/7b05b277beee599532606e9bb6d7a71f5ca2ab6e:bridge/src/com/android/layoutlib/bridge/impl/RenderSessionImpl.java;l=543;bpv=1;bpt=0;drc=085857f145aac790e2a08cf6eb9546f98e26c338) that can return an invalid `Matrix.
gpeal
reviewed
Sep 25, 2025
|
|
||
| if (renderWidth <= 0 || renderHeight <= 0) { | ||
| // Safeguard against errors during Bitmap creation by returning early if dimensions are invalid. | ||
| if (renderWidth <= 0 || renderHeight <= 0 || renderWidth > bounds.width() || renderHeight > bounds.height()) { |
Collaborator
There was a problem hiding this comment.
Wouldn't this not render an image if it scaled up so it covered the whole screen + 1px?
Collaborator
Author
There was a problem hiding this comment.
In that case can we clip the bounds so that we only allocate a bitmap large enough to draw what's shown onscreen?
I'm trying to guard against cases where the Canvas matrix returns an invalid scale or translation that causes something like a 1,000,000 x 1,000,000 bitmap to be allocated.
Collaborator
|
Could you take a look at the snapshot tests? Looks like there are a bunch of blanks now. |
gpeal
approved these changes
Oct 31, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Because we rely on the transform
Matrixreturned from theCanvasto size aBitmapwhen using software rendering, an erroneous matrix value can cause a very large Bitmap to be allocated, triggering an OutOfMemory exception.We've encountered such a situation when using
layoutlibfor screenshot tests, which uses aNopCanvason initial render that can return an invalid `Matrix.