Skip to content

Commit 98d6293

Browse files
prevent LIBRARY from inside EVAL
1 parent 38d638f commit 98d6293

3 files changed

Lines changed: 82 additions & 2 deletions

File tree

include/basic.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -884,6 +884,7 @@ void until_statement(struct basic_ctx* ctx);
884884
void endif_statement(struct basic_ctx* ctx);
885885
void end_statement(struct basic_ctx* ctx);
886886
void panic_statement(struct basic_ctx* ctx);
887+
bool basic_in_eval(struct basic_ctx* ctx);
887888

888889
/* Process/memory functions */
889890
int64_t basic_getproccount(struct basic_ctx* ctx);
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
DEF FNrematch(pat$, txt$)
2+
FOR I = 0 TO LEN(pat$)
3+
re_pat$(I) = MID$(pat$, I, I + 1)
4+
NEXT
5+
FOR I = 0 TO LEN(txt$)
6+
re_txt$(I) = MID$(txt$, I, I + 1)
7+
NEXT
8+
re_pat_len = LEN(pat$)
9+
re_txt_len = LEN(txt$)
10+
11+
IF re_pat$(0) = "^" THEN
12+
IF FNmatchhere(1, 0) = TRUE THEN
13+
=TRUE
14+
ENDIF
15+
=FALSE
16+
ENDIF
17+
18+
FOR i = 0 TO re_txt_len
19+
IF FNmatchhere(0, i) = TRUE THEN
20+
=TRUE
21+
ENDIF
22+
NEXT
23+
=FALSE
24+
25+
DEF FNmatchhere(pi, ti)
26+
IF pi = re_pat_len THEN
27+
=TRUE
28+
ENDIF
29+
30+
IF re_pat$(pi) = "$" THEN
31+
IF pi + 1 = re_pat_len THEN
32+
IF ti = re_txt_len THEN
33+
=TRUE
34+
ENDIF
35+
=FALSE
36+
ENDIF
37+
ENDIF
38+
39+
IF pi + 1 < re_pat_len THEN
40+
IF re_pat$(pi + 1) = "*" THEN
41+
=FNmatchstar(re_pat$(pi), pi + 2, ti)
42+
ENDIF
43+
ENDIF
44+
45+
IF ti < re_txt_len THEN
46+
IF re_pat$(pi) = "." OR re_pat$(pi) = re_txt$(ti) THEN
47+
=FNmatchhere(pi + 1, ti + 1)
48+
ENDIF
49+
ENDIF
50+
=FALSE
51+
52+
DEF FNmatchstar(c$, pi, ti)
53+
FOR i = ti TO re_txt_len
54+
IF FNmatchhere(pi, i) = TRUE THEN
55+
=TRUE
56+
ENDIF
57+
IF i = re_txt_len THEN
58+
=FALSE
59+
ENDIF
60+
IF c$ <> "." AND c$ <> re_txt$(i) THEN
61+
=FALSE
62+
ENDIF
63+
NEXT
64+
=FALSE

src/basic/main.c

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,12 @@ void library_statement(struct basic_ctx* ctx)
294294
/* Validate the file exists and is not a directory */
295295
fs_directory_entry_t* file_info = fs_get_file_info(lib_file);
296296
accept_or_return(NEWLINE, ctx);
297+
298+
if (basic_in_eval(ctx)) {
299+
tokenizer_error_print(ctx, "Loading libraries from EVAL is not allowed");
300+
return;
301+
}
302+
297303
if (!file_info || fs_is_directory(lib_file)) {
298304
tokenizer_error_printf(ctx, "Not a library file: '%s'", lib_file);
299305
return;
@@ -583,15 +589,24 @@ void chain_statement(struct basic_ctx* ctx)
583589
accept_or_return(NEWLINE, ctx);
584590
}
585591

592+
bool basic_in_eval(struct basic_ctx* ctx)
593+
{
594+
return (ctx->current_linenum == EVAL_LINE);
595+
}
596+
586597
void eval_statement(struct basic_ctx* ctx)
587598
{
588599
accept_or_return(EVAL, ctx);
589600
const char* v = str_expr(ctx);
590601
accept_or_return(NEWLINE, ctx);
591602

592-
if (ctx->current_linenum == EVAL_LINE) {
603+
if (basic_in_eval(ctx)) {
593604
ctx->eval_linenum = 0;
594-
tokenizer_error_print(ctx, "Recursive EVAL");
605+
basic_set_string_variable("ERROR$", "Recursive EVAL", ctx, false, false);
606+
basic_set_int_variable("ERROR", 1, ctx, false, false);
607+
setforeground(current_console, COLOUR_LIGHTRED);
608+
kprintf("Recursive EVAL\n");
609+
setforeground(current_console, COLOUR_WHITE);
595610
return;
596611
}
597612

0 commit comments

Comments
 (0)