Use integer division in TiffBinaryArray::doCount#9288
Conversation
doCount() uses std::lround(double(size) / typeSize) which rounds to the nearest integer. When size is not a multiple of typeSize, this can round up and report more elements than the data holds. On re-read, the inflated count causes the parser to expect more data than exists. Use integer division instead, which truncates and is consistent with how readTiffEntry computes size from count and typeSize.
There was a problem hiding this comment.
Pull request overview
Fixes incorrect element-count computation for TiffBinaryArray when the underlying byte size is not an exact multiple of the element type size, preventing write/re-read failures (issue #9287).
Changes:
- Replace floating-point rounding (
std::lround) with integer division forTiffBinaryArray::doCount().
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| } | ||
|
|
||
| return std::lround(static_cast<double>(size()) / typeSize); | ||
| return size() / typeSize; |
There was a problem hiding this comment.
With std::lround removed, now appears unused in this translation unit. Consider dropping the include to avoid unnecessary header dependencies/build time.
| } | ||
|
|
||
| return std::lround(static_cast<double>(size()) / typeSize); | ||
| return size() / typeSize; |
There was a problem hiding this comment.
This change fixes a subtle write/re-read mismatch for malformed TIFF data. Please add a regression test that writes a TIFF with a TiffBinaryArray whose byte size isn’t a multiple of its element type size, then re-reads it to ensure it doesn’t fail (issue #9287).
| return size() / typeSize; | |
| return elements_.size(); |
|
I checked the code history and it seems that we've been doing round-to-nearest here for a very long time, but I can't see any reason why. The call to If the tests pass, then I think this change is ok. |
Fix #9287:
doCount() uses std::lround(double(size) / typeSize) which rounds to the nearest integer. When size is not a multiple of typeSize, this can round up and report more elements than the data holds. On re-read, the inflated count causes the parser to expect more data than exists.
Use integer division instead, which truncates and is consistent with how readTiffEntry computes size from count and typeSize.