Skip to content

LZW_GenerateStream() allocated the LZW code buffer using:#100

Closed
rootvector2 wants to merge 1 commit intodloebl:mainfrom
rootvector2:lzw-allocation-overflow
Closed

LZW_GenerateStream() allocated the LZW code buffer using:#100
rootvector2 wants to merge 1 commit intodloebl:mainfrom
rootvector2:lzw-allocation-overflow

Conversation

@rootvector2
Copy link
Copy Markdown
Contributor

sizeof(uint16_t) * (numPixel + 2 + maxResets)

The size expression was computed in 32-bit arithmetic, allowing unsigned overflow when numPixel is large. This could result in an undersized allocation and subsequent out-of-bounds writes to pLZWData during LZW code emission.

Fix by:

  • Performing size calculation in size_t
  • Adding explicit overflow checks for addition and multiplication
  • Recording allocated capacity
  • Adding bounds checks before all pLZWData writes

This change does not alter encoder behavior for valid inputs and adds negligible overhead.

    sizeof(uint16_t) * (numPixel + 2 + maxResets)

The size expression was computed in 32-bit arithmetic, allowing
unsigned overflow when numPixel is large. This could result in an
undersized allocation and subsequent out-of-bounds writes to
pLZWData during LZW code emission.

Fix by:

- Performing size calculation in size_t
- Adding explicit overflow checks for addition and multiplication
- Recording allocated capacity
- Adding bounds checks before all pLZWData writes

This change does not alter encoder behavior for valid inputs and
adds negligible overhead.
@dloebl
Copy link
Copy Markdown
Owner

dloebl commented Feb 21, 2026

Thanks! There is indeed a small integer overflow introduced by #84. Let's keep it simple and cast to a size_t for now. I opened a pull request here: #103

As a side note, this is only affecting very large single frame GIFs (65.535 x 65.535) with tiny color palettes.

@dloebl
Copy link
Copy Markdown
Owner

dloebl commented Feb 21, 2026

Resolved with #103

@dloebl dloebl closed this Feb 22, 2026
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.

2 participants