From e926de2a8a37c322bd1a71478585881e7c8f56c3 Mon Sep 17 00:00:00 2001 From: Jacek Maksymowicz Date: Mon, 9 Sep 2024 14:53:25 +0200 Subject: [PATCH] libc: re-enable tests for strtoll, strtoull Add test for strtol, etc. to test for case where "0x" prefix is not followed by a hex digit. JIRA: RTOS-869 --- libc/stdlib/stdlib_strto.c | 69 ++++++++++++++++++++++++++------------ 1 file changed, 48 insertions(+), 21 deletions(-) diff --git a/libc/stdlib/stdlib_strto.c b/libc/stdlib/stdlib_strto.c index a382d03e0..8520a9ea5 100644 --- a/libc/stdlib/stdlib_strto.c +++ b/libc/stdlib/stdlib_strto.c @@ -977,35 +977,70 @@ TEST(stdlib_strto, too_long_numbers_float) TEST(stdlib_strto, too_long_numbers_int) { - const char num[] = "2797693134862315708145274237317043567980705675258449965989174768031572607800285387605895586327668781715404589535143824642343213268894641"; + const char num_negative[] = "-2797693134862315708145274237317043567980705675258449965989174768031572607800285387605895586327668781715404589535143824642343213268894641"; + const char *num_positive = num_negative + 1; errno = 0; - strtol(num, NULL, 10); + TEST_ASSERT_EQUAL_INT(LONG_MAX, strtol(num_positive, NULL, 10)); TEST_ASSERT_EQUAL_INT(ERANGE, errno); errno = 0; - strtoul(num, NULL, 10); + TEST_ASSERT_EQUAL_INT(ULONG_MAX, strtoul(num_positive, NULL, 10)); TEST_ASSERT_EQUAL_INT(ERANGE, errno); - /* - * Disabled because of #543 issue: https://github.com/phoenix-rtos/phoenix-rtos-project/issues/543 - * strtoll, strtoull doesn't set errno - */ + errno = 0; + TEST_ASSERT_EQUAL_INT(LLONG_MAX, strtoll(num_positive, NULL, 10)); + TEST_ASSERT_EQUAL_INT(ERANGE, errno); -#ifdef __phoenix__ - TEST_IGNORE_MESSAGE("#543 issue"); -#endif + errno = 0; + TEST_ASSERT_EQUAL_INT(ULLONG_MAX, strtoull(num_positive, NULL, 10)); + TEST_ASSERT_EQUAL_INT(ERANGE, errno); + + errno = 0; + TEST_ASSERT_EQUAL_INT(LONG_MIN, strtol(num_negative, NULL, 10)); + TEST_ASSERT_EQUAL_INT(ERANGE, errno); + + errno = 0; + TEST_ASSERT_EQUAL_INT(ULONG_MAX, strtoul(num_negative, NULL, 10)); + TEST_ASSERT_EQUAL_INT(ERANGE, errno); errno = 0; - strtoll(num, NULL, 10); + TEST_ASSERT_EQUAL_INT(LLONG_MIN, strtoll(num_negative, NULL, 10)); TEST_ASSERT_EQUAL_INT(ERANGE, errno); errno = 0; - strtoull(num, NULL, 10); + TEST_ASSERT_EQUAL_INT(ULLONG_MAX, strtoull(num_negative, NULL, 10)); TEST_ASSERT_EQUAL_INT(ERANGE, errno); } +TEST(stdlib_strto, zero_x_int) +{ + const char *nums[] = { "0x", "0xz" }; + char *end; + + errno = 0; + for (int i = 0; i < (sizeof(nums) / sizeof(nums[0])); i++) { + const char *num = nums[i]; + TEST_ASSERT_EQUAL_INT(0, strtol(num, &end, 0)); + TEST_ASSERT_EQUAL_INT(0, errno); + TEST_ASSERT_EQUAL_PTR(end, num + 1); + + TEST_ASSERT_EQUAL_INT(0, strtoul(num, &end, 0)); + TEST_ASSERT_EQUAL_INT(0, errno); + TEST_ASSERT_EQUAL_PTR(end, num + 1); + + TEST_ASSERT_EQUAL_INT(0, strtoll(num, &end, 0)); + TEST_ASSERT_EQUAL_INT(0, errno); + TEST_ASSERT_EQUAL_PTR(end, num + 1); + + TEST_ASSERT_EQUAL_INT(0, strtoull(num, &end, 0)); + TEST_ASSERT_EQUAL_INT(0, errno); + TEST_ASSERT_EQUAL_PTR(end, num + 1); + } +} + + TEST(stdlib_strto, invalid) { /* According to POSIX: When "No conversion could be performed", these functions may set errno to EINVAL. */ @@ -1071,15 +1106,6 @@ TEST(stdlib_strto, invalid_base) strtoul("1234", NULL, INT_MIN); TEST_ASSERT_EQUAL_INT(EINVAL, errno); - /* - * Disabled because of #543 issue: https://github.com/phoenix-rtos/phoenix-rtos-project/issues/543 - * strtoll, strtoull doesn't set errno - */ - -#ifdef __phoenix__ - TEST_IGNORE_MESSAGE("#543 issue"); -#endif - errno = 0; strtoll("1234", NULL, 1); TEST_ASSERT_EQUAL_INT(EINVAL, errno); @@ -1208,6 +1234,7 @@ TEST_GROUP_RUNNER(stdlib_strto) RUN_TEST_CASE(stdlib_strto, truncate_whitespaces); RUN_TEST_CASE(stdlib_strto, too_long_numbers_float) RUN_TEST_CASE(stdlib_strto, too_long_numbers_int) + RUN_TEST_CASE(stdlib_strto, zero_x_int); RUN_TEST_CASE(stdlib_strto, invalid); RUN_TEST_CASE(stdlib_strto, invalid_base); RUN_TEST_CASE(stdlib_strto, float_remaining_string);