Krolyxon
Commit daee344

Branches

Refs →

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`
Author
NRK
Date
2022-09-14 06:58 +0200
Parent
e356add
Commit
daee34477a87f7eb97aa537577a48ddcf5a7d30b

+9 −8

2 files changed

  1. M nsxiv.h +1 −1
  2. M options.c +8 −7
M nsxiv.h View
@@ -239,7 +239,7 @@ struct opt {
239239 bool animate;
240240 bool anti_alias;
241241 int gamma;
242− int slideshow;
242+ unsigned int slideshow;
243243 int framerate;
244244
245245 /* window: */
M options.c View
@@ -98,7 +98,8 @@ void parse_options(int argc, char **argv)
9898 { 0 }, /* end */
9999 };
100100
101− int n, opt;
101+ long n, opt;
102+ float f;
102103 char *end, *s;
103104 struct optparse op;
104105 const char scalemodes[] = "dfFwh"; /* must be sorted according to scalemode_t */
@@ -148,7 +149,7 @@ void parse_options(int argc, char **argv)
148149 exit(EXIT_FAILURE);
149150 case 'A':
150151 n = strtol(op.optarg, &end, 0);
151− if (*end != '\0' || n <= 0)
152+ if (*end != '\0' || n <= 0 || n > INT_MAX)
152153 error(EXIT_FAILURE, 0, "Invalid argument for option -A: %s", op.optarg);
153154 _options.framerate = n;
154155 /* fall through */
@@ -172,7 +173,7 @@ void parse_options(int argc, char **argv)
172173 break;
173174 case 'G':
174175 n = strtol(op.optarg, &end, 0);
175− if (*end != '\0')
176+ if (*end != '\0' || n < INT_MIN || n > INT_MAX)
176177 error(EXIT_FAILURE, 0, "Invalid argument for option -G: %s", op.optarg);
177178 _options.gamma = n;
178179 break;
@@ -187,7 +188,7 @@ void parse_options(int argc, char **argv)
187188 break;
188189 case 'n':
189190 n = strtol(op.optarg, &end, 0);
190− if (*end != '\0' || n <= 0)
191+ if (*end != '\0' || n <= 0 || n > INT_MAX)
191192 error(EXIT_FAILURE, 0, "Invalid argument for option -n: %s", op.optarg);
192193 _options.startnum = n - 1;
193194 break;
@@ -207,10 +208,10 @@ void parse_options(int argc, char **argv)
207208 _options.recursive = true;
208209 break;
209210 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)
212213 error(EXIT_FAILURE, 0, "Invalid argument for option -S: %s", op.optarg);
213− _options.slideshow = n;
214+ _options.slideshow = (unsigned int)f;
214215 break;
215216 case 's':
216217 s = strchr(scalemodes, op.optarg[0]);