Fix memory leak, call strdup() as late as possible, found by Coverity

Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
This commit is contained in:
Joachim Nilsson committed 2020-01-06 22:03:01 +01:00
1 parent 238f63c0d3
commit a8c071c5ef
1 file changed
+3 -9
+3 -9
View File
@@ -168,7 +168,7 @@ int tty_register(char *line, struct rlimit rlimit[], char *file)
if (!strncmp(args[i], "tty", 3)) if (!strncmp(args[i], "tty", 3))
dev = args[i]; dev = args[i];
if (!access(args[i], X_OK)) if (!access(args[i], X_OK))
cmd = strdup(args[i]); cmd = args[i];
/* The first arg must be one of the above */ /* The first arg must be one of the above */
continue; continue;
@@ -193,9 +193,6 @@ int tty_register(char *line, struct rlimit rlimit[], char *file)
if (!dev) { if (!dev) {
error: error:
_e("Incomplete or non-existing TTY device given, cannot register."); _e("Incomplete or non-existing TTY device given, cannot register.");
if (cmd)
free(cmd);
return errno = EINVAL; return errno = EINVAL;
} }
@@ -225,11 +222,8 @@ again:
entry = tty_find(dev); entry = tty_find(dev);
if (!entry) { if (!entry) {
entry = calloc(1, sizeof(*entry)); entry = calloc(1, sizeof(*entry));
if (!entry) { if (!entry)
if (cmd)
free(cmd);
return errno = ENOMEM; return errno = ENOMEM;
}
insert = 1; insert = 1;
} else { } else {
if (entry->cmd) { if (entry->cmd) {
@@ -261,7 +255,7 @@ again:
tok = cmd; tok = cmd;
else else
tok++; tok++;
entry->cmd = cmd; entry->cmd = strdup(cmd);
args[1] = strdup(tok); args[1] = strdup(tok);
for (i = 1; i < num; i++) { for (i = 1; i < num; i++) {