From 552219aec0b623024ce3cff0f8070436b36bafa9 Mon Sep 17 00:00:00 2001 From: Eric Chou Date: Wed, 12 Mar 2025 00:54:52 +0800 Subject: [PATCH] Fix memory leak in get_input when getline fails If getline() fails, we should free the pre-allocated line buffer to avoid memory leaks. If line is NULL, calling free(NULL) is safe according to the C99 specification (Section 7.20.3.2). This ensures proper memory management in case of input failure. Co-authored-by: charliechiou --- main.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/main.c b/main.c index 3b8e5bf..9c5ec3a 100644 --- a/main.c +++ b/main.c @@ -48,8 +48,10 @@ static int get_input(char player) while (x < 0 || x > (BOARD_SIZE - 1) || y < 0 || y > (BOARD_SIZE - 1)) { printf("%c> ", player); int r = getline(&line, &line_length, stdin); - if (r == -1) + if (r == -1) { + free(line); exit(1); + } if (r < 2) continue; x = 0;