Skip to content

Commit

Permalink
lib/atoi/strtoi/, tests/: strto[iu]_(): 1 is an invalid base
Browse files Browse the repository at this point in the history
Signed-off-by: Alejandro Colomar <alx@kernel.org>
  • Loading branch information
alejandro-colomar committed Jun 30, 2024
1 parent d732c33 commit 617dae9
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/atoi/strtoi/strtoi.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ strtoi_(const char *s, char **restrict endp, int base,
if (status == NULL)
status = &st;

if (base != 0 && (base < 0 || base > 36)) {
if (base != 0 && (base < 2 || base > 36)) {
*status = EINVAL;
return MAX(min, MIN(max, 0));
}
Expand Down
2 changes: 1 addition & 1 deletion lib/atoi/strtoi/strtou.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ strtou_(const char *s, char **restrict endp, int base,
if (status == NULL)
status = &st;

if (base != 0 && (base < 0 || base > 36)) {
if (base != 0 && (base < 2 || base > 36)) {
*status = EINVAL;
return MAX(min, 0);
}
Expand Down
10 changes: 10 additions & 0 deletions tests/unit/test_atoi_strtoi.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ test_strtoi(void **state)
assert_true(strtoi_("42", NULL, -1, 1, 2, &status) == 1);
assert_true(status == EINVAL);

assert_true(strtoi_("42", NULL, 1, 1, 2, &status) == 1);
assert_true(status == EINVAL);

assert_true(strtoi_("40", &end, 5, INTMAX_MIN, INTMAX_MAX, &status) == 20);
assert_true(status == 0);
assert_true(strcmp(end, "") == 0);
Expand Down Expand Up @@ -84,6 +87,9 @@ test_strtou(void **state)
assert_true(strtou_("42", NULL, -1, 1, 2, &status) == 1);
assert_true(status == EINVAL);

assert_true(strtou_("42", NULL, 1, 1, 2, &status) == 1);
assert_true(status == EINVAL);

assert_true(strtou_("40", &end, 5, 0, UINTMAX_MAX, &status) == 20);
assert_true(status == 0);
assert_true(strcmp(end, "") == 0);
Expand Down Expand Up @@ -123,6 +129,10 @@ test_strtou_noneg(void **state)
== 1);
assert_true(status == EINVAL);

assert_true(strtou_noneg("42", NULL, 1, 1, 2, &status)
== 1);
assert_true(status == EINVAL);

assert_true(strtou_noneg("40", &end, 5, 0, UINTMAX_MAX, &status)
== 20);
assert_true(status == 0);
Expand Down

0 comments on commit 617dae9

Please sign in to comment.