From fcbc810db8e6551e6c4198ea7886dc7d35855b20 Mon Sep 17 00:00:00 2001 From: Emin Arslan Date: Mon, 4 May 2026 10:34:47 +0300 Subject: [PATCH] fix: replace stdlib calls with forth_* and fix UB Co-authored-by: aider (openrouter/moonshotai/kimi-k2.6) --- forth_interp.c | 2 +- forth_words.c | 24 +++++++++++++----------- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/forth_interp.c b/forth_interp.c index 6e3f98c..0b12cc5 100644 --- a/forth_interp.c +++ b/forth_interp.c @@ -8,7 +8,7 @@ char* next_token(void) { } if (*input_ptr == '\0') return NULL; char* start = input_ptr; - while (*input_ptr != '\0' && !isspace((unsigned char)*input_ptr)) { + while (*input_ptr != '\0' && !forth_isspace((unsigned char)*input_ptr)) { input_ptr++; } if (*input_ptr != '\0') { diff --git a/forth_words.c b/forth_words.c index 6bb13ed..874994f 100644 --- a/forth_words.c +++ b/forth_words.c @@ -495,10 +495,10 @@ void do_variable(Word* w) { new_w->prev = dict_head; dict_head = new_w; - size_t len = strlen(name); + size_t len = forth_strlen(name); if (len > MAX_NAME_LEN) len = MAX_NAME_LEN; new_w->flags = (uint8_t)len; - memcpy(new_w->name, name, len); + forth_memcpy(new_w->name, name, len); new_w->name[len] = '\0'; new_w->code = do_do_var; new_w->body = var_cell; // body points directly to the data cell in user_mem @@ -524,10 +524,10 @@ void do_constant(Word* w) { new_w->prev = dict_head; dict_head = new_w; - size_t len = strlen(name); + size_t len = forth_strlen(name); if (len > MAX_NAME_LEN) len = MAX_NAME_LEN; new_w->flags = (uint8_t)len; - memcpy(new_w->name, name, len); + forth_memcpy(new_w->name, name, len); new_w->name[len] = '\0'; new_w->code = do_do_const; new_w->body = val_cell; // body points to the cell that holds the value @@ -583,9 +583,9 @@ void do_colon(Word* w) { (void)w; char* name = next_token(); if (!name) { forth_printf("':' expects a name\n"); return; } - size_t len = strlen(name); + size_t len = forth_strlen(name); if (len > MAX_NAME_LEN) len = MAX_NAME_LEN; - memcpy(compiling_name, name, len); + forth_memcpy(compiling_name, name, len); compiling_name[len] = '\0'; state = 1; compile_idx = 0; @@ -603,7 +603,7 @@ void do_semicolon(Word* w) { // Create body copy of compiled cells Cell* body_copy = forth_alloc_body(compile_idx); if (!body_copy) { forth_printf("Out of memory\n"); return; } - memcpy(body_copy, compile_buf, compile_idx * sizeof(Cell)); + forth_memcpy(body_copy, compile_buf, compile_idx * sizeof(Cell)); // Create new word entry Word* new_w = forth_alloc_word(); @@ -611,10 +611,10 @@ void do_semicolon(Word* w) { new_w->prev = dict_head; dict_head = new_w; - size_t len = strlen(compiling_name); + size_t len = forth_strlen(compiling_name); if (len > MAX_NAME_LEN) len = MAX_NAME_LEN; new_w->flags = (uint8_t)len; // no hidden, no immediate - memcpy(new_w->name, compiling_name, len); + forth_memcpy(new_w->name, compiling_name, len); new_w->name[len] = '\0'; new_w->code = do_docolon; new_w->body = body_copy; @@ -692,7 +692,8 @@ void do_until(Word* w) { if (!w_zbranch) { forth_printf("Fatal: 0branch not found\n"); return; } if (!ensure_compile_cap(2)) return; compile_buf[compile_idx++] = (Cell){.word = w_zbranch}; - compile_buf[compile_idx++] = (Cell){.num = begin_idx - compile_idx}; + compile_buf[compile_idx] = (Cell){.num = begin_idx - compile_idx}; + compile_idx++; } void do_while(Word* w) { @@ -722,7 +723,8 @@ void do_repeat(Word* w) { if (!w_branch) { forth_printf("Fatal: branch not found\n"); return; } if (!ensure_compile_cap(2)) return; compile_buf[compile_idx++] = (Cell){.word = w_branch}; - compile_buf[compile_idx++] = (Cell){.num = begin_idx - compile_idx}; + compile_buf[compile_idx] = (Cell){.num = begin_idx - compile_idx}; + compile_idx++; compile_buf[while_offset_idx].num = compile_idx - while_offset_idx; }