free/NULL out variables prior to calling strdup to avoid leaking memory
if arguments are specified more than once with "camcontrol timestamp". CID: 1366829, 1366831 MFC after: 1 week
This commit is contained in:
parent
a95a411a28
commit
09c987d4c9
@ -336,6 +336,11 @@ timestamp(struct cam_device *device, int argc, char **argv, char *combinedopt,
|
|||||||
int single_arg = 0;
|
int single_arg = 0;
|
||||||
int do_utc = 0;
|
int do_utc = 0;
|
||||||
|
|
||||||
|
#define FREE(x) do { \
|
||||||
|
free(x); \
|
||||||
|
x = NULL; \
|
||||||
|
} while(0)
|
||||||
|
|
||||||
while ((c = getopt(argc, argv, combinedopt)) != -1) {
|
while ((c = getopt(argc, argv, combinedopt)) != -1) {
|
||||||
switch (c) {
|
switch (c) {
|
||||||
case 'r': {
|
case 'r': {
|
||||||
@ -358,6 +363,7 @@ timestamp(struct cam_device *device, int argc, char **argv, char *combinedopt,
|
|||||||
}
|
}
|
||||||
case 'f': {
|
case 'f': {
|
||||||
single_arg++;
|
single_arg++;
|
||||||
|
FREE(format_string);
|
||||||
format_string = strdup(optarg);
|
format_string = strdup(optarg);
|
||||||
if (format_string == NULL) {
|
if (format_string == NULL) {
|
||||||
warn("Error allocating memory for format "
|
warn("Error allocating memory for format "
|
||||||
@ -369,6 +375,7 @@ timestamp(struct cam_device *device, int argc, char **argv, char *combinedopt,
|
|||||||
}
|
}
|
||||||
case 'm': {
|
case 'm': {
|
||||||
single_arg++;
|
single_arg++;
|
||||||
|
FREE(format_string);
|
||||||
format_string = strdup(MIL);
|
format_string = strdup(MIL);
|
||||||
if (format_string == NULL) {
|
if (format_string == NULL) {
|
||||||
warn("Error allocating memory");
|
warn("Error allocating memory");
|
||||||
@ -382,6 +389,7 @@ timestamp(struct cam_device *device, int argc, char **argv, char *combinedopt,
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'T':
|
case 'T':
|
||||||
|
FREE(timestamp_string);
|
||||||
timestamp_string = strdup(optarg);
|
timestamp_string = strdup(optarg);
|
||||||
if (timestamp_string == NULL) {
|
if (timestamp_string == NULL) {
|
||||||
warn("Error allocating memory for format "
|
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) {
|
if (action == -1) {
|
||||||
warnx("Must specify an action, either -r or -s");
|
warnx("Must specify an action, either -r or -s");
|
||||||
error = 1;
|
error = 1;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user