commit a26d541a6a97723666f0b25697ddd36b34b8d08f
parent 0eeeb0505fb7fd62a4ac1be9bfeac0070fd808c4
Author: Jacob R. Edwards <jacobouno@protonmail.com>
Date: Mon, 21 Dec 2020 16:41:02 -0800
Fix strtorange()
Diffstat:
M | main.c | | | 23 | ++++++++++------------- |
1 file changed, 10 insertions(+), 13 deletions(-)
diff --git a/main.c b/main.c
@@ -106,7 +106,7 @@ exists(const char *path)
}
int
-strtorange(unsigned int min, int max, const char *s)
+strtorange(unsigned int *r, unsigned int min, unsigned int max, const char *s)
{
unsigned long n;
char *ep;
@@ -116,12 +116,12 @@ strtorange(unsigned int min, int max, const char *s)
warnc(EINVAL, "not a number '%s'", s);
return -1;
} else if ((errno == ERANGE && n == ULONG_MAX) ||
- n < min || n > max) {
+ n < min || n > max || n > UINT_MAX || n < UINT_MIN) {
warnc(ERANGE, "'%s'", s);
return -1;
}
-
- return n;
+ *r = n;
+ return 0;
}
/* `path` old path, `newpath` new path. Result put into `path`. */
@@ -423,15 +423,14 @@ cgoto(int argc, const char **argv, int depth, const char *tmpdir, const char *ho
int
cback(int argc, const char **argv)
{
- int back;
+ unsigned int back;
wunused(1, argc, argv);
if (argc == 0)
return 1; /* back `1`, not an error */
- back = strtorange(1, INT_MAX, argv[0]);
- if (back < 0)
+ if (strtorange(&back, 1, INT_MAX, argv[0]))
return 0;
return back;
}
@@ -503,8 +502,7 @@ cfetch(int argc, const char **argv, const char *cache, const char *host, const c
FILE *fp;
const char *output;
char *line;
- unsigned int i;
- int n;
+ unsigned int i, n;
int status;
if (argc == 0) {
@@ -513,9 +511,8 @@ cfetch(int argc, const char **argv, const char *cache, const char *host, const c
}
wunused(2, argc, argv);
- n = strtorange(0, INT_MAX, argv[0]);
- if (n < 0)
- return 0;
+ if (strtorange(&n, 0, INT_MAX, argv[0]))
+ return 1;
if (argv[1])
output = argv[1];
@@ -526,7 +523,7 @@ cfetch(int argc, const char **argv, const char *cache, const char *host, const c
return 1;
i = 0;
- line = getlines(&i, (unsigned int *)&n, 1, fp);
+ line = getlines(&i, &n, 1, fp);
if (line == NULL) {
if (!ferror(fp))
warnx("%d: Out of range.", n);