From 54fa6c51f987dba465eabdf59e9322521f14ac85 Mon Sep 17 00:00:00 2001 From: Ildar Kamaletdinov Date: Sat, 7 May 2022 20:35:03 +0300 Subject: [PATCH] tools: Fix memory leaks in btgatt-server/client According to man buffer allocated by getline() should be freed by the user program even if getline() failed. Found by Linux Verification Center (linuxtesting.org) with the SVACE static analysis tool. Signed-off-by: Manika Shrivastava Signed-off-by: Ayush Garg --- tools/btgatt-client.c | 6 +++++- tools/btgatt-server.c | 5 ++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/tools/btgatt-client.c b/tools/btgatt-client.c index 69d6419..b936a6a 100755 --- a/tools/btgatt-client.c +++ b/tools/btgatt-client.c @@ -1369,12 +1369,16 @@ static void prompt_read_cb(int fd, uint32_t events, void *user_data) return; } - if ((read = getline(&line, &len, stdin)) == -1) + read = getline(&line, &len, stdin); + if (read < 0) { + free(line); return; + } if (read <= 1) { cmd_help(cli, NULL); print_prompt(); + free(line); return; } diff --git a/tools/btgatt-server.c b/tools/btgatt-server.c index 6c36d47..03230b2 100755 --- a/tools/btgatt-server.c +++ b/tools/btgatt-server.c @@ -1079,12 +1079,15 @@ static void prompt_read_cb(int fd, uint32_t events, void *user_data) } read = getline(&line, &len, stdin); - if (read < 0) + if (read < 0) { + free(line); return; + } if (read <= 1) { cmd_help(server, NULL); print_prompt(); + free(line); return; } -- 2.7.4