From 4fccd4d48d7412f21a4fa254d06d311d93c854a7 Mon Sep 17 00:00:00 2001 From: zongor Date: Mon, 28 Jul 2025 23:13:39 -0400 Subject: [PATCH] remove temporary malloc --- src/arch/linux/main.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/arch/linux/main.c b/src/arch/linux/main.c index f727605..b73f8cc 100644 --- a/src/arch/linux/main.c +++ b/src/arch/linux/main.c @@ -4,6 +4,8 @@ #include "../../lexer.h" #include +#define MAX_SRC_SIZE 16384 + int main(int argc, char **argv) { VM vm = {0}; vm.frames_size = FRAMES_SIZE; @@ -22,12 +24,17 @@ int main(int argc, char **argv) { return 1; } + static char source[MAX_SRC_SIZE + 1]; + fseek(f, 0, SEEK_END); long len = ftell(f); fseek(f, 0, SEEK_SET); - char *source = (char *)malloc(len + 1); - fread(source, 1, len, f); - source[len] = '\0'; + if (len >= MAX_SRC_SIZE) { + perror("source is larget than buffer"); + return 1; + } + size_t read = fread(source, 1, len, f); + source[read] = '\0'; fclose(f); init_lexer(source); @@ -38,7 +45,6 @@ int main(int argc, char **argv) { if (token.type == TOKEN_EOF) break; } - free(source); return 0;