Fix out-of-bounds image slices

This commit is contained in:
Rangi42
2025-07-01 18:06:03 -04:00
committed by Eldred Habert
parent 008920f533
commit 965288de38
12 changed files with 37 additions and 1 deletions

View File

@@ -40,6 +40,8 @@ struct Options {
uint16_t top; uint16_t top;
uint16_t width; uint16_t width;
uint16_t height; uint16_t height;
uint32_t right() const { return left + width * 8; }
uint32_t bottom() const { return top + height * 8; }
} inputSlice{0, 0, 0, 0}; // -L (margins in clockwise order, like CSS) } inputSlice{0, 0, 0, 0}; // -L (margins in clockwise order, like CSS)
std::array<uint16_t, 2> maxNbTiles{UINT16_MAX, 0}; // -N std::array<uint16_t, 2> maxNbTiles{UINT16_MAX, 0}; // -N
uint16_t nbPalettes = 8; // -n uint16_t nbPalettes = 8; // -n

View File

@@ -863,7 +863,7 @@ int main(int argc, char *argv[]) {
} }
fprintf( fprintf(
stderr, stderr,
"\tInput image slice: %" PRIu32 "x%" PRIu32 " pixels starting at (%" PRId32 ", %" PRId32 "\tInput image slice: %" PRIu16 "x%" PRIu16 " pixels starting at (%" PRIu16 ", %" PRIu16
")\n", ")\n",
options.inputSlice.width, options.inputSlice.width,
options.inputSlice.height, options.inputSlice.height,

View File

@@ -226,6 +226,30 @@ public:
if (options.inputSlice.height == 0 && height % 8 != 0) { if (options.inputSlice.height == 0 && height % 8 != 0) {
fatal("Image height (%" PRIu32 " pixels) is not a multiple of 8!", height); fatal("Image height (%" PRIu32 " pixels) is not a multiple of 8!", height);
} }
if (options.inputSlice.right() > width || options.inputSlice.bottom() > height) {
error(
"Image slice ((%" PRIu16 ", %" PRIu16 ") to (%" PRIu32 ", %" PRIu32
")) is outside the image bounds (%" PRIu32 "x%" PRIu32 ")!",
options.inputSlice.left,
options.inputSlice.top,
options.inputSlice.right(),
options.inputSlice.bottom(),
width,
height
);
if (options.inputSlice.width % 8 == 0 && options.inputSlice.height % 8 == 0) {
fprintf(
stderr,
"note: Did you mean the slice \"%" PRIu32 ",%" PRIu32 ":%" PRId32 ",%" PRId32
"\"? (width and height are in tiles, not pixels!)\n",
options.inputSlice.left,
options.inputSlice.top,
options.inputSlice.width / 8,
options.inputSlice.height / 8
);
}
giveUp();
}
pixels.resize(static_cast<size_t>(width) * static_cast<size_t>(height)); pixels.resize(static_cast<size_t>(width) * static_cast<size_t>(height));

3
test/gfx/bad_slice.err Normal file
View File

@@ -0,0 +1,3 @@
error: Image slice ((2, 2) to (130, 130)) is outside the image bounds (20x20)!
note: Did you mean the slice "2,2:2,2"? (width and height are in tiles, not pixels!)
Conversion aborted after 1 error

1
test/gfx/bad_slice.flags Normal file
View File

@@ -0,0 +1 @@
-L 2,2:16,16

BIN
test/gfx/bad_slice.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 246 B

View File

@@ -0,0 +1,2 @@
error: Image slice ((2, 2) to (34, 18)) is outside the image bounds (20x20)!
Conversion aborted after 1 error

View File

@@ -0,0 +1 @@
-L 2,2:4,2

BIN
test/gfx/oob_slice_wh.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 246 B

View File

@@ -0,0 +1,2 @@
error: Image slice ((2, 99) to (10, 107)) is outside the image bounds (20x20)!
Conversion aborted after 1 error

View File

@@ -0,0 +1 @@
-L 2,99:1,1

BIN
test/gfx/oob_slice_xy.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 246 B