]> xenbits.xensource.com Git - unikraft/unikraft.git/commitdiff
lib/vfscore: Fix potential vfscore_file mem leak
authorAndrei Tatar <andrei@unikraft.io>
Mon, 28 Aug 2023 19:13:49 +0000 (21:13 +0200)
committerRazvan Deaconescu <razvand@unikraft.io>
Fri, 20 Oct 2023 16:32:28 +0000 (19:32 +0300)
Previously if `fdalloc` failed to allocate a file descriptor, the
function would exit without decrementing the reference count on the
vfscore_file object, leading to a potential memory leak.
This change reorders operations to prevent this scenario.

Signed-off-by: Andrei Tatar <andrei@unikraft.io>
Reviewed-by: Delia Pavel <delia_maria.pavel@stud.acs.upb.ro>
Approved-by: Razvan Deaconescu <razvand@unikraft.io>
GitHub-Closes: #1074

lib/vfscore/fd.c

index 096fa7eea58a7fa442dab249efb77cf5623fd94e..d4a4ddd38d3766863059a3041eb7f4187a3da007 100644 (file)
@@ -188,14 +188,14 @@ int fdalloc(struct vfscore_file *fp, int *newfd)
 {
        int fd, ret = 0;
 
-       fhold(fp);
-
        fd = vfscore_alloc_fd();
        if (fd < 0) {
                ret = fd;
                goto exit;
        }
 
+       fhold(fp);
+
        ret = vfscore_install_fd(fd, fp);
        if (ret)
                fdrop(fp);