Skip to content

Potential fix for code scanning alert no. 3235: Multiplication result converted to larger type#139

Closed
bniladridas wants to merge 1 commit into
mainfrom
alert-autofix-3235
Closed

Potential fix for code scanning alert no. 3235: Multiplication result converted to larger type#139
bniladridas wants to merge 1 commit into
mainfrom
alert-autofix-3235

Conversation

@bniladridas

Copy link
Copy Markdown
Member

Potential fix for https://github.com/bniladridas/hybrid-compute/security/code-scanning/3235

To fix this safely without changing behavior for valid inputs, ensure allocation-size multiplications are performed in a wider unsigned type (size_t) before multiplication, and reject impossible sizes when they exceed INT_MAX where the code still stores byte counts in int (out_size, delays_size).

Best fix in this region:

  • In include/stb_image.h, inside the shown GIF loading block (around lines 7729–7759), compute:
    • size_t alloc_size = (size_t)layers * (size_t)stride;
    • size_t delays_alloc = (size_t)layers * sizeof(int);
  • Guard with if (alloc_size > INT_MAX) / if (delays_alloc > INT_MAX) and return existing OOM/error path.
  • Pass these size_t values to allocation/reallocation macros.
  • Cast back to int only after bounds check when assigning to out_size / delays_size.

No new external dependencies are needed.

Suggested fixes powered by Copilot Autofix. Review carefully before merging.

… converted to larger type

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
@changeset-bot

changeset-bot Bot commented May 4, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: d319c3f

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@github-actions

github-actions Bot commented May 4, 2026

Copy link
Copy Markdown
Contributor

☑️ I checked the pre-commit hooks and there was nothing to fix at commit d319c3f.

@github-actions github-actions Bot added the core label May 4, 2026
bniladridas added a commit that referenced this pull request May 4, 2026
Combines all 15 automated code scanning alert fixes for multiplication
result converted to larger type. These changes span
`include/stb_image.h`, `include/stb_image_write.h`, and
`src/preprocess.c` with consistent application of `size_t` casts to
prevent integer overflow before conversion to larger types. Overflow
checks are added where allocation sizes are stored back into `int`
variables such as `out_size`.

> [!NOTE]
> Merging this pull request will automatically close #125, #126, #127,
#128, #129, #130, #131, #132, #133, #134, #135, #136, #137, #138, #139

---------

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
Co-authored-by: Niladri Das <bniladridas@users.noreply.github.com>
@bniladridas

Copy link
Copy Markdown
Member Author

Fixed in #140

@bniladridas bniladridas closed this May 4, 2026
@bniladridas bniladridas deleted the alert-autofix-3235 branch May 4, 2026 16:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant