lvandeve / lodepng

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

Loop conditions #80

Closed AntonyCorbett closed 5 years ago

AntonyCorbett commented 5 years ago

The loop conditions seem odd in this utility function.

 unsigned long readBitsFromReversedStream(size_t& bitp, const unsigned char* bits, unsigned long nbits)
  {
    unsigned long result = 0;
    for(size_t i = nbits - 1; i < nbits; i--) result += ((readBitFromReversedStream(bitp, bits)) << i);
    return result;
  }
lvandeve commented 5 years ago

Thanks for the report!

True indeed, it looks odd, but relies on the unsigned integer overflow to wrap around to a large positive integer, which is why it was guaranteed to work

This may be from an older version though, it's no longer like that in here in this function:

https://github.com/lvandeve/lodepng/blob/master/lodepng.cpp

So should be in order now :) Closing this issue