Skip to content

Add overflow protection to LZW buffer allocation#107

Open
uwezkhan wants to merge 1 commit intodloebl:mainfrom
uwezkhan:fix/lzw-allocation-overflow-guard
Open

Add overflow protection to LZW buffer allocation#107
uwezkhan wants to merge 1 commit intodloebl:mainfrom
uwezkhan:fix/lzw-allocation-overflow-guard

Conversation

@uwezkhan
Copy link
Copy Markdown
Contributor

@uwezkhan uwezkhan commented Mar 1, 2026

This patch adds explicit overflow guards to the LZW
buffer allocation in cgif_raw.c.

The allocation size is derived from:

numPixel + 2 + maxResets

Without guarding against additive and multiplicative
overflow, extreme dimension values could cause the
computed allocation size to wrap, leading to an
undersized heap allocation and potential out-of-bounds
writes during LZW encoding.

The patch introduces:

  • Additive overflow check using SIZE_MAX
  • Multiplicative overflow check before sizeof(uint16_t)
  • Preserves existing cleanup logic
  • Uses existing CGIF_EALLOC error handling

No public API changes.
No behavioral changes for valid inputs.
Negligible runtime impact.

Security impact:
Prevents potential heap buffer overflow in the LZW
encoding path due to allocation size wraparound.

@dloebl
Copy link
Copy Markdown
Owner

dloebl commented Mar 15, 2026

Hey @uwezkhan,
please read the contributing guidelines before opening any new pull requests or issues.

Missing test case (required): Bug fix PRs must include a test in tests/ that reproduces the issue on main (fails before the fix, passes after). No test was provided and no changes to tests/meson.build were made. See CONTRIBUTING.md:, section "Submitting Pull Requests", item 3.

AI tooling attribution: If any AI models or tooling were used to produce this PR, please disclose that per our contributing guidelines (CONTRIBUTING.md:, item 5).

@uwezkhan uwezkhan force-pushed the fix/lzw-allocation-overflow-guard branch from 4ff2400 to 07bf827 Compare April 12, 2026 20:14
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