Added the old size as argument to the STBI_REALLOC() and STBIW_REALLOC() macros

This commit is contained in:
Romain Bailly 2016-01-14 10:34:30 +01:00
parent 1ec8a8c4a4
commit 7e741ffc1e
2 changed files with 12 additions and 11 deletions

View File

@ -639,9 +639,9 @@ typedef unsigned char validate_uint32[sizeof(stbi__uint32)==4 ? 1 : -1];
#endif #endif
#ifndef STBI_MALLOC #ifndef STBI_MALLOC
#define STBI_MALLOC(sz) malloc(sz) #define STBI_MALLOC(sz) malloc(sz)
#define STBI_REALLOC(p,sz) realloc(p,sz) #define STBI_REALLOC(p,oldsz,newsz) realloc(p,newsz)
#define STBI_FREE(p) free(p) #define STBI_FREE(p) free(p)
#endif #endif
// x86/x64 detection // x86/x64 detection
@ -3616,14 +3616,14 @@ stbi_inline static int stbi__zhuffman_decode(stbi__zbuf *a, stbi__zhuffman *z)
static int stbi__zexpand(stbi__zbuf *z, char *zout, int n) // need to make room for n bytes static int stbi__zexpand(stbi__zbuf *z, char *zout, int n) // need to make room for n bytes
{ {
char *q; char *q;
int cur, limit; int cur, limit, old_limit;
z->zout = zout; z->zout = zout;
if (!z->z_expandable) return stbi__err("output buffer limit","Corrupt PNG"); if (!z->z_expandable) return stbi__err("output buffer limit","Corrupt PNG");
cur = (int) (z->zout - z->zout_start); cur = (int) (z->zout - z->zout_start);
limit = (int) (z->zout_end - z->zout_start); limit = old_limit = (int) (z->zout_end - z->zout_start);
while (cur + n > limit) while (cur + n > limit)
limit *= 2; limit *= 2;
q = (char *) STBI_REALLOC(z->zout_start, limit); q = (char *) STBI_REALLOC(z->zout_start, old_limit, limit);
if (q == NULL) return stbi__err("outofmem", "Out of memory"); if (q == NULL) return stbi__err("outofmem", "Out of memory");
z->zout_start = q; z->zout_start = q;
z->zout = q + cur; z->zout = q + cur;
@ -4408,11 +4408,12 @@ static int stbi__parse_png_file(stbi__png *z, int scan, int req_comp)
if (scan == STBI__SCAN_header) { s->img_n = pal_img_n; return 1; } if (scan == STBI__SCAN_header) { s->img_n = pal_img_n; return 1; }
if ((int)(ioff + c.length) < (int)ioff) return 0; if ((int)(ioff + c.length) < (int)ioff) return 0;
if (ioff + c.length > idata_limit) { if (ioff + c.length > idata_limit) {
stbi__uint32 idata_limit_old = idata_limit;
stbi_uc *p; stbi_uc *p;
if (idata_limit == 0) idata_limit = c.length > 4096 ? c.length : 4096; if (idata_limit == 0) idata_limit = c.length > 4096 ? c.length : 4096;
while (ioff + c.length > idata_limit) while (ioff + c.length > idata_limit)
idata_limit *= 2; idata_limit *= 2;
p = (stbi_uc *) STBI_REALLOC(z->idata, idata_limit); if (p == NULL) return stbi__err("outofmem", "Out of memory"); p = (stbi_uc *) STBI_REALLOC(z->idata, idata_limit_old, idata_limit); if (p == NULL) return stbi__err("outofmem", "Out of memory");
z->idata = p; z->idata = p;
} }
if (!stbi__getn(s, z->idata+ioff,c.length)) return stbi__err("outofdata","Corrupt PNG"); if (!stbi__getn(s, z->idata+ioff,c.length)) return stbi__err("outofdata","Corrupt PNG");

View File

@ -166,9 +166,9 @@ STBIWDEF int stbi_write_hdr_to_func(stbi_write_func *func, void *context, int w,
#endif #endif
#ifndef STBIW_MALLOC #ifndef STBIW_MALLOC
#define STBIW_MALLOC(sz) malloc(sz) #define STBIW_MALLOC(sz) malloc(sz)
#define STBIW_REALLOC(p,sz) realloc(p,sz) #define STBIW_REALLOC(p,oldsz,newsz) realloc(p,newsz)
#define STBIW_FREE(p) free(p) #define STBIW_FREE(p) free(p)
#endif #endif
#ifndef STBIW_MEMMOVE #ifndef STBIW_MEMMOVE
#define STBIW_MEMMOVE(a,b,sz) memmove(a,b,sz) #define STBIW_MEMMOVE(a,b,sz) memmove(a,b,sz)
@ -647,7 +647,7 @@ int stbi_write_hdr(char const *filename, int x, int y, int comp, const float *da
static void *stbiw__sbgrowf(void **arr, int increment, int itemsize) static void *stbiw__sbgrowf(void **arr, int increment, int itemsize)
{ {
int m = *arr ? 2*stbiw__sbm(*arr)+increment : increment+1; int m = *arr ? 2*stbiw__sbm(*arr)+increment : increment+1;
void *p = STBIW_REALLOC(*arr ? stbiw__sbraw(*arr) : 0, itemsize * m + sizeof(int)*2); void *p = STBIW_REALLOC(*arr ? stbiw__sbraw(*arr) : 0, *arr ? (stbiw__sbm(*arr)*itemsize + sizeof(int)*2) : 0, itemsize * m + sizeof(int)*2);
STBIW_ASSERT(p); STBIW_ASSERT(p);
if (p) { if (p) {
if (!*arr) ((int *) p)[1] = 0; if (!*arr) ((int *) p)[1] = 0;