nsxivdaee344
fix potential truncation of cli arguments (#367)
strtol() returns a `long`, but we're storing the result in an `int` which might end up getting truncated. change `n` to `long` and guard against >INT_MAX arguments in cases where it matters. use a float for storing argument of `-S` change `opt.slideshow` to `unsigned` similar to `img.ss.delay`
+9 −8
2 files changed
| @@ -239,7 +239,7 @@ struct opt { | |||
| 239 | 239 | bool animate; | |
| 240 | 240 | bool anti_alias; | |
| 241 | 241 | int gamma; | |
| 242 | − | int slideshow; | |
| 242 | + | unsigned int slideshow; | |
| 243 | 243 | int framerate; | |
| 244 | 244 | ||
| 245 | 245 | /* window: */ | |
| @@ -98,7 +98,8 @@ void parse_options(int argc, char **argv) | |||
| 98 | 98 | { 0 }, /* end */ | |
| 99 | 99 | }; | |
| 100 | 100 | ||
| 101 | − | int n, opt; | |
| 101 | + | long n, opt; | |
| 102 | + | float f; | |
| 102 | 103 | char *end, *s; | |
| 103 | 104 | struct optparse op; | |
| 104 | 105 | const char scalemodes[] = "dfFwh"; /* must be sorted according to scalemode_t */ | |
| @@ -148,7 +149,7 @@ void parse_options(int argc, char **argv) | |||
| 148 | 149 | exit(EXIT_FAILURE); | |
| 149 | 150 | case 'A': | |
| 150 | 151 | n = strtol(op.optarg, &end, 0); | |
| 151 | − | if (*end != '\0' || n <= 0) | |
| 152 | + | if (*end != '\0' || n <= 0 || n > INT_MAX) | |
| 152 | 153 | error(EXIT_FAILURE, 0, "Invalid argument for option -A: %s", op.optarg); | |
| 153 | 154 | _options.framerate = n; | |
| 154 | 155 | /* fall through */ | |
| @@ -172,7 +173,7 @@ void parse_options(int argc, char **argv) | |||
| 172 | 173 | break; | |
| 173 | 174 | case 'G': | |
| 174 | 175 | n = strtol(op.optarg, &end, 0); | |
| 175 | − | if (*end != '\0') | |
| 176 | + | if (*end != '\0' || n < INT_MIN || n > INT_MAX) | |
| 176 | 177 | error(EXIT_FAILURE, 0, "Invalid argument for option -G: %s", op.optarg); | |
| 177 | 178 | _options.gamma = n; | |
| 178 | 179 | break; | |
| @@ -187,7 +188,7 @@ void parse_options(int argc, char **argv) | |||
| 187 | 188 | break; | |
| 188 | 189 | case 'n': | |
| 189 | 190 | n = strtol(op.optarg, &end, 0); | |
| 190 | − | if (*end != '\0' || n <= 0) | |
| 191 | + | if (*end != '\0' || n <= 0 || n > INT_MAX) | |
| 191 | 192 | error(EXIT_FAILURE, 0, "Invalid argument for option -n: %s", op.optarg); | |
| 192 | 193 | _options.startnum = n - 1; | |
| 193 | 194 | break; | |
| @@ -207,10 +208,10 @@ void parse_options(int argc, char **argv) | |||
| 207 | 208 | _options.recursive = true; | |
| 208 | 209 | break; | |
| 209 | 210 | case 'S': | |
| 210 | − | n = strtof(op.optarg, &end) * 10; | |
| 211 | − | if (*end != '\0' || n <= 0) | |
| 211 | + | f = strtof(op.optarg, &end) * 10.0f; | |
| 212 | + | if (*end != '\0' || f <= 0 || f >= (float)UINT_MAX) | |
| 212 | 213 | error(EXIT_FAILURE, 0, "Invalid argument for option -S: %s", op.optarg); | |
| 213 | − | _options.slideshow = n; | |
| 214 | + | _options.slideshow = (unsigned int)f; | |
| 214 | 215 | break; | |
| 215 | 216 | case 's': | |
| 216 | 217 | s = strchr(scalemodes, op.optarg[0]); | |