tools: Fix memory leaks in btgatt-server/client
authorIldar Kamaletdinov <i.kamaletdinov@omp.ru>
Sat, 7 May 2022 17:35:03 +0000 (20:35 +0300)
committerAyush Garg <ayush.garg@samsung.com>
Mon, 15 May 2023 09:25:54 +0000 (14:55 +0530)
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 <manika.sh@samsung.com>
Signed-off-by: Ayush Garg <ayush.garg@samsung.com>
tools/btgatt-client.c
tools/btgatt-server.c

index 69d6419..b936a6a 100755 (executable)
@@ -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;
        }
 
index 6c36d47..03230b2 100755 (executable)
@@ -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;
        }