From 6a5bbf6f0adc67c433c58166212fa652108b7be6 Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Tue, 14 May 2002 06:37:28 +0000 Subject: [PATCH] memory allocation checking cleanup --- include/QF/sys.h | 4 ++-- libs/models/gl_model_fullbright.c | 3 +-- libs/util/cmd.c | 18 ++++++------------ libs/util/cvar.c | 3 +-- libs/util/tga.c | 4 ---- libs/video/renderer/gl/gl_screen.c | 3 +-- libs/video/renderer/gl/gl_textures.c | 3 +-- libs/video/targets/fbset.c | 3 +-- libs/video/targets/vid_common_sw32.c | 3 +-- libs/video/targets/vid_svgalib.c | 3 +-- libs/video/targets/vid_x11.c | 3 +-- nq/source/host.c | 6 ++---- nq/source/locs.c | 3 +-- qw/source/cl_slist.c | 12 ++++-------- qw/source/locs.c | 3 +-- tools/qfcc/source/cmdlib.c | 3 +-- tools/qfcc/source/expr.c | 9 +++------ tools/qfcc/source/pr_def.c | 3 +-- tools/qfcc/source/qc-lex.l | 6 ++---- tools/qfcc/source/qc-parse.y | 6 ++---- tools/qfcc/source/switch.c | 6 ++---- tools/qfcc/source/type.c | 3 +-- tools/qfdefs/source/fix_globals.c | 3 +-- tools/qwaq/main.c | 3 +-- 24 files changed, 38 insertions(+), 78 deletions(-) diff --git a/include/QF/sys.h b/include/QF/sys.h index 400899c89..c50433f20 100644 --- a/include/QF/sys.h +++ b/include/QF/sys.h @@ -91,8 +91,8 @@ void Sys_DebugLog(const char *file, const char *fmt, ...) __attribute__((format( #define SYS_CHECKMEM(x) \ do { \ - if (!(x)) \ - Sys_Error ("%s: Failed to reallocate memory.", __func__); \ + if (!(x)) \ + Sys_Error ("%s: Failed to allocate memory.", __func__); \ } while (0) #endif // __sys_h diff --git a/libs/models/gl_model_fullbright.c b/libs/models/gl_model_fullbright.c index 1f61e9a33..297f04e77 100644 --- a/libs/models/gl_model_fullbright.c +++ b/libs/models/gl_model_fullbright.c @@ -72,8 +72,7 @@ Mod_Fullbright (byte * skin, int width, int height, char *name) // ptexels = Hunk_Alloc(s); ptexels = malloc (pixels); - if (!ptexels) - Sys_Error ("Mod_Fullbright: Memory Allocation Failure"); + SYS_CHECKMEM (ptexels); if (Mod_CalcFullbright (skin, ptexels, pixels)) { Sys_DPrintf ("FB Model ID: '%s'\n", name); texnum = GL_LoadTexture (name, width, height, ptexels, true, true, 1); diff --git a/libs/util/cmd.c b/libs/util/cmd.c index d0d99c4d6..603b097dd 100644 --- a/libs/util/cmd.c +++ b/libs/util/cmd.c @@ -658,8 +658,7 @@ Cmd_StuffCmds_f (void) // pull out the commands build = malloc (s + 1); - if (!build) - Sys_Error ("Cmd_StuffCmds_f: Memory Allocation Failure"); + SYS_CHECKMEM (build); build[0] = 0; for (i = 0; i < s - 1; i++) { @@ -1557,8 +1556,7 @@ Cmd_AddCommand (const char *cmd_name, xcommand_t function, } cmd = calloc (1, sizeof (cmd_function_t)); - if (!cmd) - Sys_Error ("Cmd_AddCommand: Memory Allocation Failure"); + SYS_CHECKMEM (cmd); cmd->name = cmd_name; cmd->function = function; cmd->description = description; @@ -1699,8 +1697,7 @@ Cmd_CompleteBuildList (const char *partial) len = strlen (partial); buf = malloc (sizeofbuf + sizeof (char *)); - if (!buf) - Sys_Error ("Cmd_CompleteBuildList: Memory Allocation Failure"); + SYS_CHECKMEM (buf); // Loop through the alias list and print all matches for (cmd = cmd_functions; cmd; cmd = cmd->next) if (!strncasecmp (partial, cmd->name, len)) @@ -1788,8 +1785,7 @@ Cmd_CompleteAliasBuildList (const char *partial) len = strlen (partial); buf = malloc (sizeofbuf + sizeof (char *)); - if (!buf) - Sys_Error ("Cmd_CompleteAliasBuildList: Memory Allocation Failure"); + SYS_CHECKMEM (buf); // Loop through the alias list and print all matches for (alias = cmd_alias; alias; alias = alias->next) if (!strncasecmp (partial, alias->name, len)) @@ -1927,8 +1923,7 @@ Cmd_Alias_f (void) cmdalias_t **a; alias = calloc (1, sizeof (cmdalias_t)); - if (!alias) - Sys_Error ("Cmd_Alias_f: Memory Allocation Failure"); + SYS_CHECKMEM (alias); alias->name = strdup (s); Hash_Add (cmd_alias_hash, alias); for (a = &cmd_alias; *a; a = &(*a)->next) @@ -1939,8 +1934,7 @@ Cmd_Alias_f (void) } // copy the rest of the command line cmd = malloc (strlen (Cmd_Args (1)) + 2); // can never be longer - if (!cmd) - Sys_Error ("Cmd_Alias_f: Memory Allocation Failure"); + SYS_CHECKMEM (cmd); cmd[0] = 0; // start out with a null string c = Cmd_Argc (); for (i = 2; i < c; i++) { diff --git a/libs/util/cvar.c b/libs/util/cvar.c index 7d9042380..9e39b0357 100644 --- a/libs/util/cvar.c +++ b/libs/util/cvar.c @@ -216,8 +216,7 @@ Cvar_CompleteBuildList (const char *partial) len = strlen(partial); buf = malloc(sizeofbuf + sizeof (char *)); - if (!buf) - Sys_Error ("Cvar_CompleteBuildList: Memory Allocation Failure"); + SYS_CHECKMEM (buf); // Loop through the alias list and print all matches for (cvar = cvar_vars; cvar; cvar = cvar->next) if (!strncasecmp(partial, cvar->name, len)) diff --git a/libs/util/tga.c b/libs/util/tga.c index d771d9150..58f586310 100644 --- a/libs/util/tga.c +++ b/libs/util/tga.c @@ -149,15 +149,11 @@ LoadTGA (VFile *fin) switch (targa->pixel_size) { case 24: tex = Hunk_TempAlloc (field_offset (tex_t, data[numPixels * 3])); - if (!tex) - Sys_Error ("LoadTGA: Memory Allocation Failure"); tex->format = tex_rgb; break; default: case 32: tex = Hunk_TempAlloc (field_offset (tex_t, data[numPixels * 4])); - if (!tex) - Sys_Error ("LoadTGA: Memory Allocation Failure"); tex->format = tex_rgba; break; } diff --git a/libs/video/renderer/gl/gl_screen.c b/libs/video/renderer/gl/gl_screen.c index 537cb1e01..329608af4 100644 --- a/libs/video/renderer/gl/gl_screen.c +++ b/libs/video/renderer/gl/gl_screen.c @@ -603,8 +603,7 @@ SCR_ScreenShot_f (void) return; } buffer = malloc (glwidth * glheight * 3); - if (!buffer) - Sys_Error ("SCR_ScreenShot_f: Memory Allocation Failure"); + SYS_CHECKMEM (buffer); qfglReadPixels (glx, gly, glwidth, glheight, GL_BGR_EXT, GL_UNSIGNED_BYTE, buffer); WriteTGAfile (pcxname, buffer, glwidth, glheight); diff --git a/libs/video/renderer/gl/gl_textures.c b/libs/video/renderer/gl/gl_textures.c index f0ccf7e70..985b5d6b7 100644 --- a/libs/video/renderer/gl/gl_textures.c +++ b/libs/video/renderer/gl/gl_textures.c @@ -508,8 +508,7 @@ GL_Upload8 (byte * data, int width, int height, qboolean mipmap, qboolean alpha) s = width * height; trans = malloc (s * sizeof (unsigned int)); - if (!trans) - Sys_Error ("GL_Upload8: Memory Allocation Failure"); + SYS_CHECKMEM (trans); // if there are no transparent pixels, make it a 3 component // texture even if it was specified as otherwise diff --git a/libs/video/targets/fbset.c b/libs/video/targets/fbset.c index d835cc649..1472c212d 100644 --- a/libs/video/targets/fbset.c +++ b/libs/video/targets/fbset.c @@ -407,8 +407,7 @@ AddVideoMode (const struct VideoMode *vmode) Die("%s:%d: Duplicate mode name `%s'\n", Opt_modedb, line, vmode->name); vmode2 = malloc(sizeof(struct VideoMode)); - if (!vmode2) - Sys_Error ("AddVideoMode: Memory Allocation Failure"); + SYS_CHECKMEM (vmode2); *vmode2 = *vmode; if (!FillScanRates(vmode2)) Die("%s:%d: Bad video mode `%s'\n", Opt_modedb, line, vmode2->name); diff --git a/libs/video/targets/vid_common_sw32.c b/libs/video/targets/vid_common_sw32.c index 7e3becf4e..afeeb68c7 100644 --- a/libs/video/targets/vid_common_sw32.c +++ b/libs/video/targets/vid_common_sw32.c @@ -197,8 +197,7 @@ VID_MakeColormaps (int fullbrights, byte *pal) vid.colormap8 = malloc (256*VID_GRADES * sizeof (byte)); vid.colormap16 = malloc (256*VID_GRADES * sizeof (short)); vid.colormap32 = malloc (256*VID_GRADES * sizeof (int)); - if (!vid.colormap8 || !vid.colormap16 || !vid.colormap32) - Sys_Error ("VID_MakeColormaps: Memory Allocation Failure"); + SYS_CHECKMEM (vid.colormap8 && vid.colormap16 && vid.colormap32); VID_MakeColormap8(vid.colormap8, pal); VID_MakeColormap16(vid.colormap16, pal); VID_MakeColormap32(vid.colormap32, pal); diff --git a/libs/video/targets/vid_svgalib.c b/libs/video/targets/vid_svgalib.c index fcf0c2527..dbe22e557 100644 --- a/libs/video/targets/vid_svgalib.c +++ b/libs/video/targets/vid_svgalib.c @@ -266,8 +266,7 @@ VID_InitModes (void) /* Get complete information on all modes */ num_modes = vga_lastmodenumber () + 1; modes = malloc (num_modes * sizeof (vga_modeinfo)); - if (!modes) - Sys_Error ("VID_InitModes: Memory Allocation Failure"); + SYS_CHECKMEM (modes); for (i = 0; i < num_modes; i++) { if (vga_hasmode (i)) { memcpy (&modes[i], vga_getmodeinfo (i), sizeof (vga_modeinfo)); diff --git a/libs/video/targets/vid_x11.c b/libs/video/targets/vid_x11.c index c19c09286..39a9627c9 100644 --- a/libs/video/targets/vid_x11.c +++ b/libs/video/targets/vid_x11.c @@ -301,8 +301,7 @@ ResetFrameBuffer (void) pwidth = 4; mem = ((vid.width * pwidth + 7) & ~7) * vid.height; buf = malloc (mem); - if (!buf) - Sys_Error ("ResetFrameBuffer: Memory Allocation Failure"); + SYS_CHECKMEM (buf); // allocate new screen buffer x_framebuffer[0] = XCreateImage (x_disp, x_vis, x_visinfo->depth, diff --git a/nq/source/host.c b/nq/source/host.c index f70e91546..c99ca8cdd 100644 --- a/nq/source/host.c +++ b/nq/source/host.c @@ -743,16 +743,14 @@ Host_InitVCR (quakeparms_t *parms) Qread (vcrFile, &com_argc, sizeof (int)); com_argv = malloc (com_argc * sizeof (char *)); - if (!com_argv) - Sys_Error ("Host_InitVCR: Memory Allocation Failure"); + SYS_CHECKMEM (com_argv); com_argv[0] = parms->argv[0]; for (i = 0; i < com_argc; i++) { Qread (vcrFile, &len, sizeof (int)); p = malloc (len); - if (!p) - Sys_Error ("Host_InitVCR: Memory Allocation Failure"); + SYS_CHECKMEM (p); Qread (vcrFile, p, len); com_argv[i + 1] = p; } diff --git a/nq/source/locs.c b/nq/source/locs.c index 30fa50f17..b2f2e259a 100644 --- a/nq/source/locs.c +++ b/nq/source/locs.c @@ -97,8 +97,7 @@ locs_add (const vec3_t location, const char *name) num = locations_count - 1; locations[num] = malloc (sizeof (location_t)); - if (!locations[num]) - Sys_Error ("locs_add: Memory Allocation Failure"); + SYS_CHECKMEM (locations[num]); locations[num]->loc[0] = location[0]; locations[num]->loc[1] = location[1]; diff --git a/qw/source/cl_slist.c b/qw/source/cl_slist.c index a7fa7bf64..27d01eb89 100644 --- a/qw/source/cl_slist.c +++ b/qw/source/cl_slist.c @@ -128,8 +128,7 @@ SL_Add (server_entry_t *start, char *ip, char *desc) start->next = 0; start->server = malloc (strlen (ip) + 1); start->desc = malloc (strlen (desc ? desc : ip) + 1); - if (!start->server || !start->desc) - Sys_Error ("SL_Add: Memory Allocation Failure"); + SYS_CHECKMEM (start->server && start->desc); strcpy (start->server, ip); strcpy (start->desc, desc ? desc : ip); start->status = NULL; @@ -145,8 +144,7 @@ SL_Add (server_entry_t *start, char *ip, char *desc) p->next->prev = p; p->next->server = malloc (strlen (ip) + 1); p->next->desc = malloc (strlen (desc ? desc : ip) + 1); - if (!p->next->server || !p->next->desc) - Sys_Error ("SL_Add: Memory Allocation Failure"); + SYS_CHECKMEM (p->next->server && p->next->desc); strcpy (p->next->server, ip); strcpy (p->next->desc, desc ? desc : ip); @@ -190,8 +188,7 @@ SL_InsB (server_entry_t *start, server_entry_t *place, char *ip, char *desc) new->server = malloc (strlen (ip) + 1); new->desc = malloc (strlen (desc) + 1); - if (!new->server || !new->desc) - Sys_Error ("SL_InsB: Memory Allocation Failure"); + SYS_CHECKMEM (new->server && new->desc); strcpy (new->server, ip); strcpy (new->desc, desc); other = place->prev; @@ -636,8 +633,7 @@ SL_LoadF (VFile *f, server_entry_t *start) if ((st = gettokstart (line, 1, ' ')) != NULL) { len = gettoklen (line, 1, ' '); addr = malloc (len + 1); - if (!addr) - Sys_Error ("SL_LoadF: Memory Allocation Failure"); + SYS_CHECKMEM (addr); strncpy (addr, &line[0], len); addr[len] = '\0'; if ((st = gettokstart (line, 2, ' '))) { diff --git a/qw/source/locs.c b/qw/source/locs.c index 9364da4b3..d38fea545 100644 --- a/qw/source/locs.c +++ b/qw/source/locs.c @@ -98,8 +98,7 @@ locs_add (const vec3_t location, const char *name) num = locations_count - 1; locations[num] = malloc (sizeof (location_t)); - if (!locations[num]) - Sys_Error ("locs_add: Memory Allocation Failure"); + SYS_CHECKMEM (locations[num]); locations[num]->loc[0] = location[0]; locations[num]->loc[1] = location[1]; diff --git a/tools/qfcc/source/cmdlib.c b/tools/qfcc/source/cmdlib.c index 6c9925b21..db5556560 100644 --- a/tools/qfcc/source/cmdlib.c +++ b/tools/qfcc/source/cmdlib.c @@ -224,8 +224,7 @@ LoadFile (char *filename, void **bufferptr) f = SafeOpenRead (filename); length = FileLength (f); buffer = malloc (length + 1); - if (!buffer) - Sys_Error ("LoadFile: Memory Allocation Failure"); + SYS_CHECKMEM (buffer); ((char *) buffer)[length] = 0; SafeRead (f, buffer, length); fclose (f); diff --git a/tools/qfcc/source/expr.c b/tools/qfcc/source/expr.c index 61e96e9cd..95f3d5496 100644 --- a/tools/qfcc/source/expr.c +++ b/tools/qfcc/source/expr.c @@ -348,8 +348,7 @@ new_label_name (void) int len = 1 + strlen (fname) + 1 + num_digits (lnum) + 1; char *lname = malloc (len); - if (!lname) - Sys_Error ("new_label_expr: Memory Allocation Failure"); + SYS_CHECKMEM (lname); snprintf (lname, len, "$%s_%d", fname, lnum); return lname; } @@ -601,8 +600,7 @@ do_op_string (int op, expr_t *e1, expr_t *e2) case '+': len = strlen (s1) + strlen (s2) + 1; buf = malloc (len); - if (!buf) - Sys_Error ("do_op_string: Memory Allocation Failure"); + SYS_CHECKMEM (buf); strcpy (buf, s1); strcat (buf, s2); e1->e.string_val = buf; @@ -1570,8 +1568,7 @@ function_expr (expr_t *e1, expr_t *e2) ret->type = ex_def; ret->e.def = memcpy (malloc (sizeof (def_t)), &def_ret, sizeof (def_t)); - if (!ret->e.def) - Sys_Error ("function_expr: Memory Allocation Failure"); + SYS_CHECKMEM (ret->e.def); ret->e.def->type = ftype->aux_type; call->e.block.result = ret; } diff --git a/tools/qfcc/source/pr_def.c b/tools/qfcc/source/pr_def.c index 33ba59206..d3bcf25b7 100644 --- a/tools/qfcc/source/pr_def.c +++ b/tools/qfcc/source/pr_def.c @@ -268,8 +268,7 @@ PR_FreeLocation (def_t *def) if (!free_free_locs) { free_free_locs = malloc (256 * sizeof (locref_t)); - if (!free_free_locs) - Sys_Error ("PR_FreeLocation: Memory Allocation Failure"); + SYS_CHECKMEM (free_free_locs); for (loc = free_free_locs; loc - free_free_locs < 255; loc++) loc->next = loc + 1; loc->next = 0; diff --git a/tools/qfcc/source/qc-lex.l b/tools/qfcc/source/qc-lex.l index d9994d321..2bbbcfcc9 100644 --- a/tools/qfcc/source/qc-lex.l +++ b/tools/qfcc/source/qc-lex.l @@ -376,8 +376,7 @@ void add_frame_macro (char *token) { frame_t *frame = malloc (sizeof (frame_t)); - if (!frame) - Sys_Error ("add_frame_macro: Memory Allocation Failure"); + SYS_CHECKMEM (frame); frame->name = strdup (token); frame->num = frame_number++; @@ -403,8 +402,7 @@ make_string (char *token) int quote; s = str = malloc (strlen (token) + 1); - if (!str) - Sys_Error ("make_string: Memory Allocation Failure"); + SYS_CHECKMEM (str); mask = 0x00; boldnext = 0; diff --git a/tools/qfcc/source/qc-parse.y b/tools/qfcc/source/qc-parse.y index a556bc1c9..05b464ac6 100644 --- a/tools/qfcc/source/qc-parse.y +++ b/tools/qfcc/source/qc-parse.y @@ -1274,8 +1274,7 @@ scan_scope (hashtab_t *tab, def_t *scope) for (def = scope->scope_next; def; def = def->scope_next) { if (def->name && !def->removed) { def_state_t *ds = malloc (sizeof (def_state_t)); - if (!ds) - Sys_Error ("scan_scope: Memory Allocation Failure"); + SYS_CHECKMEM (ds); ds->def = def; ds->state = def->initialized; Hash_Add (tab, ds); @@ -1305,8 +1304,7 @@ merge_local_inits (hashtab_t *dl_1, hashtab_t *dl_2) (*ds)->def->initialized = (*ds)->state; nds = malloc (sizeof (def_state_t)); - if (!nds) - Sys_Error ("merge_local_inits: Memory Allocation Failure"); + SYS_CHECKMEM (nds); nds->def = (*ds)->def; nds->state = (*ds)->state && d->state; Hash_Add (tab, nds); diff --git a/tools/qfcc/source/switch.c b/tools/qfcc/source/switch.c index 881c6f9d7..dd8af75c0 100644 --- a/tools/qfcc/source/switch.c +++ b/tools/qfcc/source/switch.c @@ -74,8 +74,7 @@ case_label_expr (switch_block_t *switch_block, expr_t *value) { case_label_t *cl = malloc (sizeof (case_label_t)); - if (!cl) - Sys_Error ("case_label_expr: Memory Allocation Failure"); + SYS_CHECKMEM (cl); if (value) convert_name (value); @@ -105,8 +104,7 @@ new_switch_block (void) { switch_block_t *switch_block = malloc (sizeof (switch_block_t)); - if (!switch_block) - Sys_Error ("new_switch_block: Memory Allocation Failure"); + SYS_CHECKMEM (switch_block); switch_block->labels = Hash_NewTable (127, 0, 0, 0); Hash_SetHashCompare (switch_block->labels, get_hash, compare); switch_block->test = 0; diff --git a/tools/qfcc/source/type.c b/tools/qfcc/source/type.c index a9e2cbd16..638259700 100644 --- a/tools/qfcc/source/type.c +++ b/tools/qfcc/source/type.c @@ -121,8 +121,7 @@ find_type (type_t *type) // allocate a new one check = malloc (sizeof (*check)); - if (!check) - Sys_Error ("find_type: Memory Allocation Failure"); + SYS_CHECKMEM (check); *check = *type; chain_type (check); diff --git a/tools/qfdefs/source/fix_globals.c b/tools/qfdefs/source/fix_globals.c index 0869901a5..14da7bb09 100644 --- a/tools/qfdefs/source/fix_globals.c +++ b/tools/qfdefs/source/fix_globals.c @@ -105,8 +105,7 @@ fix_missing_globals (progs_t *pr, def_t *globals) } progs = malloc (pr->progs_size + strings_size); - if (!progs) - Sys_Error ("fix_missing_globals: Memory Allocation Failure"); + SYS_CHECKMEM (progs); memcpy (progs, pr->progs, pr->progs_size); offs = progs->ofs_strings + progs->numstrings; diff --git a/tools/qwaq/main.c b/tools/qwaq/main.c index b70cf1347..321511f56 100644 --- a/tools/qwaq/main.c +++ b/tools/qwaq/main.c @@ -64,8 +64,7 @@ main () Cvar_Init_Hash (); Cmd_Init_Hash (); membase = malloc (memsize); - if (!membase) - Sys_Error ("Memory Allocation Failure"); + SYS_CHECKMEM (membase); Memory_Init (membase, memsize); Cvar_Init (); Cbuf_Init ();