]> xenbits.xensource.com Git - unikraft/unikraft.git/commitdiff
lib/vfscore: Fix possible memleak in getcwd
authorVlad-Andrei Badoiu <vlad_andrei.badoiu@upb.ro>
Sat, 21 Nov 2020 15:07:31 +0000 (17:07 +0200)
committerFelipe Huici <felipe.huici@neclab.eu>
Wed, 9 Dec 2020 20:02:21 +0000 (21:02 +0100)
If path is null and the memory is allocated by us then if size < len
we exit the call without freeing the memory. We solve this by moving the
check for sizer earlier.

Signed-off-by: Vlad-Andrei Badoiu <vlad_andrei.badoiu@upb.ro>
Reviewed-by: Felipe Huici <felipe.huici@neclab.eu>
lib/vfscore/main.c

index 72ec0d6f48ed7f5ba0b87d7c71b7e812c4c18a3c..7f45ba13fef3519a45720978323b8364ea4fb48e 100644 (file)
@@ -1353,6 +1353,11 @@ char *getcwd(char *path, size_t size)
        size_t len = strlen(t->t_cwd) + 1;
        int error;
 
+       if (size < len) {
+               error = ERANGE;
+               goto out_errno;
+       }
+
        if (!path) {
                if (!size)
                        size = len;
@@ -1368,11 +1373,6 @@ char *getcwd(char *path, size_t size)
                }
        }
 
-       if (size < len) {
-               error = ERANGE;
-               goto out_errno;
-       }
-
        memcpy(path, t->t_cwd, len);
        trace_vfs_getcwd_ret(path);
        return path;