nothings / stb

stb single-file public domain libraries for C/C++
https://twitter.com/nothings
Other
25.77k stars 7.66k forks source link

[stb_image] image too large check is wrong #1659

Open MWP opened 4 days ago

MWP commented 4 days ago

stb_image being used on a STM32 ARM Cortex-M7 micro controller. Firmware built using gcc.

https://github.com/nothings/stb/blob/013ac3beddff3dbffafd5177e7972067cd2b5083/stb_image.h#L5121

stb_image.h line 5121 checking size of a PNG image returns with the error "Image too large to decode", when it should not. In my test case, the PNG is 8bpp, RGBA, 300 x 100 pixels. I have step-debugged the code, img_x, img_y, img_n are correct (300, 100, 4 respectively).

NBickford-NV commented 4 days ago

Hi! I'm trying to think about how this can happen -- are ints 16 bits on this platform, maybe? (1 << 30) / s->img_x / s->img_n here should evaluate to (1 << 30) / 300 / 4 == 894784, but if (1 << 30)'s truncated to 16 bits, then it would evaluate to 0. (The idea behind this check is that it tests whether x y n > 2^30 without risking integer overflow.)

Thanks!