From 2c8b6b3944f71549faaaead88b21d6b06a0c8db5 Mon Sep 17 00:00:00 2001 From: Lukas Krickl Date: Fri, 20 Mar 2026 08:14:19 +0100 Subject: [PATCH] script: lex now checks code size and token max size. --- src/l_lsl.c | 82 +++++++++++++++++++++++++++++++++-------------------- 1 file changed, 52 insertions(+), 30 deletions(-) diff --git a/src/l_lsl.c b/src/l_lsl.c index 6e280d7..d9837f8 100644 --- a/src/l_lsl.c +++ b/src/l_lsl.c @@ -87,24 +87,46 @@ lrts_bool l_lsl_isterm(char c) { } /* peeks current token */ -#define l_lsl_lex_peek(lex) \ - (lex->code)[lex->pos] +char l_lsl_lex_peek(struct l_lsl_vm *v, struct l_lsl_lex *lex) { + if (lex->pos > lex->code_len) { + l_lsl_lex_err(v, lex, L_LSL_ERR_LEX, "Lexer out of bounds"); + } + return (lex->code)[lex->pos]; +} /* peeks current token and then advances cursor */ -#define l_lsl_lex_consume(lex)\ - (lex->code)[lex->pos++] +char l_lsl_lex_consume(struct l_lsl_vm *v, struct l_lsl_lex *lex) { + if (lex->pos >= lex->code_len) { + l_lsl_lex_err(v, lex, L_LSL_ERR_LEX, "Lexer out of bounds"); + } + return (lex->code)[lex->pos++]; +} /* advances cursor and then peeks current token */ -#define l_lsl_lex_advance(v) \ - (lex->code)[++lex->pos] +char l_lsl_lex_advance(struct l_lsl_vm *v, struct l_lsl_lex *lex) { + if (lex->pos >= lex->code_len) { + l_lsl_lex_err(v, lex, L_LSL_ERR_LEX, "Lexer out of bounds"); + } + return (lex->code)[++lex->pos]; +} /* writes a char to token and advances token_len */ -#define l_lsl_lex_write_token(t, c) \ - t.val[t.len] = c; \ - t.len += 1; +void l_lsl_lex_write_token(struct l_lsl_vm *v, struct l_lsl_lex *lex, + struct l_lsl_token *t, char c) { + if (t->len >= L_LSL_TOK_MAX) { + l_lsl_lex_err(v, lex, L_LSL_ERR_LEX, "Token too large"); + } + t->val[t->len] = c; + t->len += 1; +} -#define l_lsl_lex_term_token(t) \ - t.val[t.len] = '\0'; +void l_lsl_lex_term_token(struct l_lsl_vm *v, struct l_lsl_lex *lex, + struct l_lsl_token *t) { + if (t->len >= L_LSL_TOK_MAX) { + l_lsl_lex_err(v, lex, L_LSL_ERR_LEX, "Token too large"); + } + t->val[t->len] = '\0'; +} char tokbuf[L_LSL_TOK_MAX]; struct l_lsl_token l_lsl_next_token(struct l_lsl_vm *v, struct l_lsl_lex *lex) { @@ -116,13 +138,13 @@ struct l_lsl_token l_lsl_next_token(struct l_lsl_vm *v, struct l_lsl_lex *lex) { - c = l_lsl_lex_peek(lex); + c = l_lsl_lex_peek(v, lex); /* consume all spaces */ while (l_lsl_isspace(c)) { if (c == '\n') { lex->line_num++; } - c = l_lsl_lex_advance(lex); + c = l_lsl_lex_advance(v, lex); } if (l_lsl_isnum(c)) { @@ -130,32 +152,32 @@ struct l_lsl_token l_lsl_next_token(struct l_lsl_vm *v, struct l_lsl_lex *lex) { if (c == '0') { /* special base */ /* specific base number */ - c = l_lsl_lex_advance(lex); + c = l_lsl_lex_advance(v, lex); if (l_lsl_isnum(c)) { /* octal */ while (l_lsl_isoct(c)) { - l_lsl_lex_write_token(t, c); - c = l_lsl_lex_advance(lex); + l_lsl_lex_write_token(v, lex, &t, c); + c = l_lsl_lex_advance(v, lex); } - c = l_lsl_lex_advance(lex); + c = l_lsl_lex_advance(v, lex); t.base = 8; } else if (c == 'x') { /* hex */ - c = l_lsl_lex_advance(lex); + c = l_lsl_lex_advance(v, lex); while (l_lsl_ishexnum(c)) { - l_lsl_lex_write_token(t, c); - c = l_lsl_lex_advance(lex); + l_lsl_lex_write_token(v, lex, &t, c); + c = l_lsl_lex_advance(v, lex); } t.base = 16; } else if (c == 'b') { /* bin */ - c = l_lsl_lex_advance(lex); + c = l_lsl_lex_advance(v, lex); while (l_lsl_isbin(c)) { - l_lsl_lex_write_token(t, c); - c = l_lsl_lex_advance(lex); + l_lsl_lex_write_token(v, lex, &t, c); + c = l_lsl_lex_advance(v, lex); } - c = l_lsl_lex_advance(lex); + c = l_lsl_lex_advance(v, lex); t.base = 2; } else { l_lsl_lex_err(v, lex, L_LSL_ERR_INVALID_NUMBER_BASE, @@ -165,8 +187,8 @@ struct l_lsl_token l_lsl_next_token(struct l_lsl_vm *v, struct l_lsl_lex *lex) { } else { /* decimal number */ while (l_lsl_isnum(c)) { - l_lsl_lex_write_token(t, c); - c = l_lsl_lex_advance(lex); + l_lsl_lex_write_token(v, lex, &t, c); + c = l_lsl_lex_advance(v, lex); } t.base = 10; } @@ -175,20 +197,20 @@ struct l_lsl_token l_lsl_next_token(struct l_lsl_vm *v, struct l_lsl_lex *lex) { } else if (c == '\'' || c == '\"') { /* string literal */ /* consume initial term */ - c = l_lsl_lex_advance(lex); + c = l_lsl_lex_advance(v, lex); string_term = c; while (c && c != string_term) { - c = l_lsl_lex_advance(lex); + c = l_lsl_lex_advance(v, lex); } } else { l_lsl_lex_err(v, lex, L_LSL_ERR_LEX, "unknown char: %c\n", c); t.type = L_LSL_TOK_NONE; } - l_lsl_lex_term_token(t); + l_lsl_lex_term_token(v, lex, &t); /* end of a token must be a term */ - c = l_lsl_lex_peek(lex); + c = l_lsl_lex_peek(v, lex); if (!l_lsl_isterm(c)) { l_lsl_lex_err(v, lex, L_LSL_ERR_LEX, "unexpected end of token\n", c); t.type = L_LSL_TOK_NONE; -- 2.30.2