From 658e4423772cef1b552432547691e08387002dd9 Mon Sep 17 00:00:00 2001
From: Ozkan Sezer <[EMAIL REDACTED]>
Date: Sun, 30 Nov 2025 23:11:00 +0300
Subject: [PATCH] stb_image.h: apply patches from mainstream PRs 1839, 1862 and
1863.
Patches authored by @NBickford-NV to fix mainstream issues 1838, 1860,
1861.
---
src/stb_image.h | 31 +++++++++++++++----------------
1 file changed, 15 insertions(+), 16 deletions(-)
diff --git a/src/stb_image.h b/src/stb_image.h
index 99115a67..03c22e02 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;
@@ -1287,7 +1287,7 @@ static stbi__uint16 *stbi__convert_8_to_16(stbi_uc *orig, int w, int h, int chan
int img_len = w * h * channels;
stbi__uint16 *enlarged;
- enlarged = (stbi__uint16 *) stbi__malloc(img_len*2);
+ enlarged = (stbi__uint16 *) stbi__malloc_mad2(img_len, 2, 0);
if (enlarged == NULL) return (stbi__uint16 *) stbi__errpuc("outofmem", "Out of memory");
for (i = 0; i < img_len; ++i)
@@ -1956,7 +1956,7 @@ static stbi__uint16 *stbi__convert_format16(stbi__uint16 *data, int img_n, int r
if (req_comp == img_n) return data;
STBI_ASSERT(req_comp >= 1 && req_comp <= 4);
- good = (stbi__uint16 *) stbi__malloc(req_comp * x * y * 2);
+ good = (stbi__uint16 *) stbi__malloc_mad4(req_comp, x, y, 2, 0);
if (good == NULL) {
STBI_FREE(data);
return (stbi__uint16 *) stbi__errpuc("outofmem", "Out of memory");
@@ -5227,10 +5227,10 @@ static void stbi__de_iphone(stbi__png *z)
static int stbi__parse_png_file(stbi__png *z, int scan, int req_comp, unsigned int *palette_buffer, int palette_buffer_len)
{
- stbi_uc _palette[1024], pal_img_n=0;
+ stbi_uc _palette[1024]={0}, pal_img_n=0;
stbi_uc *palette = _palette;
stbi_uc has_trans=0, tc[3]={0};
- stbi__uint16 tc16[3];
+ stbi__uint16 tc16[3]={0};
stbi__uint32 ioff=0, idata_limit=0, i, pal_len=0;
int first=1,k,interlace=0, color=0, is_iphone=0;
stbi__context *s = z->s;
@@ -7147,7 +7147,10 @@ static void *stbi__load_gif_main_outofmem(stbi__gif *g, stbi_uc *out, int **dela
STBI_FREE(g->background);
if (out) STBI_FREE(out);
- if (delays && *delays) STBI_FREE(*delays);
+ if (delays && *delays) {
+ STBI_FREE(*delays);
+ *delays = NULL;
+ }
return stbi__errpuc("outofmem", "Out of memory");
}
@@ -7182,19 +7185,15 @@ static void *stbi__load_gif_main(stbi__context *s, int **delays, int *x, int *y,
stride = g.w * g.h * 4;
if (out) {
- void *tmp = (stbi_uc*) STBI_REALLOC_SIZED( out, out_size, layers * stride );
- if (!tmp)
+ out = (stbi_uc*) STBI_REALLOC_SIZED( out, out_size, layers * stride );
+ if (!out)
return stbi__load_gif_main_outofmem(&g, out, delays);
- else {
- out = (stbi_uc*) tmp;
- out_size = layers * stride;
- }
+ out_size = layers * stride;
if (delays) {
- int *new_delays = (int*) STBI_REALLOC_SIZED( *delays, delays_size, sizeof(int) * layers );
- if (!new_delays)
+ *delays = (int*) STBI_REALLOC_SIZED( *delays, delays_size, sizeof(int) * layers );
+ if (!*delays)
return stbi__load_gif_main_outofmem(&g, out, delays);
- *delays = new_delays;
delays_size = layers * sizeof(int);
}
} else {