diff options
author | Linnnus <[email protected]> | 2025-04-15 00:52:26 +0000 |
---|---|---|
committer | Linnnus <[email protected]> | 2025-04-15 01:08:30 +0000 |
commit | f667ae8eff0f0e87130c7b5b934e32c9fdb9f957 (patch) | |
tree | 61a21f4b8df171ff0b6f3f027b372091c985a2c4 | |
parent | a140d00b5cb36a729cbe7d8bcc591472befaa49d (diff) |
fix(cli): Do proper line allocation for REPL
-rw-r--r-- | src/cli/main.c | 7 |
1 files changed, 5 insertions, 2 deletions
diff --git a/src/cli/main.c b/src/cli/main.c index cbecf50..ae84837 100644 --- a/src/cli/main.c +++ b/src/cli/main.c @@ -109,7 +109,8 @@ static void run_file(SandState *S, const char *filename) { static void repl(SandState *S) { char *line = NULL; - size_t line_length = 0; + size_t line_capacity = 0; + ssize_t line_length = 0; unsigned prompt_nr = 1; @@ -118,7 +119,7 @@ static void repl(SandState *S) { fflush(stdout); // At this point the memory at `line` is reused, invalidating all existing references. - if (getline(&line, &line_length, stdin) < 0) { + if ((line_length = getline(&line, &line_capacity, stdin)) < 0) { fputc('\n', stdout); break; // No more input - leave REPL loop. } @@ -130,6 +131,8 @@ static void repl(SandState *S) { sand_interpret(S, filename, line, line_length); } + + free(line); } static void print_help(FILE *stream, const char *argv0) { |