lvandeve / lodepng

PNG encoder and decoder in C and C++.
zlib License
2.04k stars 420 forks source link

Fix undefined behaviour when casting an out of range value to an enum #95

Closed bobsayshilol closed 4 years ago

bobsayshilol commented 4 years ago

Without a fixed underlying type, the range of LodePNGColorType is [0, 8) since all the values can be represented by 3 bits. This means that the cast from an arbitrary unsigned char to a LodePNGColorType in lodepng_inspect() is not valid for values not in that range. To fix this we check that the colour type is valid before casting it to a LodePNGColorType so that all LodePNGColorType instances are valid.

lvandeve commented 4 years ago

Thanks for fixing, that issue indeed showed up with "-fsanitize=undefined" and is fixed with this

lvandeve commented 4 years ago

I did another change to this to allow representing the invalid values in the enum: commit 165dfcffddc4bb31d32b90edc56bfb971ead9390

This to be able to store invalid values of corrupt PNGs in this enum as well (but without having names for those values, but they can still be printf'd to diagnose corrupt PNGs this way)

bobsayshilol commented 4 years ago

Thanks! I considered doing it both ways, but picked the restricted values because I prefer to be explicit about what's accepted vs what's not, though it did make the code a little bit messier doing it that way. Both are valid, so no complaints here.