Replace hard coded array with malloc() to prevent stack overflow.

This was an issue an Windows with it's small stack. It didn't trigger on
Linux. While at it make the code a little bit more robust by allocating
exactly the amount of data we need and not some arbitrary guess.
This commit is contained in:
Yamagi 2021-10-31 17:08:30 +01:00
parent 3873c76e12
commit 8dde5b8539
2 changed files with 14 additions and 26 deletions

View File

@ -785,16 +785,8 @@ R_Upload32(unsigned *data, int width, int height, qboolean mipmap)
qboolean qboolean
R_Upload8(byte *data, int width, int height, qboolean mipmap, qboolean is_sky) R_Upload8(byte *data, int width, int height, qboolean mipmap, qboolean is_sky)
{ {
unsigned trans[1024 * 1024]; int s = width * height;
int i, s; unsigned *trans = malloc(s * sizeof(unsigned));
int p;
s = width * height;
if (s > sizeof(trans) / 4)
{
ri.Sys_Error(ERR_DROP, "%s: too large", __func__);
}
if (gl_config.palettedtexture && is_sky) if (gl_config.palettedtexture && is_sky)
{ {
@ -809,9 +801,9 @@ R_Upload8(byte *data, int width, int height, qboolean mipmap, qboolean is_sky)
} }
else else
{ {
for (i = 0; i < s; i++) for (int i = 0; i < s; i++)
{ {
p = data[i]; int p = data[i];
trans[i] = d_8to24table[p]; trans[i] = d_8to24table[p];
/* transparent, so scan around for /* transparent, so scan around for
@ -846,7 +838,9 @@ R_Upload8(byte *data, int width, int height, qboolean mipmap, qboolean is_sky)
} }
} }
return R_Upload32(trans, width, height, mipmap); qboolean ret = R_Upload32(trans, width, height, mipmap);
free(trans);
return ret;
} }
} }

View File

@ -238,20 +238,12 @@ GL3_Upload32(unsigned *data, int width, int height, qboolean mipmap)
qboolean qboolean
GL3_Upload8(byte *data, int width, int height, qboolean mipmap, qboolean is_sky) GL3_Upload8(byte *data, int width, int height, qboolean mipmap, qboolean is_sky)
{ {
unsigned trans[1024 * 1024]; int s = width * height;
int i, s; unsigned *trans = malloc(s * sizeof(unsigned));
int p;
s = width * height; for (int i = 0; i < s; i++)
if (s > sizeof(trans) / 4)
{ {
ri.Sys_Error(ERR_DROP, "%s: too large", __func__); int p = data[i];
}
for (i = 0; i < s; i++)
{
p = data[i];
trans[i] = d_8to24table[p]; trans[i] = d_8to24table[p];
/* transparent, so scan around for /* transparent, so scan around for
@ -286,7 +278,9 @@ GL3_Upload8(byte *data, int width, int height, qboolean mipmap, qboolean is_sky)
} }
} }
return GL3_Upload32(trans, width, height, mipmap); qboolean ret = GL3_Upload32(trans, width, height, mipmap);
free(trans);
return ret;
} }
typedef struct typedef struct