]> diplodocus.org Git - nmh/commitdiff
readline(3) wrapper: Move free(3) of line to where it's reachable.
authorRalph Corderoy <ralph@inputplus.co.uk>
Sun, 10 Sep 2017 14:03:09 +0000 (15:03 +0100)
committerRalph Corderoy <ralph@inputplus.co.uk>
Sun, 10 Sep 2017 14:03:09 +0000 (15:03 +0100)
Every line returned by readline() has to be freed.  The call to free()
was unreachable because all paths heading towards it veered off,
resulting in a memory leak.  Move the free() to just after where the
line has been used for the last time.  Fixes 3a85e0bc9.

sbr/read_switch_multiword_via_readline.c

index fe84b4e..3d6e881 100644 (file)
@@ -49,6 +49,8 @@ read_switch_multiword_via_readline(char *prompt, struct swit *ansp)
        add_history(ans);
        strncpy(ansbuf, ans, sizeof(ansbuf));
        ansbuf[sizeof(ansbuf) - 1] = '\0';
        add_history(ans);
        strncpy(ansbuf, ans, sizeof(ansbuf));
        ansbuf[sizeof(ansbuf) - 1] = '\0';
+        free(ans);
+
        cpp = brkstring(ansbuf, " ", NULL);
        switch (smatch(*cpp, ansp)) {
            case AMBIGSW:
        cpp = brkstring(ansbuf, " ", NULL);
        switch (smatch(*cpp, ansp)) {
            case AMBIGSW:
@@ -58,10 +60,8 @@ read_switch_multiword_via_readline(char *prompt, struct swit *ansp)
                printf(" -%s unknown. Hit <CR> for help.\n", *cpp);
                continue;
            default:
                printf(" -%s unknown. Hit <CR> for help.\n", *cpp);
                continue;
            default:
-               free(ans);
                return cpp;
        }
                return cpp;
        }
-       free(ans);
     }
 }
 
     }
 }