Source-Changes-HG archive

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

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



details:   https://anonhg.NetBSD.org/src/rev/27ca1a4bd889
branches:  trunk
changeset: 760395:27ca1a4bd889
user:      wiz <wiz%NetBSD.org@localhost>
date:      Tue Jan 04 09:32:31 2011 +0000

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

diffstat:

 usr.sbin/grfconfig/grfconfig.c |  21 +++++++++++++++++++--
 usr.sbin/kvm_mkdb/nlist_aout.c |  12 ++++++++----
 2 files changed, 27 insertions(+), 6 deletions(-)

diffs (135 lines):

diff -r 398887c3a1ab -r 27ca1a4bd889 usr.sbin/grfconfig/grfconfig.c
--- a/usr.sbin/grfconfig/grfconfig.c    Tue Jan 04 09:28:44 2011 +0000
+++ b/usr.sbin/grfconfig/grfconfig.c    Tue Jan 04 09:32:31 2011 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: grfconfig.c,v 1.14 2009/04/26 19:24:18 mlelstv Exp $   */
+/*     $NetBSD: grfconfig.c,v 1.15 2011/01/04 09:32:31 wiz Exp $       */
 
 /*-
  * Copyright (c) 1997 The NetBSD Foundation, Inc.
@@ -36,7 +36,7 @@
 #endif /* not lint */
 
 #ifndef lint
-__RCSID("$NetBSD: grfconfig.c,v 1.14 2009/04/26 19:24:18 mlelstv Exp $");
+__RCSID("$NetBSD: grfconfig.c,v 1.15 2011/01/04 09:32:31 wiz Exp $");
 #endif /* not lint */
 
 #include <sys/file.h>
@@ -127,6 +127,7 @@
                if (!(fp = fopen(modefile, "r"))) {
                        printf("grfconfig: Cannot open mode definition "
                            "file.\n");
+                       (void)close(grffd);
                        return (1);
                }
                while (fgets(buf, sizeof(buf), fp)) {
@@ -168,6 +169,8 @@
                        if (i < 14) {
                                printf("grfconfig: too few values in mode "
                                    "definition file:\n %s\n", obuf);
+                               (void)fclose(fp);
+                               (void)close(grffd);
                                return (1);
                        }
 
@@ -193,6 +196,8 @@
                                } else {
                                        printf("grfconfig: Illegal mode "
                                            "number: %s\n", cps[0]);
+                                       (void)fclose(fp);
+                                       (void)close(grffd);
                                        return (1);
                                }
 
@@ -210,6 +215,8 @@
                            (gv->vtotal == 0)) {
                                printf("grfconfig: Illegal value in "
                                    "mode #%d:\n %s\n", gv->mode_num, obuf);
+                               (void)fclose(fp);
+                               (void)close(grffd);
                                return (1);  
                        }
 
@@ -230,6 +237,8 @@
                                            "grfconfig for more information "
                                            "about the new mode definition "
                                            "file.\n");
+                                       (void)fclose(fp);
+                                       (void)close(grffd);
                                        return (1);
                                }
                        }
@@ -255,6 +264,8 @@
                                printf("grfconfig: Illegal flags in "
                                    "mode #%d: %s are both defined!\n",
                                    gv->mode_num, errortext);
+                               (void)fclose(fp);
+                               (void)close(grffd);
                                return (1);
                        }
 
@@ -274,6 +285,8 @@
                                printf(" See the manpage of grfconfig for "
                                    "more information about the new mode "
                                    "definition file.\n");
+                               (void)fclose(fp);
+                               (void)close(grffd);
                                return (1);
                        }
 
@@ -298,6 +311,8 @@
                                printf(" See the manpage of grfconfig for "
                                    "more information about the new mode "
                                    "definition file.\n");
+                               (void)fclose(fp);
+                               (void)close(grffd);
                                return (1);
                        } else if (((gv->vtotal / 2) > lowlim) &&
                            ((gv->vtotal / 2) < uplim)) {
@@ -317,6 +332,8 @@
                                printf(" See the manpage of grfconfig for "
                                    "more information about the new mode "
                                    "definition file.\n");
+                               (void)fclose(fp);
+                               (void)close(grffd);
                                return (1);
                        }
 
diff -r 398887c3a1ab -r 27ca1a4bd889 usr.sbin/kvm_mkdb/nlist_aout.c
--- a/usr.sbin/kvm_mkdb/nlist_aout.c    Tue Jan 04 09:28:44 2011 +0000
+++ b/usr.sbin/kvm_mkdb/nlist_aout.c    Tue Jan 04 09:32:31 2011 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: nlist_aout.c,v 1.8 2003/08/07 11:25:23 agc Exp $ */
+/* $NetBSD: nlist_aout.c,v 1.9 2011/01/04 09:36:13 wiz Exp $ */
 
 /*-
  * Copyright (c) 1990, 1993
@@ -66,7 +66,7 @@
 #if 0
 static char sccsid[] = "from: @(#)nlist.c      8.1 (Berkeley) 6/6/93";
 #else
-__RCSID("$NetBSD: nlist_aout.c,v 1.8 2003/08/07 11:25:23 agc Exp $");
+__RCSID("$NetBSD: nlist_aout.c,v 1.9 2011/01/04 09:36:13 wiz Exp $");
 #endif
 #endif /* not lint */
 
@@ -125,12 +125,16 @@
 
        /* Read in exec structure. */
        nr = read(fd, &ebuf, sizeof(struct exec));
-       if (nr != sizeof(struct exec))
+       if (nr != sizeof(struct exec)) {
+               (void)close(fd);
                return (-1);
+       }
 
        /* Check magic number. */
-       if (N_BADMAG(ebuf))
+       if (N_BADMAG(ebuf)) {
+               (void)close(fd);
                return (-1);
+       }
 
        /*
         * We've recognized it as an a.out binary.  From here



Home | Main Index | Thread Index | Old Index