Replace large stack allocations with dynamic allocations.

This commit is contained in:
Thatcher Ulrich 2016-03-02 15:31:07 -05:00
parent a83ab31335
commit 291ad22e84
2 changed files with 24 additions and 18 deletions

View File

@ -3408,10 +3408,13 @@ static stbi_uc *load_jpeg_image(stbi__jpeg *z, int *out_x, int *out_y, int *comp
static unsigned char *stbi__jpeg_load(stbi__context *s, int *x, int *y, int *comp, int req_comp) static unsigned char *stbi__jpeg_load(stbi__context *s, int *x, int *y, int *comp, int req_comp)
{ {
stbi__jpeg j; unsigned char* result;
j.s = s; stbi__jpeg* j = (stbi__jpeg*) stbi__malloc(sizeof(stbi__jpeg));
stbi__setup_jpeg(&j); j->s = s;
return load_jpeg_image(&j, x,y,comp,req_comp); stbi__setup_jpeg(j);
result = load_jpeg_image(j, x,y,comp,req_comp);
STBI_FREE(j);
return result;
} }
static int stbi__jpeg_test(stbi__context *s) static int stbi__jpeg_test(stbi__context *s)
@ -5649,13 +5652,15 @@ static int stbi__gif_header(stbi__context *s, stbi__gif *g, int *comp, int is_in
static int stbi__gif_info_raw(stbi__context *s, int *x, int *y, int *comp) static int stbi__gif_info_raw(stbi__context *s, int *x, int *y, int *comp)
{ {
stbi__gif g; stbi__gif* g = (stbi__gif*) stbi__malloc(sizeof(stbi__gif));
if (!stbi__gif_header(s, &g, comp, 1)) { if (!stbi__gif_header(s, g, comp, 1)) {
STBI_FREE(g);
stbi__rewind( s ); stbi__rewind( s );
return 0; return 0;
} }
if (x) *x = g.w; if (x) *x = g->w;
if (y) *y = g.h; if (y) *y = g->h;
STBI_FREE(g);
return 1; return 1;
} }
@ -5908,20 +5913,20 @@ static stbi_uc *stbi__gif_load_next(stbi__context *s, stbi__gif *g, int *comp, i
static stbi_uc *stbi__gif_load(stbi__context *s, int *x, int *y, int *comp, int req_comp) static stbi_uc *stbi__gif_load(stbi__context *s, int *x, int *y, int *comp, int req_comp)
{ {
stbi_uc *u = 0; stbi_uc *u = 0;
stbi__gif g; stbi__gif* g = (stbi__gif*) stbi__malloc(sizeof(stbi__gif));
memset(&g, 0, sizeof(g)); memset(g, 0, sizeof(*g));
u = stbi__gif_load_next(s, &g, comp, req_comp); u = stbi__gif_load_next(s, g, comp, req_comp);
if (u == (stbi_uc *) s) u = 0; // end of animated gif marker if (u == (stbi_uc *) s) u = 0; // end of animated gif marker
if (u) { if (u) {
*x = g.w; *x = g->w;
*y = g.h; *y = g->h;
if (req_comp && req_comp != 4) if (req_comp && req_comp != 4)
u = stbi__convert_format(u, 4, req_comp, g.w, g.h); u = stbi__convert_format(u, 4, req_comp, g->w, g->h);
} }
else if (g.out) else if (g->out)
STBI_FREE(g.out); STBI_FREE(g->out);
STBI_FREE(g);
return u; return u;
} }

View File

@ -736,7 +736,7 @@ unsigned char * stbi_zlib_compress(unsigned char *data, int data_len, int *out_l
unsigned int bitbuf=0; unsigned int bitbuf=0;
int i,j, bitcount=0; int i,j, bitcount=0;
unsigned char *out = NULL; unsigned char *out = NULL;
unsigned char **hash_table[stbiw__ZHASH]; // 64KB on the stack! unsigned char ***hash_table = (unsigned char***) STBIW_MALLOC(stbiw__ZHASH * sizeof(char**));
if (quality < 5) quality = 5; if (quality < 5) quality = 5;
stbiw__sbpush(out, 0x78); // DEFLATE 32K window stbiw__sbpush(out, 0x78); // DEFLATE 32K window
@ -808,6 +808,7 @@ unsigned char * stbi_zlib_compress(unsigned char *data, int data_len, int *out_l
for (i=0; i < stbiw__ZHASH; ++i) for (i=0; i < stbiw__ZHASH; ++i)
(void) stbiw__sbfree(hash_table[i]); (void) stbiw__sbfree(hash_table[i]);
STBIW_FREE(hash_table);
{ {
// compute adler32 on input // compute adler32 on input