]> xenbits.xensource.com Git - people/royger/freebsd.git/commitdiff
free/NULL out variables prior to calling strdup to avoid leaking memory
authorngie <ngie@FreeBSD.org>
Sat, 10 Dec 2016 23:26:34 +0000 (23:26 +0000)
committerngie <ngie@FreeBSD.org>
Sat, 10 Dec 2016 23:26:34 +0000 (23:26 +0000)
if arguments are specified more than once with "camcontrol timestamp".

CID: 13668291366831
MFC after: 1 week

sbin/camcontrol/timestamp.c

index 642a15a007f96e746c596fbe242f2be6eeaf40b7..5f3adfe8b9b70c4cc69a6b1752f3f55d20f0a87b 100644 (file)
@@ -336,6 +336,11 @@ timestamp(struct cam_device *device, int argc, char **argv, char *combinedopt,
        int single_arg = 0;
        int do_utc = 0;
 
+#define        FREE(x) do {    \
+       free(x);        \
+       x = NULL;       \
+} while(0)
+
        while ((c = getopt(argc, argv, combinedopt)) != -1) {
                switch (c) {
                case 'r': {
@@ -358,6 +363,7 @@ timestamp(struct cam_device *device, int argc, char **argv, char *combinedopt,
                }
                case 'f': {
                        single_arg++;
+                       FREE(format_string);
                        format_string = strdup(optarg);
                        if (format_string == NULL) {
                                warn("Error allocating memory for format "
@@ -369,6 +375,7 @@ timestamp(struct cam_device *device, int argc, char **argv, char *combinedopt,
                }
                case 'm': {
                        single_arg++;
+                       FREE(format_string);
                        format_string = strdup(MIL);
                        if (format_string == NULL) {
                                warn("Error allocating memory");
@@ -382,6 +389,7 @@ timestamp(struct cam_device *device, int argc, char **argv, char *combinedopt,
                        break;
                }
                case 'T':
+                       FREE(timestamp_string);
                        timestamp_string = strdup(optarg);
                        if (timestamp_string == NULL) {
                                warn("Error allocating memory for format "
@@ -395,6 +403,8 @@ timestamp(struct cam_device *device, int argc, char **argv, char *combinedopt,
                }
        }
 
+#undef FREE
+
        if (action == -1) {
                warnx("Must specify an action, either -r or -s");
                error = 1;