diff --git a/include/QF/progs.h b/include/QF/progs.h index 0cd31a021..779b6a7d8 100644 --- a/include/QF/progs.h +++ b/include/QF/progs.h @@ -107,6 +107,7 @@ int PR_AccessField (progs_t *pr, const char *name, etype_t type, edict_t *EDICT_NUM(progs_t *pr, int n); int NUM_FOR_EDICT(progs_t *pr, edict_t *e); +int NUM_FOR_BAD_EDICT(progs_t *pr, edict_t *e); #define NEXT_EDICT(p,e) ((edict_t *)( (byte *)e + (p)->pr_edict_size)) diff --git a/libs/gamecode/engine/pr_edict.c b/libs/gamecode/engine/pr_edict.c index bc053a81b..85ebfdbd8 100644 --- a/libs/gamecode/engine/pr_edict.c +++ b/libs/gamecode/engine/pr_edict.c @@ -361,7 +361,7 @@ PR_ValueString (progs_t * pr, etype_t type, pr_type_t *val) break; case ev_entity: snprintf (line, sizeof (line), "entity %i", - NUM_FOR_EDICT (pr, PROG_TO_EDICT (pr, val->entity_var))); + NUM_FOR_BAD_EDICT (pr, PROG_TO_EDICT (pr, val->entity_var))); break; case ev_func: f = pr->pr_functions + val->func_var; @@ -425,7 +425,7 @@ PR_UglyValueString (progs_t * pr, etype_t type, pr_type_t *val) break; case ev_entity: snprintf (line, sizeof (line), "%i", - NUM_FOR_EDICT (pr, PROG_TO_EDICT (pr, val->entity_var))); + NUM_FOR_BAD_EDICT (pr, PROG_TO_EDICT (pr, val->entity_var))); break; case ev_func: f = pr->pr_functions + val->func_var; @@ -544,7 +544,7 @@ ED_Print (progs_t * pr, edict_t *ed) return; } - Sys_Printf ("\nEDICT %i:\n", NUM_FOR_EDICT (pr, ed)); + Sys_Printf ("\nEDICT %i:\n", NUM_FOR_BAD_EDICT (pr, ed)); for (i = 1; i < pr->progs->numfielddefs; i++) { d = &pr->pr_fielddefs[i]; name = PR_GetString (pr, d->s_name); @@ -1459,20 +1459,31 @@ EDICT_NUM (progs_t * pr, int n) { int offs = n * pr->pr_edict_size; if (offs < 0 || n >= pr->pr_edictareasize) - PR_Error (pr, "EDICT_NUM: bad number %i", n); + PR_RunError (pr, "EDICT_NUM: bad number %i", n); return PROG_TO_EDICT (pr, offs); } int -NUM_FOR_EDICT (progs_t * pr, edict_t *e) +NUM_FOR_BAD_EDICT (progs_t *pr, edict_t *e) { int b; b = (byte *) e - (byte *) * (pr)->edicts; b = b / pr->pr_edict_size; + return b; +} + +int +NUM_FOR_EDICT (progs_t *pr, edict_t *e) +{ + int b; + + b = NUM_FOR_BAD_EDICT (pr, e); + if (b && (b < 0 || b >= *(pr)->num_edicts)) - PR_Error (pr, "NUM_FOR_EDICT: bad pointer %d %p %p", b, e, * (pr)->edicts); + PR_RunError (pr, "NUM_FOR_EDICT: bad pointer %d %p %p", b, e, * (pr)->edicts); + return b; } diff --git a/libs/gamecode/engine/pr_exec.c b/libs/gamecode/engine/pr_exec.c index 428c6cc02..b9818ec4d 100644 --- a/libs/gamecode/engine/pr_exec.c +++ b/libs/gamecode/engine/pr_exec.c @@ -268,7 +268,7 @@ PR_ExecuteProgram (progs_t * pr, func_t fnum) if (!fnum || fnum >= pr->progs->numfunctions) { if (*pr->globals.self) ED_Print (pr, PROG_TO_EDICT (pr, *pr->globals.self)); - PR_Error (pr, "PR_ExecuteProgram: NULL function"); + PR_RunError (pr, "PR_ExecuteProgram: NULL function"); } f = &pr->pr_functions[fnum];