Module Name:    src
Committed By:   christos
Date:           Tue Sep 29 14:27:00 UTC 2015

Modified Files:
        src/usr.bin/usbhidaction: usbhidaction.c

Log Message:
- PR/50291: Fix memory leak
- estrdup/emalloc
- use EXIT_FAILURE instead of 1


To generate a diff of this commit:
cvs rdiff -u -r1.25 -r1.26 src/usr.bin/usbhidaction/usbhidaction.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.bin/usbhidaction/usbhidaction.c
diff -u src/usr.bin/usbhidaction/usbhidaction.c:1.25 src/usr.bin/usbhidaction/usbhidaction.c:1.26
--- src/usr.bin/usbhidaction/usbhidaction.c:1.25	Thu Jan 24 12:46:00 2013
+++ src/usr.bin/usbhidaction/usbhidaction.c	Tue Sep 29 10:27:00 2015
@@ -1,4 +1,4 @@
-/*      $NetBSD: usbhidaction.c,v 1.25 2013/01/24 17:46:00 christos Exp $ */
+/*      $NetBSD: usbhidaction.c,v 1.26 2015/09/29 14:27:00 christos Exp $ */
 
 /*
  * Copyright (c) 2000, 2002 The NetBSD Foundation, Inc.
@@ -31,7 +31,7 @@
 #include <sys/cdefs.h>
 
 #ifndef lint
-__RCSID("$NetBSD: usbhidaction.c,v 1.25 2013/01/24 17:46:00 christos Exp $");
+__RCSID("$NetBSD: usbhidaction.c,v 1.26 2015/09/29 14:27:00 christos Exp $");
 #endif
 
 #include <stdio.h>
@@ -50,6 +50,7 @@ __RCSID("$NetBSD: usbhidaction.c,v 1.25 
 #include <util.h>
 #include <syslog.h>
 #include <signal.h>
+#include <util.h>
 
 static int verbose = 0;
 static int isdemon = 0;
@@ -136,22 +137,23 @@ main(int argc, char **argv)
 
 	if (dev[0] != '/') {
 		(void)snprintf(devnamebuf, sizeof(devnamebuf), "/dev/%s%s",
-			 isdigit((unsigned char)dev[0]) ? "uhid" : "", dev);
+		     isdigit((unsigned char)dev[0]) ? "uhid" : "", dev);
 		dev = devnamebuf;
 	}
 
 	if (demon && conf[0] != '/')
-		errx(1, "config file must have an absolute path, %s", conf);
+		errx(EXIT_FAILURE,
+		    "config file must have an absolute path, %s", conf);
 
 	fd = open(dev, O_RDWR | O_CLOEXEC);
 	if (fd < 0)
-		err(1, "%s", dev);
+		err(EXIT_FAILURE, "%s", dev);
 
 	if (ioctl(fd, USB_GET_REPORT_ID, &reportid) < 0)
 		reportid = -1;
 	repd = hid_get_report_desc(fd);
 	if (repd == NULL)
-		err(1, "hid_get_report_desc() failed");
+		err(EXIT_FAILURE, "hid_get_report_desc() failed");
 
 	commands = parse_conf(conf, repd, reportid, ignore);
 
@@ -159,14 +161,14 @@ main(int argc, char **argv)
 
 	if (verbose)
 		(void)printf("report size %d\n", sz);
-	if (sz > (int)sizeof(buf))
-		errx(1, "report too large");
+	if ((size_t)sz > sizeof(buf))
+		errx(EXIT_FAILURE, "report too large");
 
 	(void)signal(SIGHUP, sighup);
 
 	if (demon) {
 		if (daemon(0, 0) < 0)
-			err(1, "daemon()");
+			err(EXIT_FAILURE, "daemon()");
 		(void)pidfile(NULL);
 		isdemon = 1;
 	}
@@ -181,13 +183,13 @@ main(int argc, char **argv)
 		}
 		if (n < 0) {
 			if (verbose)
-				err(1, "read");
+				err(EXIT_FAILURE, "read");
 			else
-				exit(1);
+				exit(EXIT_FAILURE);
 		}
 #if 0
 		if (n != sz) {
-			err(2, "read size");
+			err(EXIT_FAILURE, "read size");
 		}
 #endif
 		for (cmd = commands; cmd; cmd = cmd->next) {
@@ -213,7 +215,7 @@ usage(void)
 
 	(void)fprintf(stderr, "usage: %s -c config_file [-d] -f hid_dev "
 		"[-i] [-t table] [-v]\n", getprogname());
-	exit(1);
+	exit(EXIT_FAILURE);
 }
 
 static int
@@ -242,7 +244,7 @@ parse_conf(const char *conf, report_desc
 	
 	f = fopen(conf, "r");
 	if (f == NULL)
-		err(1, "%s", conf);
+		err(EXIT_FAILURE, "%s", conf);
 
 	cmds = NULL;
 	for (line = 1; ; line++) {
@@ -268,14 +270,12 @@ parse_conf(const char *conf, report_desc
 				(void)fclose(f);
 				return (NULL);
 			} else {
-				errx(1, "config file `%s', line %d,"
+				errx(EXIT_FAILURE, "config file `%s', line %d,"
 				     ", syntax error: %s", conf, line, buf);
 			}
 		}
 
-		cmd = malloc(sizeof *cmd);
-		if (cmd == NULL)
-			err(1, "malloc failed");
+		cmd = emalloc(sizeof *cmd);
 		cmd->next = cmds;
 		cmds = cmd;
 		cmd->line = line;
@@ -294,9 +294,10 @@ parse_conf(const char *conf, report_desc
 					(void)fclose(f);
 					return (NULL);
 				} else {
-					errx(1, "config file `%s', line %d, "
-					     "bad value: %s\n",
-					     conf, line, value);
+					errx(EXIT_FAILURE,
+					    "config file `%s', line %d, "
+					    "bad value: %s\n",
+					    conf, line, value);
 				}
 			}
 		}
@@ -375,15 +376,15 @@ parse_conf(const char *conf, report_desc
 			(void)fclose(f);
 			return (NULL);
 		} else {
-			errx(1, "config file `%s', line %d, HID item "
-			     "not found: `%s'", conf, line, name);
+			errx(EXIT_FAILURE, "config file `%s', line %d,"
+			    " HID item not found: `%s'", conf, line, name);
 		}
 
 	foundhid:
 		hid_end_parse(d);
 		cmd->item = h;
-		cmd->name = strdup(name);
-		cmd->action = strdup(action);
+		cmd->name = estrdup(name);
+		cmd->action = estrdup(action);
 		if (range) {
 			if (cmd->value == 1)
 				cmd->value = u - lo;
@@ -413,7 +414,7 @@ docmd(struct command *cmd, int value, co
 			if (isdigit((unsigned char)*p)) {
 				n = strtol(p, &p, 10) - 1;
 				if (n >= 0 && n < argc) {
-					(void)strncpy(q, argv[n], len);
+					(void)strlcpy(q, argv[n], len);
 					q += strlen(q);
 				}
 			} else if (*p == 'V') {
@@ -422,11 +423,11 @@ docmd(struct command *cmd, int value, co
 				q += strlen(q);
 			} else if (*p == 'N') {
 				p++;
-				(void)strncpy(q, cmd->name, len);
+				(void)strlcpy(q, cmd->name, len);
 				q += strlen(q);
 			} else if (*p == 'H') {
 				p++;
-				(void)strncpy(q, hid, len);
+				(void)strlcpy(q, hid, len);
 				q += strlen(q);
 			} else if (*p) {
 				*q++ = *p++;
@@ -445,13 +446,21 @@ docmd(struct command *cmd, int value, co
 }
 
 static void
+freecommand(struct command *cmd)
+{
+	free(cmd->name);
+	free(cmd->action);
+	free(cmd);
+}
+
+static void
 freecommands(struct command *cmd)
 {
 	struct command *next;
 
 	while (cmd) {
 		next = cmd->next;
-		free(cmd);
+		freecommand(cmd);
 		cmd = next;
 	}
 }

Reply via email to