]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lib/env: fix memory leak [coverity scan]
authorKarel Zak <kzak@redhat.com>
Thu, 9 Mar 2023 12:10:41 +0000 (13:10 +0100)
committerKarel Zak <kzak@redhat.com>
Thu, 9 Mar 2023 12:10:41 +0000 (13:10 +0100)
Signed-off-by: Karel Zak <kzak@redhat.com>
lib/env.c

index 5c385f934b91f1272472e2f24189adf269761558..2ccabff6fc2ae0e9aa25dc516578180299659b46 100644 (file)
--- a/lib/env.c
+++ b/lib/env.c
@@ -89,19 +89,22 @@ static struct ul_env_list *env_list_add(struct ul_env_list *ls0, const char *str
 */
 struct ul_env_list *env_from_fd(int fd)
 {
-       char *buf = NULL;
+       char *buf = NULL, *p;
        size_t rc = 0;
        struct ul_env_list *ls = NULL;
 
        if ((rc = read_all_alloc(fd, &buf)) < 1)
                return NULL;
        buf[rc] = '\0';
+       p = buf;
 
        while (rc > 0) {
-               ls = env_list_add(ls, buf);
-               buf += strlen(buf) + 1;
-               rc -= strlen(buf) + 1;
+               ls = env_list_add(ls, p);
+               p += strlen(p) + 1;
+               rc -= strlen(p) + 1;
        }
+
+       free(buf);
        return ls;
 }