]> git.ipfire.org Git - thirdparty/lldpd.git/commitdiff
lldpcli: fix memory leak with use of readline
authorVincent Bernat <vincent@bernat.im>
Tue, 14 Jun 2016 09:58:21 +0000 (11:58 +0200)
committerVincent Bernat <vincent@bernat.im>
Tue, 14 Jun 2016 09:58:21 +0000 (11:58 +0200)
The lines returned by readline() should be freed.

src/client/lldpcli.c

index 1079069e768508db1feda7ba07e7d3157e6d489b..e534343c808349fb7f40d4c5eeeeff6707579131 100644 (file)
@@ -254,7 +254,7 @@ readline(const char *p)
        fflush(stderr);
        if (fgets(line, sizeof(line) - 2, stdin) == NULL)
                return NULL;
-       return line;
+       return strdup(line);
 }
 #endif
 
@@ -579,14 +579,16 @@ main(int argc, char *argv[])
        rl_bind_key('?',  cmd_help);
        rl_bind_key('\t', cmd_complete);
 #endif
-       const char *line;
+       char *line = NULL;
        do {
                if ((line = readline(prompt()))) {
                        int n = parse_and_exec(conn, fmt, line);
-                       (void)n;
+                       if (n != 0) {
 #ifdef HAVE_READLINE_HISTORY
-                       if (n != 0) add_history(line);
+                               add_history(line);
 #endif
+                       }
+                       free(line);
                }
        } while (!must_exit && line != NULL);
        rc = EXIT_SUCCESS;