parse-util: make sure "-0.-0s" is not considered a valid time specification
Did I mention that strtoll() is super-hard to use properly?
This commit is contained in:
+15
-9
@@ -996,10 +996,10 @@ int parse_time(const char *t, usec_t *usec, usec_t default_unit) {
|
||||
}
|
||||
|
||||
for (;;) {
|
||||
long long l, z = 0;
|
||||
char *e;
|
||||
unsigned n = 0;
|
||||
usec_t multiplier = default_unit, k;
|
||||
long long l, z = 0;
|
||||
unsigned n = 0;
|
||||
char *e;
|
||||
|
||||
p += strspn(p, WHITESPACE);
|
||||
|
||||
@@ -1010,6 +1010,9 @@ int parse_time(const char *t, usec_t *usec, usec_t default_unit) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (*p == '-') /* Don't allow "-0" */
|
||||
return -ERANGE;
|
||||
|
||||
errno = 0;
|
||||
l = strtoll(p, &e, 10);
|
||||
if (errno > 0)
|
||||
@@ -1020,14 +1023,15 @@ int parse_time(const char *t, usec_t *usec, usec_t default_unit) {
|
||||
if (*e == '.') {
|
||||
char *b = e + 1;
|
||||
|
||||
if (*b == '-') /* Don't allow 0.-0 */
|
||||
return -EINVAL;
|
||||
|
||||
errno = 0;
|
||||
z = strtoll(b, &e, 10);
|
||||
if (errno > 0)
|
||||
return -errno;
|
||||
|
||||
if (z < 0)
|
||||
return -ERANGE;
|
||||
|
||||
if (e == b)
|
||||
return -EINVAL;
|
||||
|
||||
@@ -1144,26 +1148,28 @@ int parse_nsec(const char *t, nsec_t *nsec) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (*p == '-')
|
||||
return -ERANGE;
|
||||
|
||||
errno = 0;
|
||||
l = strtoll(p, &e, 10);
|
||||
|
||||
if (errno > 0)
|
||||
return -errno;
|
||||
|
||||
if (l < 0)
|
||||
return -ERANGE;
|
||||
|
||||
if (*e == '.') {
|
||||
char *b = e + 1;
|
||||
|
||||
if (*b == '-')
|
||||
return -EINVAL;
|
||||
|
||||
errno = 0;
|
||||
z = strtoll(b, &e, 10);
|
||||
if (errno > 0)
|
||||
return -errno;
|
||||
|
||||
if (z < 0)
|
||||
return -ERANGE;
|
||||
|
||||
if (e == b)
|
||||
return -EINVAL;
|
||||
|
||||
|
||||
@@ -42,6 +42,12 @@ static void test_parse_sec(void) {
|
||||
assert_se(parse_sec(" . ", &u) < 0);
|
||||
assert_se(parse_sec(" 5. ", &u) < 0);
|
||||
assert_se(parse_sec(".s ", &u) < 0);
|
||||
assert_se(parse_sec("-5s ", &u) < 0);
|
||||
assert_se(parse_sec("-0.3s ", &u) < 0);
|
||||
assert_se(parse_sec("-0.0s ", &u) < 0);
|
||||
assert_se(parse_sec("-0.-0s ", &u) < 0);
|
||||
assert_se(parse_sec("0.-0s ", &u) < 0);
|
||||
assert_se(parse_sec("3.-0s ", &u) < 0);
|
||||
assert_se(parse_sec(" infinity .7", &u) < 0);
|
||||
assert_se(parse_sec(".3 infinity", &u) < 0);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user