Skip to content

Commit 658e442

Browse files
committed
stb_image.h: apply patches from mainstream PRs 1839, 1862 and 1863.
Patches authored by @NBickford-NV to fix mainstream issues 1838, 1860, 1861.
1 parent 9c29d1e commit 658e442

1 file changed

Lines changed: 15 additions & 16 deletions

File tree

src/stb_image.h

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1110,7 +1110,7 @@ static int stbi__mad3sizes_valid(int a, int b, int c, int add)
11101110
}
11111111

11121112
// returns 1 if "a*b*c*d + add" has no negative terms/factors and doesn't overflow
1113-
#if !defined(STBI_NO_LINEAR) || !defined(STBI_NO_HDR) || !defined(STBI_NO_PNM)
1113+
#if !defined(STBI_NO_LINEAR) || !defined(STBI_NO_HDR) || !defined(STBI_NO_PNM) || !defined(STBI_NO_PNG) || !defined(STBI_NO_PSD)
11141114
static int stbi__mad4sizes_valid(int a, int b, int c, int d, int add)
11151115
{
11161116
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)
11331133
return stbi__malloc(a*b*c + add);
11341134
}
11351135

1136-
#if !defined(STBI_NO_LINEAR) || !defined(STBI_NO_HDR) || !defined(STBI_NO_PNM)
1136+
#if !defined(STBI_NO_LINEAR) || !defined(STBI_NO_HDR) || !defined(STBI_NO_PNM) || !defined(STBI_NO_PNG) || !defined(STBI_NO_PSD)
11371137
static void *stbi__malloc_mad4(int a, int b, int c, int d, int add)
11381138
{
11391139
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
12871287
int img_len = w * h * channels;
12881288
stbi__uint16 *enlarged;
12891289

1290-
enlarged = (stbi__uint16 *) stbi__malloc(img_len*2);
1290+
enlarged = (stbi__uint16 *) stbi__malloc_mad2(img_len, 2, 0);
12911291
if (enlarged == NULL) return (stbi__uint16 *) stbi__errpuc("outofmem", "Out of memory");
12921292

12931293
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
19561956
if (req_comp == img_n) return data;
19571957
STBI_ASSERT(req_comp >= 1 && req_comp <= 4);
19581958

1959-
good = (stbi__uint16 *) stbi__malloc(req_comp * x * y * 2);
1959+
good = (stbi__uint16 *) stbi__malloc_mad4(req_comp, x, y, 2, 0);
19601960
if (good == NULL) {
19611961
STBI_FREE(data);
19621962
return (stbi__uint16 *) stbi__errpuc("outofmem", "Out of memory");
@@ -5227,10 +5227,10 @@ static void stbi__de_iphone(stbi__png *z)
52275227

52285228
static int stbi__parse_png_file(stbi__png *z, int scan, int req_comp, unsigned int *palette_buffer, int palette_buffer_len)
52295229
{
5230-
stbi_uc _palette[1024], pal_img_n=0;
5230+
stbi_uc _palette[1024]={0}, pal_img_n=0;
52315231
stbi_uc *palette = _palette;
52325232
stbi_uc has_trans=0, tc[3]={0};
5233-
stbi__uint16 tc16[3];
5233+
stbi__uint16 tc16[3]={0};
52345234
stbi__uint32 ioff=0, idata_limit=0, i, pal_len=0;
52355235
int first=1,k,interlace=0, color=0, is_iphone=0;
52365236
stbi__context *s = z->s;
@@ -7147,7 +7147,10 @@ static void *stbi__load_gif_main_outofmem(stbi__gif *g, stbi_uc *out, int **dela
71477147
STBI_FREE(g->background);
71487148

71497149
if (out) STBI_FREE(out);
7150-
if (delays && *delays) STBI_FREE(*delays);
7150+
if (delays && *delays) {
7151+
STBI_FREE(*delays);
7152+
*delays = NULL;
7153+
}
71517154
return stbi__errpuc("outofmem", "Out of memory");
71527155
}
71537156

@@ -7182,19 +7185,15 @@ static void *stbi__load_gif_main(stbi__context *s, int **delays, int *x, int *y,
71827185
stride = g.w * g.h * 4;
71837186

71847187
if (out) {
7185-
void *tmp = (stbi_uc*) STBI_REALLOC_SIZED( out, out_size, layers * stride );
7186-
if (!tmp)
7188+
out = (stbi_uc*) STBI_REALLOC_SIZED( out, out_size, layers * stride );
7189+
if (!out)
71877190
return stbi__load_gif_main_outofmem(&g, out, delays);
7188-
else {
7189-
out = (stbi_uc*) tmp;
7190-
out_size = layers * stride;
7191-
}
7191+
out_size = layers * stride;
71927192

71937193
if (delays) {
7194-
int *new_delays = (int*) STBI_REALLOC_SIZED( *delays, delays_size, sizeof(int) * layers );
7195-
if (!new_delays)
7194+
*delays = (int*) STBI_REALLOC_SIZED( *delays, delays_size, sizeof(int) * layers );
7195+
if (!*delays)
71967196
return stbi__load_gif_main_outofmem(&g, out, delays);
7197-
*delays = new_delays;
71987197
delays_size = layers * sizeof(int);
71997198
}
72007199
} else {

0 commit comments

Comments
 (0)