fix: replace stdlib calls with forth_* and fix UB

Co-authored-by: aider (openrouter/moonshotai/kimi-k2.6) <aider@aider.chat>
This commit is contained in:
2026-05-04 10:34:47 +03:00
parent dbf4eb5d0e
commit fcbc810db8
2 changed files with 14 additions and 12 deletions
+1 -1
View File
@@ -8,7 +8,7 @@ char* next_token(void) {
} }
if (*input_ptr == '\0') return NULL; if (*input_ptr == '\0') return NULL;
char* start = input_ptr; 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++; input_ptr++;
} }
if (*input_ptr != '\0') { if (*input_ptr != '\0') {
+13 -11
View File
@@ -495,10 +495,10 @@ void do_variable(Word* w) {
new_w->prev = dict_head; new_w->prev = dict_head;
dict_head = new_w; dict_head = new_w;
size_t len = strlen(name); size_t len = forth_strlen(name);
if (len > MAX_NAME_LEN) len = MAX_NAME_LEN; if (len > MAX_NAME_LEN) len = MAX_NAME_LEN;
new_w->flags = (uint8_t)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->name[len] = '\0';
new_w->code = do_do_var; new_w->code = do_do_var;
new_w->body = var_cell; // body points directly to the data cell in user_mem 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; new_w->prev = dict_head;
dict_head = new_w; dict_head = new_w;
size_t len = strlen(name); size_t len = forth_strlen(name);
if (len > MAX_NAME_LEN) len = MAX_NAME_LEN; if (len > MAX_NAME_LEN) len = MAX_NAME_LEN;
new_w->flags = (uint8_t)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->name[len] = '\0';
new_w->code = do_do_const; new_w->code = do_do_const;
new_w->body = val_cell; // body points to the cell that holds the value 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; (void)w;
char* name = next_token(); char* name = next_token();
if (!name) { forth_printf("':' expects a name\n"); return; } 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; if (len > MAX_NAME_LEN) len = MAX_NAME_LEN;
memcpy(compiling_name, name, len); forth_memcpy(compiling_name, name, len);
compiling_name[len] = '\0'; compiling_name[len] = '\0';
state = 1; state = 1;
compile_idx = 0; compile_idx = 0;
@@ -603,7 +603,7 @@ void do_semicolon(Word* w) {
// Create body copy of compiled cells // Create body copy of compiled cells
Cell* body_copy = forth_alloc_body(compile_idx); Cell* body_copy = forth_alloc_body(compile_idx);
if (!body_copy) { forth_printf("Out of memory\n"); return; } 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 // Create new word entry
Word* new_w = forth_alloc_word(); Word* new_w = forth_alloc_word();
@@ -611,10 +611,10 @@ void do_semicolon(Word* w) {
new_w->prev = dict_head; new_w->prev = dict_head;
dict_head = new_w; 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; if (len > MAX_NAME_LEN) len = MAX_NAME_LEN;
new_w->flags = (uint8_t)len; // no hidden, no immediate 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->name[len] = '\0';
new_w->code = do_docolon; new_w->code = do_docolon;
new_w->body = body_copy; 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 (!w_zbranch) { forth_printf("Fatal: 0branch not found\n"); return; }
if (!ensure_compile_cap(2)) return; if (!ensure_compile_cap(2)) return;
compile_buf[compile_idx++] = (Cell){.word = w_zbranch}; 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) { 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 (!w_branch) { forth_printf("Fatal: branch not found\n"); return; }
if (!ensure_compile_cap(2)) return; if (!ensure_compile_cap(2)) return;
compile_buf[compile_idx++] = (Cell){.word = w_branch}; 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; compile_buf[while_offset_idx].num = compile_idx - while_offset_idx;
} }