mirror of
https://github.com/servo/servo.git
synced 2025-08-04 21:20:23 +01:00
pixels: Add limitation to max image total bytes length (#37172)
Limit the maximum image allocation size to 2GB to minimize the possibility of out of memory errors on some `ImageBitmap`, `ImageData`, `Canvas`, and `OffscreenCanvas` operations such as construction, `toBlob`, and `toDataURL`. Other browsers have similar limits: - Chromium: 2^32-1 (~4GB) - Firefox: 2^31-1 (~2GB) Testing: Improvements to the following tests: - `html/canvas/element/pixel-manipulation/2d.imageData.object.ctor.basics.html` assert_throws_dom("INDEX_SIZE_ERR", function() { new ImageData(1 << 31, 1 << 31); }); - `html/canvas/element/manual/imagebitmap/createImageBitmap-invalid-args.html` makeOversizedCanvas + makeOversizedOffscreenCanvas Signed-off-by: Andrei Volykhin <andrei.volykhin@gmail.com>
This commit is contained in:
parent
3bb7c71eb6
commit
801ac9e22a
6 changed files with 93 additions and 150 deletions
|
@ -31,6 +31,20 @@ pub enum PixelFormat {
|
|||
BGRA8,
|
||||
}
|
||||
|
||||
// Computes image byte length, returning None if overflow occurred or the total length exceeds
|
||||
// the maximum image allocation size.
|
||||
pub fn compute_rgba8_byte_length_if_within_limit(width: usize, height: usize) -> Option<usize> {
|
||||
// Maximum allowed image allocation size (2^31-1 ~ 2GB).
|
||||
const MAX_IMAGE_BYTE_LENGTH: usize = 2147483647;
|
||||
|
||||
// The color components of each pixel must be stored in four sequential
|
||||
// elements in the order of red, green, blue, and then alpha.
|
||||
4usize
|
||||
.checked_mul(width)
|
||||
.and_then(|v| v.checked_mul(height))
|
||||
.filter(|v| *v <= MAX_IMAGE_BYTE_LENGTH)
|
||||
}
|
||||
|
||||
pub fn rgba8_get_rect(pixels: &[u8], size: Size2D<u64>, rect: Rect<u64>) -> Cow<[u8]> {
|
||||
assert!(!rect.is_empty());
|
||||
assert!(Rect::from_size(size).contains_rect(&rect));
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue