Skip to content

Commit 5f5329a

Browse files
committed
Fix out-of-bounds image slices
1 parent 008920f commit 5f5329a

File tree

12 files changed

+37
-1
lines changed

12 files changed

+37
-1
lines changed

include/gfx/main.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ struct Options {
4040
uint16_t top;
4141
uint16_t width;
4242
uint16_t height;
43+
uint32_t right() const { return left + width * 8; }
44+
uint32_t bottom() const { return top + height * 8; }
4345
} inputSlice{0, 0, 0, 0}; // -L (margins in clockwise order, like CSS)
4446
std::array<uint16_t, 2> maxNbTiles{UINT16_MAX, 0}; // -N
4547
uint16_t nbPalettes = 8; // -n

src/gfx/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -863,7 +863,7 @@ int main(int argc, char *argv[]) {
863863
}
864864
fprintf(
865865
stderr,
866-
"\tInput image slice: %" PRIu32 "x%" PRIu32 " pixels starting at (%" PRId32 ", %" PRId32
866+
"\tInput image slice: %" PRIu16 "x%" PRIu16 " pixels starting at (%" PRIu16 ", %" PRIu16
867867
")\n",
868868
options.inputSlice.width,
869869
options.inputSlice.height,

src/gfx/process.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,30 @@ class Png {
226226
if (options.inputSlice.height == 0 && height % 8 != 0) {
227227
fatal("Image height (%" PRIu32 " pixels) is not a multiple of 8!", height);
228228
}
229+
if (options.inputSlice.right() > width || options.inputSlice.bottom() > height) {
230+
error(
231+
"Image slice ((%" PRIu16 ", %" PRIu16 ") to (%" PRIu32 ", %" PRIu32
232+
")) is outside the image bounds (%" PRIu32 "x%" PRIu32 ")!",
233+
options.inputSlice.left,
234+
options.inputSlice.top,
235+
options.inputSlice.right(),
236+
options.inputSlice.bottom(),
237+
width,
238+
height
239+
);
240+
if (options.inputSlice.width % 8 == 0 && options.inputSlice.height % 8 == 0) {
241+
fprintf(
242+
stderr,
243+
"note: Did you mean the slice \"%" PRIu32 ",%" PRIu32 ":%" PRId32 ",%" PRId32
244+
"\"? (width and height are in tiles, not pixels!)\n",
245+
options.inputSlice.left,
246+
options.inputSlice.top,
247+
options.inputSlice.width / 8,
248+
options.inputSlice.height / 8
249+
);
250+
}
251+
giveUp();
252+
}
229253

230254
pixels.resize(static_cast<size_t>(width) * static_cast<size_t>(height));
231255

test/gfx/bad_slice.err

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

test/gfx/bad_slice.flags

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-L 2,2:16,16

test/gfx/bad_slice.png

246 Bytes
Loading

test/gfx/oob_slice_wh.err

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
error: Image slice ((2, 2) to (34, 18)) is outside the image bounds (20x20)!
2+
Conversion aborted after 1 error

test/gfx/oob_slice_wh.flags

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-L 2,2:4,2

test/gfx/oob_slice_wh.png

246 Bytes
Loading

test/gfx/oob_slice_xy.err

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
error: Image slice ((2, 99) to (10, 107)) is outside the image bounds (20x20)!
2+
Conversion aborted after 1 error

0 commit comments

Comments
 (0)