ImageData#constructor: check bounds of dimensions + don't overflow size

Also adds a related `too-large` test.
This commit is contained in:
Halil Durak
2026-02-19 12:02:50 +03:00
parent 944f34b833
commit 96cfdebced
2 changed files with 13 additions and 6 deletions

View File

@@ -59,10 +59,6 @@
} }
</script> </script>
<script id=constructor-invalid-colorspace>
testing.expectError("TypeError", () => {
new ImageData(5, 5, { colorSpace: "display-p3" });
});
</script> </script>
<script id=single-pixel> <script id=single-pixel>
@@ -73,3 +69,7 @@
testing.expectEqual(1, img.height); testing.expectEqual(1, img.height);
} }
</script> </script>
<script id=too-large>
testing.expectError("IndexSizeError", () => new ImageData(2_147_483_648, 2_147_483_648));
</script>

View File

@@ -58,7 +58,10 @@ pub fn constructor(
maybe_settings: ?ConstructorSettings, maybe_settings: ?ConstructorSettings,
page: *Page, page: *Page,
) !*ImageData { ) !*ImageData {
if (width == 0 or height == 0) { // Though arguments are unsigned long, these are capped to max. i32 on Chrome.
// https://github.com/chromium/chromium/blob/main/third_party/blink/renderer/core/html/canvas/image_data.cc#L61
const max_i32 = std.math.maxInt(i32);
if (width == 0 or width > max_i32 or height == 0 or height > max_i32) {
return error.IndexSizeError; return error.IndexSizeError;
} }
@@ -70,7 +73,11 @@ pub fn constructor(
return error.TypeError; return error.TypeError;
} }
const size = width * height * 4; var size, var overflown = @mulWithOverflow(width, height);
if (overflown == 1) return error.IndexSizeError;
size, overflown = @mulWithOverflow(size, 4);
if (overflown == 1) return error.IndexSizeError;
return page._factory.create(ImageData{ return page._factory.create(ImageData{
._width = width, ._width = width,
._height = height, ._height = height,