lvandeve / lodepng

PNG encoder and decoder in C and C++.
zlib License
2.06k stars 421 forks source link

Guard some truncating casts with & 0xFF #40

Open stinos opened 8 years ago

stinos commented 8 years ago

The result of adding two unsigned chars is an int, so assigning this again to unsigned char results in a narrowing cast. Afaik this is well-defined by the standard but code compiled with cl with all runtime checks enabled will trigger a breakpoint when e.g. scanline[i] + recon[i - bytewidth] yields a number > 255. Fix this, and make it clear what's happening, by and'ing with 0xFF (which should be discarded by any sane optimizer anyway).

cosinekitty commented 8 years ago

Does an explicit cast to (unsigned char) also prevent the runtime breakpoint? That would seem clearer and would definitely avoid any unnecessary bitwise AND instructions.

stinos commented 8 years ago

No that doesn't prevent it (the truncation is still there, whether the cast is implicit or explicit). If you're concerned about casts not being optimized away I could make e.g. a function or macro narrow_cast which for msvc does the AND but for other toolsets is a no-op.