Add fix for https://github.com/nothings/stb/issues/1860 by checking malloc multiplication before allocating#1862
Open
NBickford-NV wants to merge 3 commits into
Open
Conversation
…e it could run into a similar issue
Contributor
Author
|
(It looks like the fix in |
|
First patch requires adjusting restrictions on diff --git a/src/stb_image.h b/src/stb_image.h
index 99115a6..03c22e0 100644
--- a/src/stb_image.h
+++ b/src/stb_image.h
@@ -1110,7 +1110,7 @@ static int stbi__mad3sizes_valid(int a, int b, int c, int add)
}
// returns 1 if "a*b*c*d + add" has no negative terms/factors and doesn't overflow
-#if !defined(STBI_NO_LINEAR) || !defined(STBI_NO_HDR) || !defined(STBI_NO_PNM)
+#if !defined(STBI_NO_LINEAR) || !defined(STBI_NO_HDR) || !defined(STBI_NO_PNM) || !defined(STBI_NO_PNG) || !defined(STBI_NO_PSD)
static int stbi__mad4sizes_valid(int a, int b, int c, int d, int add)
{
return stbi__mul2sizes_valid(a, b) && stbi__mul2sizes_valid(a*b, c) &&
@@ -1133,7 +1133,7 @@ static void *stbi__malloc_mad3(int a, int b, int c, int add)
return stbi__malloc(a*b*c + add);
}
-#if !defined(STBI_NO_LINEAR) || !defined(STBI_NO_HDR) || !defined(STBI_NO_PNM)
+#if !defined(STBI_NO_LINEAR) || !defined(STBI_NO_HDR) || !defined(STBI_NO_PNM) || !defined(STBI_NO_PNG) || !defined(STBI_NO_PSD)
static void *stbi__malloc_mad4(int a, int b, int c, int d, int add)
{
if (!stbi__mad4sizes_valid(a, b, c, d, add)) return NULL; |
Contributor
Author
|
Thank you @sezero ! I've added that change to this pull request. |
NBickford-NV
added a commit
to nvpro-samples/stb
that referenced
this pull request
Dec 4, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This should fix #1860 .
Previously,
stbi__convert_format16didn't check if the multiplicationreq_comp * x * y * 2would overflow before callingstbi__malloc. Thankfully, the fix is a quick change to usestbi__malloc_mad4instead of a rawstbi__malloc. (stbi__convert_format8already checked this. The floating-point path checks this insidestbi__hdr_load.)I also added a safety check to code in
stbi__convert_8_to_16that looks like it could run into a similar issue. (We know thatreq_comp * x * yis valid here because we've been passed a valid buffer. That does imply that we could save a few operations by doing anstbi__malloc_mad2insidestbi__convert_format16, though.)Thanks!