]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
script: fix command and command_norm memory leaks
authorKarel Zak <kzak@redhat.com>
Tue, 7 Apr 2026 13:25:17 +0000 (15:25 +0200)
committerKarel Zak <kzak@redhat.com>
Tue, 7 Apr 2026 15:00:52 +0000 (17:00 +0200)
Always own ctl.command by using xstrdup() for the -c optarg path
(the -- path already allocates via ul_strv_join), and free both
ctl.command and ctl.command_norm at exit.

Signed-off-by: Karel Zak <kzak@redhat.com>
term-utils/script.c

index 0a1773e9e823e62ad811dc647fb48ae040fdc98a..d04737eb5ffbd32a73ace2d05214b12dac1e69a2 100644 (file)
@@ -133,7 +133,7 @@ struct script_control {
 
        const char *ttyname;
        const char *ttytype;
-       const char *command;
+       char *command;
        char *command_norm;     /* normalized (without \n) */
        int ttycols;
        int ttylines;
@@ -840,7 +840,9 @@ int main(int argc, char **argv)
                        ctl.append = 1;
                        break;
                case 'c':
-                       ctl.command = optarg;
+                       free(ctl.command);
+                       ctl.command = xstrdup(optarg);
+                       free(ctl.command_norm);
                        ctl.command_norm = xstrdup(ctl.command);
                        ul_strrep(ctl.command_norm, '\n', ' ');
                        break;
@@ -1115,6 +1117,8 @@ done:
                utempter_remove_record(ul_pty_get_childfd(ctl.pty));
 #endif
        ul_free_pty(ctl.pty);
+       free(ctl.command);
+       free(ctl.command_norm);
 
        /* default exit code */
        rc = rc ? EXIT_FAILURE : EXIT_SUCCESS;