From 78929521696ab99b9ab76b235c96718eb161e85e Mon Sep 17 00:00:00 2001 From: wiz Date: Tue, 4 Jan 2011 09:25:21 +0000 Subject: [PATCH] Fix fd leak in error case. Found by cppcheck. --- 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(-) diff --git a/usr.sbin/eeprom/ofhandlers.c b/usr.sbin/eeprom/ofhandlers.c index 4379bdc22135..c793eb979cc8 100644 --- a/usr.sbin/eeprom/ofhandlers.c +++ b/usr.sbin/eeprom/ofhandlers.c @@ -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 @@ of_handler(keyword, arg) 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 @@ of_handler(keyword, arg) 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 @@ of_handler(keyword, arg) 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 @@ of_handler(keyword, arg) } 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 --git a/usr.sbin/eeprom/ophandlers.c b/usr.sbin/eeprom/ophandlers.c index 0b39e01cf7d6..3b25c29d21f3 100644 --- a/usr.sbin/eeprom/ophandlers.c +++ b/usr.sbin/eeprom/ophandlers.c @@ -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 @@ op_handler(keyword, arg) 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 @@ op_handler(keyword, arg) 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 @@ op_handler(keyword, arg) 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 @@ op_handler(keyword, arg) } 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 --git a/usr.sbin/eeprom/prephandlers.c b/usr.sbin/eeprom/prephandlers.c index 07ff5847a7d7..2a8d4d1a5984 100644 --- a/usr.sbin/eeprom/prephandlers.c +++ b/usr.sbin/eeprom/prephandlers.c @@ -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 @@ prep_handler(char *keyword, char *arg) 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 @@ out: 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 @@ out: } 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,