From 66dd3ef0704bf15f8465277cd560c5db961608ef Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Tue, 25 Feb 2020 21:23:13 +0900 Subject: [PATCH] Make a bunch of count things positive-only This fixes a pile of FIXMEs, because some things should never be negative. --- include/QF/pr_comp.h | 8 ++++---- libs/gamecode/pr_builtins.c | 3 ++- libs/gamecode/pr_debug.c | 17 +++++++++-------- libs/gamecode/pr_load.c | 2 +- tools/qfcc/source/dump_globals.c | 15 ++++++++++----- tools/qfcc/source/qfprogs.c | 3 ++- 6 files changed, 28 insertions(+), 20 deletions(-) diff --git a/include/QF/pr_comp.h b/include/QF/pr_comp.h index 8c03a7981..60022098f 100644 --- a/include/QF/pr_comp.h +++ b/include/QF/pr_comp.h @@ -27,7 +27,7 @@ typedef int16_t pr_short_t; typedef uint16_t pr_ushort_t; typedef int32_t pr_int_t; typedef uint32_t pr_uint_t; -typedef pr_int_t func_t; +typedef pr_uint_t func_t; typedef pr_int_t string_t; typedef pr_uint_t pointer_t; @@ -455,7 +455,7 @@ typedef struct dfunction_s { pr_int_t parm_start; pr_uint_t locals; // total ints of parms + locals - pr_int_t profile; // runtime + pr_uint_t profile; // runtime string_t s_name; pr_int_t s_file; // source file defined in @@ -502,10 +502,10 @@ typedef struct dprograms_s { pr_uint_t numfielddefs; pr_uint_t ofs_functions; - pr_int_t numfunctions; // function 0 is an empty + pr_uint_t numfunctions; // function 0 is an empty pr_uint_t ofs_strings; - pr_int_t numstrings; // first string is a null string + pr_uint_t numstrings; // first string is a null string pr_uint_t ofs_globals; pr_uint_t numglobals; diff --git a/libs/gamecode/pr_builtins.c b/libs/gamecode/pr_builtins.c index a3ab8c9d9..12c2e6050 100644 --- a/libs/gamecode/pr_builtins.c +++ b/libs/gamecode/pr_builtins.c @@ -158,7 +158,8 @@ bi_no_function (progs_t *pr) VISIBLE int PR_RelocateBuiltins (progs_t *pr) { - pr_int_t i, ind; + pr_uint_t i; + pr_int_t ind; int bad = 0; dfunction_t *desc; bfunction_t *func; diff --git a/libs/gamecode/pr_debug.c b/libs/gamecode/pr_debug.c index e9bc860a9..9fd61b020 100644 --- a/libs/gamecode/pr_debug.c +++ b/libs/gamecode/pr_debug.c @@ -483,7 +483,7 @@ PR_LoadDebug (progs_t *pr) i = pr->progs->numfunctions * sizeof (pr_auxfunction_t *); res->auxfunction_map = pr->allocate_progs_mem (pr, i); - for (i = 0; (int) i < pr->progs->numfunctions; i++) //FIXME (cast) + for (i = 0; i < pr->progs->numfunctions; i++) res->auxfunction_map[i] = 0; for (i = 0; i < res->debug->num_auxfunctions; i++) { @@ -539,7 +539,7 @@ VISIBLE pr_auxfunction_t * PR_Debug_MappedAuxFunction (progs_t *pr, pr_uint_t func) { prdeb_resources_t *res = pr->pr_debug_resources; - if (!res->debug || (int)func >= pr->progs->numfunctions) {//FIXME (cast) + if (!res->debug || func >= pr->progs->numfunctions) { return 0; } return res->auxfunction_map[func]; @@ -1046,7 +1046,7 @@ pr_debug_func_view (qfot_type_t *type, pr_type_t *value, void *_data) progs_t *pr = data->pr; dstring_t *dstr = data->dstr; - if (value->func_var < 0 || value->func_var >= pr->progs->numfunctions) { + if (value->func_var >= pr->progs->numfunctions) { dasprintf (dstr, "INVALID:%d", value->func_var); } else if (!value->func_var) { dstring_appendstr (dstr, "NULL"); @@ -1300,6 +1300,7 @@ PR_PrintStatement (progs_t *pr, dstatement_t *s, int contents) unsigned parm_ind = 0; pr_int_t opval; etype_t optype = ev_void; + func_t func; if (mode == 'P') { opchar = fmt[3]; @@ -1343,10 +1344,10 @@ PR_PrintStatement (progs_t *pr, dstatement_t *s, int contents) case 'F': str = global_string (&data, opval, optype, contents & 1); - if (G_FUNCTION (pr, opval) >= 0 - && G_FUNCTION (pr, opval) - < pr->progs->numfunctions) - call_func = pr->pr_functions + G_FUNCTION (pr, opval); + func = G_FUNCTION (pr, opval); + if (func < pr->progs->numfunctions) { + call_func = pr->pr_functions + func; + } break; case 'P': parm_def = PR_Get_Param_Def (pr, call_func, parm_ind); @@ -1468,7 +1469,7 @@ PR_StackTrace (progs_t *pr) VISIBLE void PR_Profile (progs_t * pr) { - pr_int_t max, num, i; + pr_uint_t max, num, i; dfunction_t *best, *f; num = 0; diff --git a/libs/gamecode/pr_load.c b/libs/gamecode/pr_load.c index 51723cd64..c8157efa8 100644 --- a/libs/gamecode/pr_load.c +++ b/libs/gamecode/pr_load.c @@ -370,7 +370,7 @@ PR_AddLoadFinishFunc (progs_t *pr, int (*func)(progs_t *)) static int pr_run_ctors (progs_t *pr) { - pr_int_t fnum; + pr_uint_t fnum; dfunction_t *func; for (fnum = 0; fnum < pr->progs->numfunctions; fnum++) { diff --git a/tools/qfcc/source/dump_globals.c b/tools/qfcc/source/dump_globals.c index 65271b289..9343a0ac7 100644 --- a/tools/qfcc/source/dump_globals.c +++ b/tools/qfcc/source/dump_globals.c @@ -86,7 +86,12 @@ dump_def (progs_t *pr, pr_def_t *def, int indent) break; case ev_string: string = G_INT (pr, offset); - if (string < 0 || string >= pr->progs->numstrings) { + // at runtime, strings can be negative (thus string_t is + // signed), but negative strings means they have been + // dynamically allocated, thus a negative string index should + // never appear in compiled code + if (string < 0 + || (pr_uint_t) string >= pr->progs->numstrings) { str = "invalid string offset"; comment = va (" %d %s", string, str); } else { @@ -115,7 +120,7 @@ dump_def (progs_t *pr, pr_def_t *def, int indent) { func_t func = G_FUNCTION (pr, offset); int start; - if (func >= 0 && func < pr->progs->numfunctions) { + if (func < pr->progs->numfunctions) { start = pr->pr_functions[func].first_statement; if (start > 0) comment = va (" %d @ %x", func, start); @@ -230,9 +235,9 @@ qfo_fields (qfo_t *qfo) void dump_functions (progs_t *pr) { - int i, j; + pr_uint_t i, j, count; const char *name; - int start, count; + int start; const char *comment; pr_def_t *encodings_def; pointer_t type_encodings = 0; @@ -277,7 +282,7 @@ dump_functions (progs_t *pr) if (!local_defs) { continue; } - for (j = 0; j < (int)aux->num_locals; j++) { + for (j = 0; j < aux->num_locals; j++) { dump_def (pr, local_defs + j, 1); } } diff --git a/tools/qfcc/source/qfprogs.c b/tools/qfcc/source/qfprogs.c index 37acd11fc..02f05bce9 100644 --- a/tools/qfcc/source/qfprogs.c +++ b/tools/qfcc/source/qfprogs.c @@ -255,7 +255,8 @@ static int load_progs (const char *name) { QFile *file; - int i, size; + int size; + pr_uint_t i; char buff[5]; Hash_FlushTable (func_tab);