From 617dae995c1a830096c18720aaf79623c282aa1e Mon Sep 17 00:00:00 2001 From: Alejandro Colomar Date: Sun, 30 Jun 2024 03:00:40 +0200 Subject: [PATCH] lib/atoi/strtoi/, tests/: strto[iu]_(): 1 is an invalid base Signed-off-by: Alejandro Colomar --- lib/atoi/strtoi/strtoi.h | 2 +- lib/atoi/strtoi/strtou.h | 2 +- tests/unit/test_atoi_strtoi.c | 10 ++++++++++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/atoi/strtoi/strtoi.h b/lib/atoi/strtoi/strtoi.h index a8f6f5408..8fa6b62f1 100644 --- a/lib/atoi/strtoi/strtoi.h +++ b/lib/atoi/strtoi/strtoi.h @@ -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)); } diff --git a/lib/atoi/strtoi/strtou.h b/lib/atoi/strtoi/strtou.h index 5ce7bd6a1..7ca6b346f 100644 --- a/lib/atoi/strtoi/strtou.h +++ b/lib/atoi/strtoi/strtou.h @@ -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); } diff --git a/tests/unit/test_atoi_strtoi.c b/tests/unit/test_atoi_strtoi.c index 02d62cf3d..782c8a4c6 100644 --- a/tests/unit/test_atoi_strtoi.c +++ b/tests/unit/test_atoi_strtoi.c @@ -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); @@ -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); @@ -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);