Source-Changes-HG archive

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

[src/trunk]: src/usr.bin/usbhidaction - PR/50291: Fix memory leak



details:   https://anonhg.NetBSD.org/src/rev/bf5e5ee2c6b2
branches:  trunk
changeset: 810901:bf5e5ee2c6b2
user:      christos <christos%NetBSD.org@localhost>
date:      Tue Sep 29 14:27:00 2015 +0000

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

diffstat:

 usr.bin/usbhidaction/usbhidaction.c |  67 ++++++++++++++++++++----------------
 1 files changed, 38 insertions(+), 29 deletions(-)

diffs (204 lines):

diff -r 454e031fb7a0 -r bf5e5ee2c6b2 usr.bin/usbhidaction/usbhidaction.c
--- a/usr.bin/usbhidaction/usbhidaction.c       Tue Sep 29 09:14:11 2015 +0000
+++ b/usr.bin/usbhidaction/usbhidaction.c       Tue Sep 29 14:27:00 2015 +0000
@@ -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 @@
 #include <util.h>
 #include <syslog.h>
 #include <signal.h>
+#include <util.h>
 
 static int verbose = 0;
 static int isdemon = 0;
@@ -136,22 +137,23 @@
 
        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 @@
 
        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 @@
                }
                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 @@
 
        (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 @@
        
        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 @@
                                (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 @@
                                        (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 @@
                        (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 @@
                        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 @@
                                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 @@
 }
 
 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;
        }
 }



Home | Main Index | Thread Index | Old Index