gpioset: fix memory leak in interactive mode
authorEsben Haabendal <esben@geanix.com>
Tue, 6 Dec 2022 08:46:37 +0000 (09:46 +0100)
committerBartosz Golaszewski <bartosz.golaszewski@linaro.org>
Wed, 7 Dec 2022 09:40:34 +0000 (10:40 +0100)
Even when readline() returns an empty buffer, we still need to free() it to
avoid leaking memory.

Signed-off-by: Esben Haabendal <esben@geanix.com>
[Kent: suggested a shorter version]
Co-developed-by: Kent Gibson <warthog618@gmail.com>
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
tools/gpioset.c

index 698dba383ed5f3e94e93eafc3020614153b5aedb..374fe728abc91e60cec169abafbea6b6f8faf3e1 100644 (file)
@@ -768,8 +768,10 @@ static void interact(struct gpiod_line_request **requests,
                fflush(stdout);
 
                line = readline(PROMPT);
-               if (!line || line[0] == '\0')
+               if (!line || line[0] == '\0') {
+                       free(line);
                        continue;
+               }
 
                for (i = strlen(line) - 1; (i > 0) && isspace(line[i]); i--)
                        line[i] = '\0';