diff options
author | Daniel Roberson <daniel@planethacker.net> | 2018-04-18 21:52:01 -0700 |
---|---|---|
committer | Tobias Klauser <tklauser@distanz.ch> | 2018-04-19 16:13:56 +0200 |
commit | 15f78c073276ad49b3541215b2a302b1a32c9f2b (patch) | |
tree | 3e2175cb2b44dd79d017ecce4c1e307032ace840 /staging | |
parent | 65ba0860baa53670e79211edbd047a8080ec1f8a (diff) |
mausezahn: fix strtok() segfault if s or m are missing
The -d flag will result in a segmentation fault if 'm' or 's' are
specified without a numeric value.
Example: mausezahn -d m ### Results in a crash
Signed-off-by: Daniel Roberson <daniel@planethacker.net>
Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
Diffstat (limited to 'staging')
-rw-r--r-- | staging/mausezahn.c | 28 |
1 files changed, 21 insertions, 7 deletions
diff --git a/staging/mausezahn.c b/staging/mausezahn.c index 4f312fd..d3d037b 100644 --- a/staging/mausezahn.c +++ b/staging/mausezahn.c @@ -396,6 +396,8 @@ int getopts (int argc, char *argv[]) FILE *afp; char hexpld[MAX_PAYLOAD_SIZE*2]; int hexpld_specified=0; + long delay; + char unit; opterr = 1; // let getopt print error message if necessary @@ -458,13 +460,25 @@ int getopts (int argc, char *argv[]) break; case 'd': errno=0; - // determine whether seconds or msecs are used - // default is usec!!! - time_factor=1; - if (exists(optarg,"s") || exists(optarg,"sec")) time_factor=1000000; - if (exists(optarg,"m") || exists(optarg,"msec")) time_factor=1000; - dum = strtok(optarg,"ms"); - tx.delay = strtol(dum, (char **)NULL, 10) * time_factor; + time_factor=0; + delay=0; + unit='u'; // default is usecs + if (sscanf(optarg, "%ld%c", &delay, &unit) == EOF) { + perror("sscanf"); + return (-1); + } + if (delay < 0) { + fprintf(stderr, " Incorrect delay format\n"); + return(-1); + } + if (unit == 's') time_factor=1000000; // seconds + else if (unit == 'm') time_factor=1000; // msecs + else if (unit == 'u') time_factor=1; // usecs + else { + fprintf(stderr, " Incorrect delay format\n"); + return(-1); + } + tx.delay = delay * time_factor; if ((errno == ERANGE && (tx.delay == LONG_MAX || tx.delay == LONG_MIN)) || (errno != 0 && tx.delay == 0)) { perror("strtol"); |