Replace magic numbers in stack bounds checks with named constants

Define MAX_FOR_DEPTH, MAX_GOSUB_DEPTH, MAX_WHILE_DEPTH in interp.h
and use them in all overflow checks in interp.c.
This commit is contained in:
Eremey Valetov
2026-02-22 13:35:39 -05:00
parent 701d51938c
commit b5bb78a174
2 changed files with 10 additions and 7 deletions
+6 -3
View File
@@ -32,11 +32,14 @@ typedef struct {
int option_base;
/* Control flow stacks */
for_entry_t for_stack[16];
#define MAX_FOR_DEPTH 16
#define MAX_GOSUB_DEPTH 24
#define MAX_WHILE_DEPTH 16
for_entry_t for_stack[MAX_FOR_DEPTH];
int for_sp;
gosub_entry_t gosub_stack[24];
gosub_entry_t gosub_stack[MAX_GOSUB_DEPTH];
int gosub_sp;
while_entry_t while_stack[16];
while_entry_t while_stack[MAX_WHILE_DEPTH];
int while_sp;
/* DEF FN */
+4 -4
View File
@@ -1509,7 +1509,7 @@ void gw_exec_stmt(void)
program_line_t *target = gw_find_line(num);
if (!target) gw_error(ERR_UL);
if (gw.gosub_sp >= 24)
if (gw.gosub_sp >= MAX_GOSUB_DEPTH)
gw_error(ERR_OM);
gw.gosub_stack[gw.gosub_sp].ret_text = gw.text_ptr;
gw.gosub_stack[gw.gosub_sp].ret_line = gw.cur_line;
@@ -1593,7 +1593,7 @@ void gw_exec_stmt(void)
}
}
if (gw.for_sp >= 16)
if (gw.for_sp >= MAX_FOR_DEPTH)
gw_error(ERR_OM);
for_entry_t *f = &gw.for_stack[gw.for_sp++];
@@ -1759,7 +1759,7 @@ void gw_exec_stmt(void)
}
}
if (!found) {
if (gw.while_sp >= 16)
if (gw.while_sp >= MAX_WHILE_DEPTH)
gw_error(ERR_OM);
gw.while_stack[gw.while_sp].while_text = while_text;
gw.while_stack[gw.while_sp].while_line = while_line;
@@ -1887,7 +1887,7 @@ void gw_exec_stmt(void)
if (!target) gw_error(ERR_UL);
if (is_gosub) {
if (gw.gosub_sp >= 24)
if (gw.gosub_sp >= MAX_GOSUB_DEPTH)
gw_error(ERR_OM);
gw.gosub_stack[gw.gosub_sp].ret_text = gw.text_ptr;
gw.gosub_stack[gw.gosub_sp].ret_line = gw.cur_line;