Skip to content

FreeImageIO: bound _MemoryWriteProc end position (Windows 32-bit long overflow) - #228

Merged
danoli3 merged 1 commit into
masterfrom
fix/memio-write-position-overflow
Oct 28, 2026
Merged

danoli3 merged 1 commit into
masterfrom
fix/memio-write-position-overflow

Conversation

@danoli3

@danoli3 danoli3 commented Oct 27, 2026

Copy link
Copy Markdown
Owner

Follow-up to #217. That PR made the size * count total overflow-safe, but the write growth loop still computes current_position + required_bytes in a long. On Windows long is 32 bits, so a seek to INT_MAX - 7 followed by a 16-byte write is signed overflow. The wrapped sum skips the growth loop, and memcpy then writes past the buffer.

Fix

_MemoryWriteProc now returns 0 when required_bytes > INT_MAX - current_position, and it checks this before it adds the two. _MemorySeekProc keeps current_position in [0, INT_MAX], so the subtraction itself cannot overflow. Writes whose end fits in an int behave as before.

Test

New MemWritePositionTest (ctest):

  • Seeks to INT_MAX - 7, writes 16 bytes, and expects the write to return 0 with the position unchanged.
  • Seeks back to 0, writes 16 bytes, and checks the contents through FreeImage_AcquireMemory.

The test can only fail before the fix on Windows jobs. On LP64 hosts the old code does not overflow: it grows the buffer to 2 GiB and then returns 0, so the test passes there with or without the fix.

Verification

  • Windows-width model: the function body with long replaced by int32_t, built with -fsanitize=signed-integer-overflow.
    • master: runtime error: signed integer overflow: 2147483640 + 16 cannot be represented in type 'int32_t'
    • this branch: returns 0, and the position stays at 2147483640.
  • macOS arm64, ASan+UBSan Debug build: all 12 ctest tests pass.

Should land before the next tag.

The growth loop adds current_position and required_bytes in a long,
which is 32 bits on Windows (LLP64). After a seek to INT_MAX - 7, a
16-byte write is signed overflow there: the wrapped sum skips the
growth loop and memcpy writes past the buffer. The #217 guard only
caps the size * count total, not this sum.

Refuse any write whose end the int stream positions cannot represent
before forming the sum. current_position is always in [0, INT_MAX]
via _MemorySeekProc, so INT_MAX - current_position cannot overflow.

MemWritePositionTest seeks to INT_MAX - 7, expects the 16-byte write
to return 0 without moving the position, then checks an ordinary
write still works. A Windows-width model of the function (long ->
int32_t, -fsanitize=signed-integer-overflow) reports
"2147483640 + 16 cannot be represented in type 'int32_t'" on master
and returns 0 with the fix.
@danoli3
danoli3 merged commit c210b8b into master Oct 28, 2026
76 of 77 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant