1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 20:47:45 +00:00

LibGfx: Prevent out of bounds access when scaling small Bitmaps

Since the color interpolation requires two pixels in the horizontal and
vertical direction to work, 1 pixel wide or high bitmaps would cause a
crash when scaling. Fix this by clamping the index into the valid range.

Fixes #16047.
This commit is contained in:
Darius Arnold 2022-11-16 11:38:51 +01:00 committed by Andreas Kling
parent 71d5dc510e
commit 884d8b14ac
3 changed files with 226 additions and 49 deletions

View file

@ -0,0 +1,128 @@
/*
* Copyright (c) 2022, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibGfx/Bitmap.h>
#include <LibTest/TestCase.h>
TEST_CASE(0001_bitmap_upscaling_width1_height1)
{
auto bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::BGRx8888, Gfx::IntSize { 1, 1 });
EXPECT_EQ(bitmap.is_error(), false);
bitmap.value()->fill(Gfx::Color::White);
auto scaledBitmap = bitmap.value()->scaled(5.5f, 5.5f);
EXPECT_EQ(scaledBitmap.is_error(), false);
EXPECT_EQ(scaledBitmap.value()->size(), Gfx::IntSize(6, 6));
for (auto x = 0; x < scaledBitmap.value()->width(); x++) {
for (auto y = 0; y < scaledBitmap.value()->height(); y++) {
EXPECT_EQ(scaledBitmap.value()->get_pixel(x, y), bitmap.value()->get_pixel(0, 0));
}
}
}
TEST_CASE(0002_bitmap_upscaling_width1)
{
auto bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::BGRx8888, Gfx::IntSize { 1, 10 });
EXPECT_EQ(bitmap.is_error(), false);
bitmap.value()->fill(Gfx::Color::White);
auto scaledBitmap = bitmap.value()->scaled(5.5f, 5.5f);
EXPECT_EQ(scaledBitmap.is_error(), false);
EXPECT_EQ(scaledBitmap.value()->size(), Gfx::IntSize(6, 55));
for (auto x = 0; x < scaledBitmap.value()->width(); x++) {
for (auto y = 0; y < scaledBitmap.value()->height(); y++) {
EXPECT_EQ(scaledBitmap.value()->get_pixel(x, y), bitmap.value()->get_pixel(0, 0));
}
}
}
TEST_CASE(0003_bitmap_upscaling_height1)
{
auto bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::BGRx8888, Gfx::IntSize { 10, 1 });
EXPECT_EQ(bitmap.is_error(), false);
bitmap.value()->fill(Gfx::Color::White);
auto scaledBitmap = bitmap.value()->scaled(5.5f, 5.5f);
EXPECT_EQ(scaledBitmap.is_error(), false);
EXPECT_EQ(scaledBitmap.value()->size(), Gfx::IntSize(55, 6));
for (auto x = 0; x < scaledBitmap.value()->width(); x++) {
for (auto y = 0; y < scaledBitmap.value()->height(); y++) {
EXPECT_EQ(scaledBitmap.value()->get_pixel(x, y), bitmap.value()->get_pixel(0, 0));
}
}
}
TEST_CASE(0004_bitmap_upscaling_keep_width)
{
auto bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::BGRx8888, Gfx::IntSize { 1, 10 });
EXPECT_EQ(bitmap.is_error(), false);
bitmap.value()->fill(Gfx::Color::White);
auto scaledBitmap = bitmap.value()->scaled(1.f, 5.5f);
EXPECT_EQ(scaledBitmap.is_error(), false);
EXPECT_EQ(scaledBitmap.value()->size(), Gfx::IntSize(1, 55));
for (auto x = 0; x < scaledBitmap.value()->width(); x++) {
for (auto y = 0; y < scaledBitmap.value()->height(); y++) {
EXPECT_EQ(scaledBitmap.value()->get_pixel(x, y), bitmap.value()->get_pixel(0, 0));
}
}
}
TEST_CASE(0005_bitmap_upscaling_keep_height)
{
auto bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::BGRx8888, Gfx::IntSize { 10, 1 });
EXPECT_EQ(bitmap.is_error(), false);
bitmap.value()->fill(Gfx::Color::White);
auto scaledBitmap = bitmap.value()->scaled(5.5f, 1.f);
EXPECT_EQ(scaledBitmap.is_error(), false);
EXPECT_EQ(scaledBitmap.value()->size(), Gfx::IntSize(55, 1));
for (auto x = 0; x < scaledBitmap.value()->width(); x++) {
for (auto y = 0; y < scaledBitmap.value()->height(); y++) {
EXPECT_EQ(scaledBitmap.value()->get_pixel(x, y), bitmap.value()->get_pixel(0, 0));
}
}
}
TEST_CASE(0006_bitmap_downscaling_width1_height1)
{
auto bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::BGRx8888, Gfx::IntSize { 10, 10 });
EXPECT_EQ(bitmap.is_error(), false);
bitmap.value()->fill(Gfx::Color::White);
auto scaledBitmap = bitmap.value()->scaled(0.099f, 0.099f);
EXPECT_EQ(scaledBitmap.is_error(), false);
EXPECT_EQ(scaledBitmap.value()->size(), Gfx::IntSize(1, 1));
for (auto x = 0; x < scaledBitmap.value()->width(); x++) {
for (auto y = 0; y < scaledBitmap.value()->height(); y++) {
EXPECT_EQ(scaledBitmap.value()->get_pixel(x, y), bitmap.value()->get_pixel(0, 0));
}
}
}
TEST_CASE(0007_bitmap_downscaling_width1)
{
auto bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::BGRx8888, Gfx::IntSize { 10, 10 });
EXPECT_EQ(bitmap.is_error(), false);
bitmap.value()->fill(Gfx::Color::White);
auto scaledBitmap = bitmap.value()->scaled(1.f, 0.099f);
EXPECT_EQ(scaledBitmap.is_error(), false);
EXPECT_EQ(scaledBitmap.value()->size(), Gfx::IntSize(10, 1));
for (auto x = 0; x < scaledBitmap.value()->width(); x++) {
for (auto y = 0; y < scaledBitmap.value()->height(); y++) {
EXPECT_EQ(scaledBitmap.value()->get_pixel(x, y), bitmap.value()->get_pixel(0, 0));
}
}
}
TEST_CASE(0008_bitmap_downscaling_height1)
{
auto bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::BGRx8888, Gfx::IntSize { 10, 10 });
EXPECT_EQ(bitmap.is_error(), false);
bitmap.value()->fill(Gfx::Color::White);
auto scaledBitmap = bitmap.value()->scaled(0.099f, 1.f);
EXPECT_EQ(scaledBitmap.is_error(), false);
EXPECT_EQ(scaledBitmap.value()->size(), Gfx::IntSize(1, 10));
for (auto x = 0; x < scaledBitmap.value()->width(); x++) {
for (auto y = 0; y < scaledBitmap.value()->height(); y++) {
EXPECT_EQ(scaledBitmap.value()->get_pixel(x, y), bitmap.value()->get_pixel(0, 0));
}
}
}