Skip to content

fix: prevent integer overflow in 16-bit image conversions (#1936)#1940

Open
im-lunex wants to merge 2 commits into
nothings:masterfrom
im-lunex:fix-#1936
Open

fix: prevent integer overflow in 16-bit image conversions (#1936)#1940
im-lunex wants to merge 2 commits into
nothings:masterfrom
im-lunex:fix-#1936

Conversation

@im-lunex

@im-lunex im-lunex commented Apr 23, 2026

Copy link
Copy Markdown

In stbi__convert_format16, stbi__convert_16_to_8, and stbi__convert_8_to_16,
the size calculation w * h * channels * 2 can overflow a signed 32-bit int,
leading to a buffer overflow. This fix adds a call to stbi__mad3sizes_valid
before each allocation to reject oversized images early.

Tested with:

  • AddressSanitizer and UBSan (no runtime errors)
  • Normal PNG loading (still works)
  • Deliberate overflow dimensions (correctly returns NULL)

fixes #1936

im-lunex added 2 commits April 23, 2026 21:22
…othings#1936)): Prevent integer overflows when processing large 16-bit PNG images by

validating allocation sizes with stbi__mad3sizes_valid before calling
malloc in stbi__convert_format16, stbi__convert_16_to_8, and
stbi__convert_8_to_16

add size-validations in stb_image.h

fixes nothings#1936
…othings#1936)): Prevent integer overflows when processing large 16-bit PNG images by

validating allocation sizes with stbi__mad3sizes_valid before calling
malloc in stbi__convert_format16, stbi__convert_16_to_8, and
stbi__convert_8_to_16

Fixes nothings#1936
@im-lunex
im-lunex changed the base branch from master to dev April 26, 2026 05:29
@im-lunex im-lunex closed this Apr 26, 2026
@im-lunex im-lunex reopened this Apr 26, 2026
@im-lunex
im-lunex changed the base branch from dev to master April 26, 2026 05:33
@im-lunex im-lunex closed this Apr 26, 2026
@im-lunex im-lunex reopened this Apr 26, 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.

stb_image: integer overflow in 3 pixel-format conversion allocations

1 participant