Source-Changes-HG archive

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

[src/trunk]: src/usr.sbin/pwd_mkdb Fix fd leaks in error cases. Found by cppc...



details:   https://anonhg.NetBSD.org/src/rev/d716330d3329
branches:  trunk
changeset: 760401:d716330d3329
user:      wiz <wiz%NetBSD.org@localhost>
date:      Tue Jan 04 10:01:51 2011 +0000

description:
Fix fd leaks in error cases. Found by cppcheck.

diffstat:

 usr.sbin/pwd_mkdb/pwd_mkdb.c |  17 +++++++++++------
 1 files changed, 11 insertions(+), 6 deletions(-)

diffs (47 lines):

diff -r c629289b8af7 -r d716330d3329 usr.sbin/pwd_mkdb/pwd_mkdb.c
--- a/usr.sbin/pwd_mkdb/pwd_mkdb.c      Tue Jan 04 09:58:03 2011 +0000
+++ b/usr.sbin/pwd_mkdb/pwd_mkdb.c      Tue Jan 04 10:01:51 2011 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: pwd_mkdb.c,v 1.52 2010/08/18 08:06:39 christos Exp $   */
+/*     $NetBSD: pwd_mkdb.c,v 1.53 2011/01/04 10:01:51 wiz Exp $        */
 
 /*
  * Copyright (c) 2000, 2009 The NetBSD Foundation, Inc.
@@ -90,7 +90,7 @@
  The NetBSD Foundation, Inc.  All rights reserved.\
   Copyright (c) 1991, 1993, 1994\
  The Regents of the University of California.  All rights reserved.");
-__RCSID("$NetBSD: pwd_mkdb.c,v 1.52 2010/08/18 08:06:39 christos Exp $");
+__RCSID("$NetBSD: pwd_mkdb.c,v 1.53 2011/01/04 10:01:51 wiz Exp $");
 #endif /* not lint */
 
 #if HAVE_NBTOOL_CONFIG_H
@@ -645,19 +645,24 @@
 
        if ((from_fd = open(from, O_RDONLY, 0)) < 0)
                mkpw_error("Cannot open `%s'", from);
-       if ((to_fd = open(to, O_WRONLY | O_CREAT | O_EXCL, mode)) < 0)
+       if ((to_fd = open(to, O_WRONLY | O_CREAT | O_EXCL, mode)) < 0) {
+               (void)close(from_fd);
                mkpw_error("Cannot open `%s'", to);
+       }
        while ((rcount = read(from_fd, buf, MAXBSIZE)) > 0) {
                wcount = write(to_fd, buf, (size_t)rcount);
-               if (rcount != wcount || wcount == -1)
+               if (rcount != wcount || wcount == -1) {
+                       (void)close(from_fd);
+                       (void)close(to_fd);
                        goto on_error;
+               }
        }
 
-       if (rcount < 0)
-               goto on_error;
        close(from_fd);
        if (close(to_fd))
                goto on_error;
+       if (rcount < 0)
+               goto on_error;
        return;
 
 on_error:



Home | Main Index | Thread Index | Old Index