Source-Changes-HG archive

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

[src/trunk]: src/usr.sbin/eeprom Fix fd leak in error case. Found by cppcheck.



details:   https://anonhg.NetBSD.org/src/rev/362f19fbc9d2
branches:  trunk
changeset: 760393:362f19fbc9d2
user:      wiz <wiz%NetBSD.org@localhost>
date:      Tue Jan 04 09:25:21 2011 +0000

description:
Fix fd leak in error case. Found by cppcheck.

diffstat:

 usr.sbin/eeprom/ofhandlers.c   |  18 +++++++++++++-----
 usr.sbin/eeprom/ophandlers.c   |  18 +++++++++++++-----
 usr.sbin/eeprom/prephandlers.c |  14 ++++++++++----
 3 files changed, 36 insertions(+), 14 deletions(-)

diffs (159 lines):

diff -r 48a2b2512635 -r 362f19fbc9d2 usr.sbin/eeprom/ofhandlers.c
--- a/usr.sbin/eeprom/ofhandlers.c      Tue Jan 04 09:16:17 2011 +0000
+++ b/usr.sbin/eeprom/ofhandlers.c      Tue Jan 04 09:25:21 2011 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: ofhandlers.c,v 1.4 2008/04/28 20:24:15 martin Exp $    */
+/*     $NetBSD: ofhandlers.c,v 1.5 2011/01/04 09:25:21 wiz Exp $       */
 
 /*-
  * Copyright (c) 1996 The NetBSD Foundation, Inc.
@@ -106,8 +106,10 @@
                if (strcmp(ex->ex_keyword, keyword) == 0)
                        break;
 
-       if (ioctl(fd, OFIOCGETOPTNODE, (char *)&optnode) < 0)
+       if (ioctl(fd, OFIOCGETOPTNODE, (char *)&optnode) < 0) {
+               (void)close(fd);
                BARF("OFIOCGETOPTNODE", strerror(errno));
+       }
 
        memset(&ofio_buf[0], 0, sizeof(ofio_buf));
        memset(&ofio, 0, sizeof(ofio));
@@ -121,8 +123,10 @@
 
                        ofio.of_buf = &ofio_buf[0];
                        ofio.of_buflen = sizeof(ofio_buf);
-                       if (ioctl(fd, OFIOCGET, (char *)&ofio) < 0)
+                       if (ioctl(fd, OFIOCGET, (char *)&ofio) < 0) {
+                               (void)close(fd);
                                BARF("OFIOCGET", strerror(errno));
+                       }
 
                        if (ofio.of_buflen <= 0) {
                                printf("nothing available for %s\n", keyword);
@@ -142,8 +146,10 @@
                        ofio.of_buflen = strlen(arg);
                }
 
-               if (ioctl(fd, OFIOCSET, (char *)&ofio) < 0)
+               if (ioctl(fd, OFIOCSET, (char *)&ofio) < 0) {
+                       (void)close(fd);
                        BARF("invalid keyword", keyword);
+               }
 
                if (verbose) {
                        printf("new: ");
@@ -155,8 +161,10 @@
        } else {
                ofio.of_buf = &ofio_buf[0];
                ofio.of_buflen = sizeof(ofio_buf);
-               if (ioctl(fd, OFIOCGET, (char *)&ofio) < 0)
+               if (ioctl(fd, OFIOCGET, (char *)&ofio) < 0) {
+                       (void)close(fd);
                        BARF("OFIOCGET", strerror(errno));
+               }
 
                if (ofio.of_buflen <= 0) {
                        (void)snprintf(err_str, sizeof err_str,
diff -r 48a2b2512635 -r 362f19fbc9d2 usr.sbin/eeprom/ophandlers.c
--- a/usr.sbin/eeprom/ophandlers.c      Tue Jan 04 09:16:17 2011 +0000
+++ b/usr.sbin/eeprom/ophandlers.c      Tue Jan 04 09:25:21 2011 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: ophandlers.c,v 1.10 2008/04/28 20:24:15 martin Exp $   */
+/*     $NetBSD: ophandlers.c,v 1.11 2011/01/04 09:25:21 wiz Exp $      */
 
 /*-
  * Copyright (c) 1996 The NetBSD Foundation, Inc.
@@ -114,8 +114,10 @@
                if (strcmp(ex->ex_keyword, keyword) == 0)
                        break;
 
-       if (ioctl(fd, OPIOCGETOPTNODE, (char *)&optnode) < 0)
+       if (ioctl(fd, OPIOCGETOPTNODE, (char *)&optnode) < 0) {
+               (void)close(fd);
                BARF("OPIOCGETOPTNODE", strerror(errno));
+       }
 
        memset(&opio_buf[0], 0, sizeof(opio_buf));
        memset(&opio, 0, sizeof(opio));
@@ -129,8 +131,10 @@
 
                        opio.op_buf = &opio_buf[0];
                        opio.op_buflen = sizeof(opio_buf);
-                       if (ioctl(fd, OPIOCGET, (char *)&opio) < 0)
+                       if (ioctl(fd, OPIOCGET, (char *)&opio) < 0) {
+                               (void)close(fd);
                                BARF("OPIOCGET", strerror(errno));
+                       }
 
                        if (opio.op_buflen <= 0) {
                                printf("nothing available for %s\n", keyword);
@@ -150,8 +154,10 @@
                        opio.op_buflen = strlen(arg);
                }
 
-               if (ioctl(fd, OPIOCSET, (char *)&opio) < 0)
+               if (ioctl(fd, OPIOCSET, (char *)&opio) < 0) {
+                       (void)close(fd);
                        BARF("invalid keyword", keyword);
+               }
 
                if (verbose) {
                        printf("new: ");
@@ -163,8 +169,10 @@
        } else {
                opio.op_buf = &opio_buf[0];
                opio.op_buflen = sizeof(opio_buf);
-               if (ioctl(fd, OPIOCGET, (char *)&opio) < 0)
+               if (ioctl(fd, OPIOCGET, (char *)&opio) < 0) {
+                       (void)close(fd);
                        BARF("OPIOCGET", strerror(errno));
+               }
 
                if (opio.op_buflen <= 0) {
                        (void)snprintf(err_str, sizeof err_str,
diff -r 48a2b2512635 -r 362f19fbc9d2 usr.sbin/eeprom/prephandlers.c
--- a/usr.sbin/eeprom/prephandlers.c    Tue Jan 04 09:16:17 2011 +0000
+++ b/usr.sbin/eeprom/prephandlers.c    Tue Jan 04 09:25:21 2011 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: prephandlers.c,v 1.2 2008/04/28 20:24:15 martin Exp $  */
+/*     $NetBSD: prephandlers.c,v 1.3 2011/01/04 09:25:21 wiz Exp $     */
 
 /*-
  * Copyright (c) 2006 The NetBSD Foundation, Inc.
@@ -111,8 +111,10 @@
 
                        nvio.pnv_buf = &nvio_buf[0];
                        nvio.pnv_buflen = sizeof(nvio_buf);
-                       if (ioctl(fd, PNVIOCGET, (char *) &nvio) < 0)
+                       if (ioctl(fd, PNVIOCGET, (char *) &nvio) < 0) {
+                               (void)close(fd);
                                BARF("PNVIOCGET", strerror(errno));
+                       }
 
                        if (nvio.pnv_buflen <= 0) {
                                printf("nothing available for %s\n", keyword);
@@ -131,8 +133,10 @@
                        nvio.pnv_buflen = strlen(arg);
                }
 
-               if (ioctl(fd, PNVIOCSET, (char *) &nvio) < 0)
+               if (ioctl(fd, PNVIOCSET, (char *) &nvio) < 0) {
+                       (void)close(fd);
                        BARF("invalid keyword", keyword);
+               }
 
                if (verbose) {
                        printf("new: ");
@@ -144,8 +148,10 @@
        } else {
                nvio.pnv_buf = &nvio_buf[0];
                nvio.pnv_buflen = sizeof(nvio_buf);
-               if (ioctl(fd, PNVIOCGET, (char *) &nvio) < 0)
+               if (ioctl(fd, PNVIOCGET, (char *) &nvio) < 0) {
+                       (void)close(fd);
                        BARF("PNVIOCGET", strerror(errno));
+               }
 
                if (nvio.pnv_buflen <= 0) {
                        (void) snprintf(err_str, sizeof err_str,



Home | Main Index | Thread Index | Old Index