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:
+13
-11
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user