Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/lib/libpuffs puffs_null_node_fsync: don't leak directory handle



details:   https://anonhg.NetBSD.org/src/rev/a4fc06643a8c
branches:  trunk
changeset: 460754:a4fc06643a8c
user:      tnn <tnn%NetBSD.org@localhost>
date:      Sat Nov 02 18:14:36 2019 +0000

description:
puffs_null_node_fsync: don't leak directory handle

Directory handles returned by opendir must be closed by closedir.
Also directory(3) says we mustn't close(2) descriptors returned by dirfd(3)

diffstat:

 lib/libpuffs/null.c |  21 +++++++++++++--------
 1 files changed, 13 insertions(+), 8 deletions(-)

diffs (61 lines):

diff -r c86b7a9ebda1 -r a4fc06643a8c lib/libpuffs/null.c
--- a/lib/libpuffs/null.c       Sat Nov 02 17:13:20 2019 +0000
+++ b/lib/libpuffs/null.c       Sat Nov 02 18:14:36 2019 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: null.c,v 1.34 2019/09/23 12:00:57 christos Exp $       */
+/*     $NetBSD: null.c,v 1.35 2019/11/02 18:14:36 tnn Exp $    */
 
 /*
  * Copyright (c) 2007  Antti Kantee.  All Rights Reserved.
@@ -27,7 +27,7 @@
 
 #include <sys/cdefs.h>
 #if !defined(lint)
-__RCSID("$NetBSD: null.c,v 1.34 2019/09/23 12:00:57 christos Exp $");
+__RCSID("$NetBSD: null.c,v 1.35 2019/11/02 18:14:36 tnn Exp $");
 #endif /* !lint */
 
 /*
@@ -412,20 +412,25 @@
        int fd, rv;
        int fflags;
        struct stat sb;
+       DIR *dirp;
 
        rv = 0;
        if (stat(PNPATH(pn), &sb) == -1)
                return errno;
        if (S_ISDIR(sb.st_mode)) {
-               DIR *dirp;
-               if ((dirp = opendir(PNPATH(pn))) == 0)
+               if ((dirp = opendir(PNPATH(pn))) == NULL)
                        return errno;
                fd = dirfd(dirp);
-               if (fd == -1)
-                       return errno;
+               if (fd == -1) {
+                       rv = errno;
+                       closedir(dirp);
+                       return rv;
+               }
 
                if (fsync(fd) == -1)
                        rv = errno;
+
+               closedir(dirp);
        } else {
                fd = writeableopen(PNPATH(pn));
                if (fd == -1)
@@ -440,10 +445,10 @@
 
                if (fsync_range(fd, fflags, offlo, offhi - offlo) == -1)
                        rv = errno;
+
+               close(fd);
        }
 
-       close(fd);
-
        return rv;
 }
 



Home | Main Index | Thread Index | Old Index