Module Name:    src
Committed By:   wiz
Date:           Tue Jan  4 10:01:51 UTC 2011

Modified Files:
        src/usr.sbin/pwd_mkdb: pwd_mkdb.c

Log Message:
Fix fd leaks in error cases. Found by cppcheck.


To generate a diff of this commit:
cvs rdiff -u -r1.52 -r1.53 src/usr.sbin/pwd_mkdb/pwd_mkdb.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.sbin/pwd_mkdb/pwd_mkdb.c
diff -u src/usr.sbin/pwd_mkdb/pwd_mkdb.c:1.52 src/usr.sbin/pwd_mkdb/pwd_mkdb.c:1.53
--- src/usr.sbin/pwd_mkdb/pwd_mkdb.c:1.52	Wed Aug 18 08:06:39 2010
+++ src/usr.sbin/pwd_mkdb/pwd_mkdb.c	Tue Jan  4 10:01:51 2011
@@ -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:

Reply via email to