Skip to content

TiffBinaryArray::doCount() uses floating-point rounding instead of integer division #9287

@MarkLee131

Description

@MarkLee131

Describe the bug

In src/tiffcomposite_int.cpp:798, the element count of a TiffBinaryArray is computed using floating-point arithmetic:

return std::lround(static_cast<double>(size()) / typeSize);

When size() is not an exact multiple of typeSize, this rounds to the nearest integer instead of truncating. For example, when size() = 7 and typeSize = 2, the result is lround(3.5) = 4. Integer division would give 7 / 2 = 3.

The count of 4 is written to the IFD entry. On re-read, the parser computes the expected data size as 4 * 2 = 8, which exceeds the actual data size of 7. This causes the re-read to fail because the declared count implies more data than exists.

To Reproduce

This triggers when a binary array's byte size is not an exact multiple of the element type size. In practice this is rare because well-formed TIFF data has matching sizes. A test case can be constructed by creating a binary array with a size that is not evenly divisible by the type size, writing it, and re-reading the result.

Observed on main branch, current HEAD.

Expected behavior

The count should be computed with integer division (size() / typeSize) to match the behavior of readTiffEntry() in src/tiffvisitor_int.cpp, which uses integer arithmetic for size = typeSize * count.

Desktop (please complete the following information):

  • OS and version: macOS (Darwin 25.3.0, arm64)
  • Exiv2 version and source: main branch, built from source
  • Compiler and version: Clang 22.1.1 (homebrew llvm)
  • Compilation mode and/or compiler flags: Debug

Additional context

The fix is to replace the floating-point computation with integer division:

return size() / typeSize;

I can submit a PR if helpful.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions