From 6eb6b6c0ba3b25f73ff13eae8be539e74cc95b50 Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Fri, 21 Dec 2012 21:53:13 +0900 Subject: [PATCH] Change pointer_t to unsigned and clean up the mess. It doesn't make sense to have negative pointers. The size of the commit is from enabling gcc's -Wtype-limits warning and cleaning up that mess too. --- config.d/compiling.m4 | 1 + include/QF/pr_comp.h | 2 +- include/QF/progs.h | 8 ++++---- libs/audio/renderer/snd_mix.c | 2 +- libs/gamecode/pr_debug.c | 5 ++--- libs/gamecode/pr_exec.c | 4 ++-- libs/gib/gib_execute.c | 6 +++--- libs/ruamoko/rua_obj.c | 2 +- libs/util/hash.c | 12 ++++++------ libs/video/renderer/noisetextures.c | 6 +++--- libs/video/renderer/r_part.c | 2 +- libs/video/renderer/sw/draw.c | 2 +- libs/video/renderer/sw32/draw.c | 2 +- qw/source/sv_main.c | 4 ++-- tools/bsp2img/bsp2img.c | 2 +- tools/qfcc/include/obj_file.h | 6 +++--- tools/qfcc/source/constfold.c | 4 ++-- tools/qfcc/source/dot_expr.c | 2 +- tools/qfcc/source/dump_lines.c | 5 ++--- tools/qfcc/source/linker.c | 15 +++++++-------- tools/qfcc/source/obj_file.c | 8 ++++---- tools/qfcc/source/obj_type.c | 2 +- tools/qfcc/source/statements.c | 6 +++--- tools/qfcc/source/symtab.c | 2 +- tools/qfcc/source/type.c | 2 +- 25 files changed, 55 insertions(+), 57 deletions(-) diff --git a/config.d/compiling.m4 b/config.d/compiling.m4 index bed6d99a2..07d1b832f 100644 --- a/config.d/compiling.m4 +++ b/config.d/compiling.m4 @@ -190,6 +190,7 @@ QF_CC_OPTION(-Wsign-compare) if test $CC_MAJ -gt 4 -o $CC_MAJ -eq 4 -a $CC_MIN -ge 5; then QF_CC_OPTION(-Wlogical-op) fi +QF_CC_OPTION(-Wtype-limits) QF_CC_OPTION_TEST([-fvisibility=hidden], [VISIBILITY=-fvisibility=hidden]) dnl QuakeForge uses lots of BCPL-style (//) comments, which can cause problems diff --git a/include/QF/pr_comp.h b/include/QF/pr_comp.h index 2e603b00f..00368d80a 100644 --- a/include/QF/pr_comp.h +++ b/include/QF/pr_comp.h @@ -29,7 +29,7 @@ typedef int32_t pr_int_t; typedef uint32_t pr_uint_t; typedef pr_int_t func_t; typedef pr_int_t string_t; -typedef pr_int_t pointer_t; +typedef pr_uint_t pointer_t; typedef enum { ev_void, diff --git a/include/QF/progs.h b/include/QF/progs.h index 6f9dde6aa..16e364d13 100644 --- a/include/QF/progs.h +++ b/include/QF/progs.h @@ -206,7 +206,7 @@ int PR_RunLoadFuncs (progs_t *pr); */ int PR_Check_Opcodes (progs_t *pr); -void PR_BoundsCheckSize (progs_t *pr, int addr, unsigned size); +void PR_BoundsCheckSize (progs_t *pr, pointer_t addr, unsigned size); void PR_BoundsCheck (progs_t *pr, int addr, etype_t type); //@} @@ -1577,7 +1577,7 @@ struct progs_s { ddef_t *pr_fielddefs; dstatement_t *pr_statements; pr_type_t *pr_globals; - int globals_size; + unsigned globals_size; //@} /// \name parameter block @@ -1630,8 +1630,8 @@ struct progs_s { /// \name obj info //@{ - int selector_index; - int selector_index_max; + unsigned selector_index; + unsigned selector_index_max; struct obj_list_s **selector_sels; string_t *selector_names; struct hashtab_s *selector_hash; diff --git a/libs/audio/renderer/snd_mix.c b/libs/audio/renderer/snd_mix.c index c0ab9f277..2c54836d1 100644 --- a/libs/audio/renderer/snd_mix.c +++ b/libs/audio/renderer/snd_mix.c @@ -478,7 +478,7 @@ SND_SetPaint (sfxbuffer_t *sc) }; wavinfo_t *info = sc->sfx->wavinfo (sc->sfx); - if (info->channels < 0 || info->channels > 8) + if (info->channels > 8) Sys_Error ("illegal channel count %d", info->channels); sc->paint = painters[info->channels]; } diff --git a/libs/gamecode/pr_debug.c b/libs/gamecode/pr_debug.c index 4ac781746..eadcc19b4 100644 --- a/libs/gamecode/pr_debug.c +++ b/libs/gamecode/pr_debug.c @@ -395,8 +395,7 @@ PR_Get_Lineno_Addr (progs_t *pr, pr_lineno_t *lineno) if (lineno->line) return lineno->fa.addr; - if (lineno->fa.func >= 0 - && lineno->fa.func < pr->debug->num_auxfunctions) { + if (lineno->fa.func < pr->debug->num_auxfunctions) { f = &pr->auxfunctions[lineno->fa.func]; return pr->pr_functions[f->function].first_statement; } @@ -701,7 +700,7 @@ def_string (progs_t *pr, pr_int_t ofs, dstring_t *dstr) } static const char * -global_string (progs_t *pr, pr_int_t ofs, etype_t type, int contents) +global_string (progs_t *pr, pointer_t ofs, etype_t type, int contents) { static dstring_t *line = NULL; ddef_t *def = NULL; diff --git a/libs/gamecode/pr_exec.c b/libs/gamecode/pr_exec.c index 8b1e989b0..3f9d8899d 100644 --- a/libs/gamecode/pr_exec.c +++ b/libs/gamecode/pr_exec.c @@ -255,9 +255,9 @@ PR_LeaveFunction (progs_t *pr) } VISIBLE void -PR_BoundsCheckSize (progs_t *pr, int addr, unsigned size) +PR_BoundsCheckSize (progs_t *pr, pointer_t addr, unsigned size) { - if (addr < 0 || addr >= pr->globals_size + if (addr >= pr->globals_size || size > (unsigned) (pr->globals_size - addr)) PR_RunError (pr, "invalid memory access: %d (0 to %d-%d)", addr, pr->globals_size, size); diff --git a/libs/gib/gib_execute.c b/libs/gib/gib_execute.c index 10ccf1804..c93f91f31 100644 --- a/libs/gib/gib_execute.c +++ b/libs/gib/gib_execute.c @@ -103,13 +103,13 @@ GIB_Execute_Split_Var (cbuf_t * cbuf) &GIB_DATA (cbuf)->globals, str, &i, false))) return; - if (end < 0) + if ((int) end < 0) end += var->size; else if (end > var->size) end = var->size; - if (start < 0) { + if ((int) start < 0) { start += var->size; - if (start < 0) + if ((int) start < 0) start = 0; } else if (start >= var->size || start >= end) return; diff --git a/libs/ruamoko/rua_obj.c b/libs/ruamoko/rua_obj.c index b13ee50ff..66aa049c6 100644 --- a/libs/ruamoko/rua_obj.c +++ b/libs/ruamoko/rua_obj.c @@ -1653,7 +1653,7 @@ static int rua_init_runtime (progs_t *pr) { ddef_t *def; - int i; + unsigned i; if (!pr->selector_hash) pr->selector_hash = Hash_NewTable (1021, selector_get_key, 0, pr); diff --git a/libs/util/hash.c b/libs/util/hash.c index 0f95ef7fa..587e4a24d 100644 --- a/libs/util/hash.c +++ b/libs/util/hash.c @@ -107,10 +107,10 @@ Hash_String (const char *str) // dx_hack_hash // shamelessly stolen from Daniel Phillips // from his post to lkml - unsigned long hash0 = 0x12a3fe2d, hash1 = 0x37abe8f9; + uint32_t hash0 = 0x12a3fe2d, hash1 = 0x37abe8f9; while (*str) { - unsigned long hash = hash1 + (hash0 ^ ((unsigned char)*str++ * 71523)); - if (hash < 0) hash -= 0x7fffffff; + uint32_t hash = hash1 + (hash0 ^ ((unsigned char)*str++ * 71523)); + if (hash & 0x80000000) hash -= 0x7fffffff; hash1 = hash0; hash0 = hash; } @@ -134,10 +134,10 @@ Hash_Buffer (const void *_buf, int len) // dx_hack_hash // shamelessly stolen from Daniel Phillips // from his post to lkml - unsigned long hash0 = 0x12a3fe2d, hash1 = 0x37abe8f9; + uint32_t hash0 = 0x12a3fe2d, hash1 = 0x37abe8f9; while (len-- > 0) { - unsigned long hash = hash1 + (hash0 ^ ((unsigned char)*buf++ * 71523)); - if (hash < 0) hash -= 0x7fffffff; + uint32_t hash = hash1 + (hash0 ^ ((unsigned char)*buf++ * 71523)); + if (hash & 0x80000000) hash -= 0x7fffffff; hash1 = hash0; hash0 = hash; } diff --git a/libs/video/renderer/noisetextures.c b/libs/video/renderer/noisetextures.c index 372d232df..2babe4096 100644 --- a/libs/video/renderer/noisetextures.c +++ b/libs/video/renderer/noisetextures.c @@ -68,7 +68,7 @@ noise_diamondsquare (unsigned char *noise, unsigned int size, if (startgrid != (unsigned int) (1 << gridpower)) Sys_Error("fractalnoise: grid must be power of 2"); - startgrid = bound(0, startgrid, size); + startgrid = min(startgrid, size); amplitude = 0xFFFF; // this gets halved before use noisebuf = calloc (size * size, sizeof (int)); memset(noisebuf, 0, size * size * sizeof(int)); @@ -121,8 +121,8 @@ noise_diamondsquare (unsigned char *noise, unsigned int size, void noise_plasma (unsigned char *noise, int size) { - unsigned int a, b, c, d, i; - int j, k; + unsigned int a, b, c, i; + int d, j, k; if (128 >= size) d = 64 / size; diff --git a/libs/video/renderer/r_part.c b/libs/video/renderer/r_part.c index e4e299fb9..d49c224b7 100644 --- a/libs/video/renderer/r_part.c +++ b/libs/video/renderer/r_part.c @@ -305,7 +305,7 @@ static pt_phys_func part_phys[] = { pt_phys_func R_ParticlePhysics (ptype_t type) { - if (type < pt_static || type > pt_flame) + if (type > pt_flame) Sys_Error ("R_ParticlePhysics: invalid particle type"); return part_phys[type]; } diff --git a/libs/video/renderer/sw/draw.c b/libs/video/renderer/sw/draw.c index 58e261f27..343f28d26 100644 --- a/libs/video/renderer/sw/draw.c +++ b/libs/video/renderer/sw/draw.c @@ -265,7 +265,7 @@ Draw_Character (int x, int y, unsigned int chr) if (y > vid.conheight - 8 || x < 0 || x > vid.conwidth - 8) return; - if (chr < 0 || chr > 255) + if (chr > 255) return; row = chr >> 4; diff --git a/libs/video/renderer/sw32/draw.c b/libs/video/renderer/sw32/draw.c index fe8496d7b..45d0c89d0 100644 --- a/libs/video/renderer/sw32/draw.c +++ b/libs/video/renderer/sw32/draw.c @@ -267,7 +267,7 @@ sw32_Draw_Character (int x, int y, unsigned int chr) if (y > vid.conheight - 8 || x < 0 || x > vid.conwidth - 8) return; - if (chr < 0 || chr > 255) + if (chr > 255) return; row = chr >> 4; diff --git a/qw/source/sv_main.c b/qw/source/sv_main.c index 134d65742..42a313aa6 100644 --- a/qw/source/sv_main.c +++ b/qw/source/sv_main.c @@ -1319,8 +1319,8 @@ SV_StringToFilter (const char *address, ipfilter_t *f) if (mask == -1) { if (sv_filter_automask->int_val) { mask = sizeof (b) * 8; - i = sizeof (b) - 1; - while (i >= 0 && !b[i]) { + i = sizeof (b); + while (i > 0 && !b[i - 1]) { mask -= 8; i--; } diff --git a/tools/bsp2img/bsp2img.c b/tools/bsp2img/bsp2img.c index 8608921be..649fe1427 100644 --- a/tools/bsp2img/bsp2img.c +++ b/tools/bsp2img/bsp2img.c @@ -159,7 +159,7 @@ plotpoint (image_t *image, long xco, long yco, unsigned int color) bigcol += color; - bigcol = bound (0, bigcol, 255); + bigcol = min (bigcol, 255); image->image[yco * image->width + xco] = (eightbit) bigcol; diff --git a/tools/qfcc/include/obj_file.h b/tools/qfcc/include/obj_file.h index c409bff89..cd9cd45f6 100644 --- a/tools/qfcc/include/obj_file.h +++ b/tools/qfcc/include/obj_file.h @@ -86,7 +86,7 @@ typedef struct qfo_space_s { pr_int_t defs; ///< index of first def pr_int_t num_defs; ///< zero for code or string spaces pr_int_t data; ///< byte offset in qfo - pr_int_t data_size; ///< in elements. zero for entity spaces + pr_uint_t data_size; ///< in elements. zero for entity spaces pr_int_t id; pr_int_t reserved[2]; } qfo_space_t; @@ -240,7 +240,7 @@ typedef struct qfo_reloc_s { pr_int_t space; ///< index of space holding data to be adjusted pr_int_t offset; ///< offset of the relocation pr_int_t type; ///< type of the relocation (::reloc_type) - pr_int_t target; ///< def/func/etc this relocation is for + pr_uint_t target; ///< def/func/etc this relocation is for } qfo_reloc_t; /** In-memory representation of a QFO space @@ -254,7 +254,7 @@ typedef struct qfo_mspace_s { pr_type_t *data; char *strings; } d; - int data_size; + unsigned data_size; int id; } qfo_mspace_t; diff --git a/tools/qfcc/source/constfold.c b/tools/qfcc/source/constfold.c index b0297cb78..799b2ff0f 100644 --- a/tools/qfcc/source/constfold.c +++ b/tools/qfcc/source/constfold.c @@ -1487,7 +1487,7 @@ fold_constants (expr_t *e) if (op == 'A' || op == 'g' || op == 'r') return e; t1 = extract_type (e1); - if (t1 < 0 || t1 >= ev_type_count || !do_unary_op[t1]) { + if (t1 >= ev_type_count || !do_unary_op[t1]) { print_expr (e); internal_error (e, "invalid type: %d", t1); } @@ -1519,7 +1519,7 @@ fold_constants (expr_t *e) if (op == 's') return e; - if (t1 < 0 || t1 >= ev_type_count || t2 < 0 || t2 >= ev_type_count + if (t1 >= ev_type_count || t2 >= ev_type_count || !do_op[t1] || !do_op[t1][t2]) internal_error (e, "invalid type"); return do_op[t1][t2] (op, e, e1, e2); diff --git a/tools/qfcc/source/dot_expr.c b/tools/qfcc/source/dot_expr.c index 7693c9afa..0ca173788 100644 --- a/tools/qfcc/source/dot_expr.c +++ b/tools/qfcc/source/dot_expr.c @@ -466,7 +466,7 @@ _print_expr (dstring_t *dstr, expr_t *e, int level, int id, expr_t *next) return; e->printid = id; - if (e->type < 0 || e->type > ex_value) { + if ((int) e->type < 0 || e->type > ex_value) { dasprintf (dstr, "%*se_%p [label=\"(bad expr type)\\n%d\"];\n", indent, "", e, e->line); return; diff --git a/tools/qfcc/source/dump_lines.c b/tools/qfcc/source/dump_lines.c index 24ebb9682..847bdb5c2 100644 --- a/tools/qfcc/source/dump_lines.c +++ b/tools/qfcc/source/dump_lines.c @@ -60,10 +60,9 @@ dump_lines (progs_t *pr) if (!lineno->line) { aux_func = 0; func = 0; - if (lineno->fa.func >= 0 - && lineno->fa.func < pr->debug->num_auxfunctions) + if (lineno->fa.func < pr->debug->num_auxfunctions) aux_func = pr->auxfunctions + lineno->fa.func; - if (aux_func && aux_func->function >= 0 + if (aux_func && aux_func->function < (unsigned int) pr->progs->numfunctions) func = pr->pr_functions + aux_func->function; } diff --git a/tools/qfcc/source/linker.c b/tools/qfcc/source/linker.c index 50c2ce015..28e5bbff7 100644 --- a/tools/qfcc/source/linker.c +++ b/tools/qfcc/source/linker.c @@ -183,12 +183,12 @@ linker_type_mismatch (qfo_def_t *def, qfo_def_t *prev) const char *def_encoding; const char *prev_encoding; - if (def->type < 0) - def_encoding = QFO_GETSTR (work, -def->type); + if ((int) def->type < 0) + def_encoding = QFO_GETSTR (work, -(int) def->type); else def_encoding = QFO_TYPESTR (work, def->type); - if (prev->type < 0) - prev_encoding = QFO_GETSTR (work, -prev->type); + if ((int) prev->type < 0) + prev_encoding = QFO_GETSTR (work, -(int) prev->type); else prev_encoding = QFO_TYPESTR (work, prev->type); def_error (def, "type mismatch for `%s' `%s'", WORKSTR (def->name), @@ -844,7 +844,7 @@ process_type_space (qfo_t *qfo, qfo_mspace_t *space, int pass) if (!bi->defref) continue; def = REF (bi->defref); - if (def->type >= 0) + if ((int) def->type >= 0) continue; ref = Hash_Find (defined_type_defs, WORKSTR (-def->type)); if (!ref) @@ -926,8 +926,7 @@ process_loose_relocs (qfo_t *qfo) if (reloc->type == rel_def_string) { const char *str; - if (reloc->target < 0 - || reloc->target >= qfo->spaces[qfo_strings_space].data_size) { + if (reloc->target >= qfo->spaces[qfo_strings_space].data_size) { linker_error ("bad string reloc at %d:%x", reloc->space, reloc->offset); reloc->target = 0; @@ -963,7 +962,7 @@ linker_add_qfo (qfo_t *qfo) work_func_base = work->num_funcs; for (pass = 0; pass < 2; pass++) { for (i = 0, space = qfo->spaces; i < qfo->num_spaces; i++, space++) { - if (space->type < 0 || space->type > qfos_type) { + if ((int) space->type < 0 || space->type > qfos_type) { linker_error ("bad space type"); return 1; } diff --git a/tools/qfcc/source/obj_file.c b/tools/qfcc/source/obj_file.c index 1a1058721..0e3636d89 100644 --- a/tools/qfcc/source/obj_file.c +++ b/tools/qfcc/source/obj_file.c @@ -621,7 +621,7 @@ static etype_t get_def_type (qfo_t *qfo, pointer_t type) { qfot_type_t *type_def; - if (type < 0 || type >= qfo->spaces[qfo_type_space].data_size) + if (type >= qfo->spaces[qfo_type_space].data_size) return ev_void; type_def = QFO_POINTER (qfo, qfo_type_space, qfot_type_t, type); switch ((ty_meta_e)type_def->ty) { @@ -646,7 +646,7 @@ get_type_size (qfo_t *qfo, pointer_t type) { qfot_type_t *type_def; int i, size; - if (type < 0 || type >= qfo->spaces[qfo_type_space].data_size) + if (type >= qfo->spaces[qfo_type_space].data_size) return 1; type_def = QFO_POINTER (qfo, qfo_type_space, qfot_type_t, type); switch ((ty_meta_e)type_def->ty) { @@ -684,7 +684,7 @@ function_params (qfo_t *qfo, qfo_func_t *func, dfunction_t *df) int num_params; int i; - if (func->type < 0 || func->type >= qfo->spaces[qfo_type_space].data_size) + if (func->type >= qfo->spaces[qfo_type_space].data_size) return; type = QFO_POINTER (qfo, qfo_type_space, qfot_type_t, func->type); if (type->ty != ty_none && type->t.type != ev_func) @@ -787,7 +787,7 @@ qfo_to_progs (qfo_t *qfo, int *size) dprograms_t *progs; qfo_def_t *types_def = 0; int i, j; - int locals_size = 0; + unsigned locals_size = 0; int locals_start; int big_locals = 0; int big_func = 0; diff --git a/tools/qfcc/source/obj_type.c b/tools/qfcc/source/obj_type.c index 66b2375b3..3454448ba 100644 --- a/tools/qfcc/source/obj_type.c +++ b/tools/qfcc/source/obj_type.c @@ -273,7 +273,7 @@ qfo_encode_type (type_t *type) } if (type->type_def) return type->type_def; - if (type->meta < 0 || type->meta > ty_class) + if (type->meta > ty_class) internal_error (0, "bad type meta type"); if (!type->encoding) type->encoding = type_get_encoding (type); diff --git a/tools/qfcc/source/statements.c b/tools/qfcc/source/statements.c index 9251c97a1..db6b30c38 100644 --- a/tools/qfcc/source/statements.c +++ b/tools/qfcc/source/statements.c @@ -67,7 +67,7 @@ static const char *op_type_names[] = { const char * optype_str (op_type_e type) { - if (type < 0 || type > op_temp) + if (type > op_temp) return ""; return op_type_names[type]; } @@ -1011,7 +1011,7 @@ statement_subexpr (sblock_t *sblock, expr_t *e, operand_t **op) return sblock; } - if (e->type < 0 || e->type > ex_value) + if (e->type > ex_value) internal_error (e, "bad expression type"); if (!sfuncs[e->type]) internal_error (e, "unexpected expression type"); @@ -1287,7 +1287,7 @@ statement_slist (sblock_t *sblock, expr_t *e) }; for (/**/; e; e = e->next) { - if (e->type < 0 || e->type > ex_value) + if (e->type > ex_value) internal_error (e, "bad expression type"); sblock = sfuncs[e->type] (sblock, e); } diff --git a/tools/qfcc/source/symtab.c b/tools/qfcc/source/symtab.c index ff2c0f4d6..ca3bf1c48 100644 --- a/tools/qfcc/source/symtab.c +++ b/tools/qfcc/source/symtab.c @@ -63,7 +63,7 @@ static const char *sy_type_names[] = { const char * symtype_str (sy_type_e type) { - if (type < 0 || type > sy_class) + if (type > sy_class) return ""; return sy_type_names[type]; } diff --git a/tools/qfcc/source/type.c b/tools/qfcc/source/type.c index c8d56b659..9dc578614 100644 --- a/tools/qfcc/source/type.c +++ b/tools/qfcc/source/type.c @@ -93,7 +93,7 @@ static type_t *free_types; etype_t low_level_type (type_t *type) { - if (type->type < 0 || type->type >= ev_type_count) + if (type->type >= ev_type_count) internal_error (0, "invalid type"); if (type->type == ev_type_count) internal_error (0, "found 'type count' type");