Skip to content

Commit 181b502

Browse files
madebricculus
authored andcommitted
Improve error message when loading BMP with wrong bit depth
> When loading a BMP with a 24-bit depth, the error "Incomplete or corrupt .BMP file" was shown. By testing the bit depth first, a better message is shown.
1 parent 8e254d0 commit 181b502

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

dirksimple.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -270,20 +270,20 @@ static uint8_t *loadbmp_from_memory(const char *fname, const uint8_t *buf, int b
270270

271271
const uint32_t bmpwidth = readui32le(&ptr);
272272
const uint32_t bmpheight = readui32le(&ptr);
273+
274+
ptr += 2; // skip planes
275+
const uint16_t bitcount = readui16le(&ptr);
276+
273277
if ((bmpwidth > 1024) || (bmpheight > 1024)) {
274278
return invalid_media(fname, "Image is too big"); // this is just an arbitrary limit.
275279
} else if ((bmpwidth == 0) || (bmpheight == 0)) {
276280
return invalid_media(fname, "Image is zero pixels in size");
281+
} else if (bitcount != 32) {
282+
return invalid_media(fname, "Only 32bpp .BMP files supported");
277283
} else if ((offset_bits + (bmpwidth * bmpheight * 4)) > buflen) {
278284
return invalid_media(fname, "Incomplete or corrupt .BMP file");
279285
}
280286

281-
ptr += 2; // skip planes
282-
const uint16_t bitcount = readui16le(&ptr);
283-
if (bitcount != 32) {
284-
return invalid_media(fname, "Only 32bpp .BMP files supported");
285-
}
286-
287287
const uint32_t compression = readui32le(&ptr);
288288
if ((compression != 0) && (compression != 3)) {
289289
return invalid_media(fname, "Only uncompressed RGB .BMP files supported");

0 commit comments

Comments
 (0)