Open Rexicon226 opened 7 months ago
Hi! I worry a bit about the implicit unsigned-int to int conversion at the end of stbiw__jpg_writeBits()
here:
*bitBufP = bitBuf;
If the high bit of bitCnt
is set and stb_image_write.h's compiled in C++ mode, then this was implementation-defined until C++20 (per bullet point 2 of https://en.cppreference.com/w/cpp/language/implicit_conversion#Integral_conversions) but worked OK anyways.
Maybe it's better to change the type of the bit buffer throughout stb_image_write.h to an unsigned int?
Since only the lower 24 bits of the bit buffer are used, I think this would work as well (at the expense of an additional mask):
*bitBufP = bitBuf & 0xFFFFFF;
Thank you!
The line:
https://github.com/nothings/stb/blob/ae721c50eaf761660b4f90cc590453cdb0c2acd0/stb_image_write.h#L1263
creates potential UB because
int
isn't guaranteed to be large enough. I think the solution isunsigned int
but if you know something better please suggest.You can see this easily yourself by just compiling a program like:
Compile with:
running it: