Skip to content

Commit

Permalink
Report more out of memory errors instead of silently crashing
Browse files Browse the repository at this point in the history
  • Loading branch information
andrei-drexler committed Jul 14, 2024
1 parent 5cda0de commit c3c369f
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Quake/cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,8 @@ cmd_function_t *Cmd_AddCommand2 (const char *cmd_name, xcommand_t function, cmd_
if (host_initialized)
{
cmd = (cmd_function_t *) malloc(sizeof(*cmd) + strlen(cmd_name)+1);
if (!cmd)
Sys_Error ("Cmd_AddCommand2: out of memory (%s)", cmd_name);
cmd->name = strcpy((char*)(cmd + 1), cmd_name);
cmd->dynamic = true;
}
Expand Down
4 changes: 3 additions & 1 deletion Quake/console.c
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,8 @@ qboolean Con_CopySelectionToClipboard (void)
// Convert to UTF-8
maxsize = UTF8_FromQuake (NULL, 0, qtext);
utf8 = (char *) malloc (maxsize);
if (!utf8)
Sys_Error ("Con_CopySelectionToClipboard: out of memory on %" SDL_PRIu64 " bytes", (uint64_t)maxsize);
UTF8_FromQuake (utf8, maxsize, qtext);

// Copy the UTF-8 text to clipboard
Expand Down Expand Up @@ -1422,7 +1424,7 @@ void Con_LinkPrintf (const char *addr, const char *fmt, ...)
len = strlen (addr);
link = (conlink_t *) malloc (sizeof (conlink_t) + len + 1);
if (!link)
Sys_Error ("Con_LinkPrintf: out of memory on %" SDL_PRIu64 "u bytes", (uint64_t)(sizeof (conlink_t) + len + 1));
Sys_Error ("Con_LinkPrintf: out of memory on %" SDL_PRIu64 " bytes", (uint64_t)(sizeof (conlink_t) + len + 1));

memcpy (link + 1, addr, len + 1);
link->path = (const char *)(link + 1);
Expand Down
4 changes: 4 additions & 0 deletions Quake/gl_model.c
Original file line number Diff line number Diff line change
Expand Up @@ -3738,6 +3738,8 @@ static void MD5_ComputeNormals(iqmvert_t *vert, size_t numverts, unsigned short
hashmap = (int *) calloc (hashsize, sizeof (*hashmap));
weld = (unsigned short *) malloc (numverts * sizeof (*weld));
normals = (vec3_t *) calloc (numverts, sizeof (vec3_t));
if (!hashmap || !weld || !normals)
Sys_Error ("MD5_ComputeNormals: out of memory (%u verts/%u tris)", (unsigned int)numverts, (unsigned int)(numindexes/3));

for (v = 0; v < numverts; v++)
{
Expand Down Expand Up @@ -3890,6 +3892,8 @@ static void MD5Anim_Load(md5animctx_t *ctx, boneinfo_t *bones, size_t numbones)

ctx->posedata = outposes = (bonepose_t *) Hunk_Alloc(sizeof(*outposes)*ctx->numjoints*ctx->numposes);
frameposes = (bonepose_t *) malloc (sizeof (*frameposes) * ctx->numjoints);
if (!frameposes)
Sys_Error ("MD5Anim_Load: out of memory (%u joints)", (unsigned int)ctx->numjoints);


MD5EXPECT("hierarchy");
Expand Down
2 changes: 2 additions & 0 deletions Quake/gl_texmgr.c
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,8 @@ static void TexMgr_Imagedump_f (void)

GL_Bind (GL_TEXTURE0, glt);
buffer = (byte *) malloc(glt->width * glt->height * glt->depth * channels);
if (!buffer)
Sys_Error ("TexMgr_Imagedump_f: out of memory (%dx%dx%d %d bpp)", glt->width, glt->height, glt->depth, channels * 8);

if (glt->flags & TEXPREF_CUBEMAP)
{
Expand Down
2 changes: 2 additions & 0 deletions Quake/host_cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ static filelist_item_t *FileList_AddWithData (const char *name, const void *data
}

item = (filelist_item_t *) malloc (sizeof(filelist_item_t) + datasize);
if (!item)
Sys_Error ("FileList_AddWithData: out of memory on %" SDL_PRIu64 " bytes (%s)", (uint64_t)(sizeof(filelist_item_t) + datasize), name);
q_strlcpy (item->name, name, sizeof(item->name));
if (datasize)
{
Expand Down
2 changes: 2 additions & 0 deletions Quake/pr_edict.c
Original file line number Diff line number Diff line change
Expand Up @@ -1762,6 +1762,8 @@ static void PR_MergeEngineFieldDefs (void)
{ //we now know how many entries we need to add...
ddef_t *olddefs = qcvm->fielddefs;
qcvm->fielddefs = malloc(maxdefs * sizeof(*qcvm->fielddefs));
if (!qcvm->fielddefs)
Sys_Error ("PR_MergeEngineFieldDefs: out of memory (%d defs)", maxdefs);
memcpy(qcvm->fielddefs, olddefs, qcvm->progs->numfielddefs*sizeof(*qcvm->fielddefs));
if (olddefs != (ddef_t *)((byte *)qcvm->progs + qcvm->progs->ofs_fielddefs))
free(olddefs);
Expand Down
2 changes: 2 additions & 0 deletions Quake/r_brush.c
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,8 @@ void GL_BuildBModelVertexBuffer (void)
// build vertex array
varray_bytes = sizeof (glvert_t) * numverts;
varray = (glvert_t *) malloc (varray_bytes);
if (!varray)
Sys_Error ("GL_BuildBModelVertexBuffer: out of memory on %u bytes", varray_bytes);
varray_index = 0;

for (j=1 ; j<MAX_MODELS ; j++)
Expand Down
6 changes: 5 additions & 1 deletion Quake/snd_mix.c
Original file line number Diff line number Diff line change
Expand Up @@ -238,12 +238,16 @@ known to be 0 and skip 3/4 of the filter kernel.
static void S_ApplyFilter(filter_t *filter, int *data, int stride, int count)
{
int i, j;
size_t inputsize;
float *input;
const int kernelsize = filter->kernelsize;
const float *kernel = filter->kernel;
int parity;

input = (float *) malloc(sizeof(float) * (filter->kernelsize + count));
inputsize = sizeof(float) * (filter->kernelsize + count);
input = (float *) malloc(inputsize);
if (!input)
Sys_Error ("S_ApplyFilter: out of memory on %" SDL_PRIu64 " bytes", (uint64_t)inputsize);

// set up the input buffer
// memory holds the previous filter->kernelsize samples of input.
Expand Down
2 changes: 2 additions & 0 deletions Quake/sv_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -1778,6 +1778,8 @@ void SV_SpawnServer (const char *server)
/* Host_ClearMemory() called above already cleared the whole sv structure */
qcvm->max_edicts = CLAMP (MIN_EDICTS,(int)max_edicts.value,MAX_EDICTS); //johnfitz -- max_edicts cvar
qcvm->edicts = (edict_t *) malloc (qcvm->max_edicts*qcvm->edict_size); // ericw -- sv.edicts switched to use malloc()
if (!qcvm->edicts)
Sys_Error ("SV_SpawnServer: out of memory (%d edicts x %d bytes)", qcvm->max_edicts, qcvm->edict_size);
ClearLink (&qcvm->free_edicts);

sv.datagram.maxsize = sizeof(sv.datagram_buf);
Expand Down

0 comments on commit c3c369f

Please sign in to comment.