diff --git a/libc/src/__support/str_conv_utils.h b/libc/src/__support/str_conv_utils.h --- a/libc/src/__support/str_conv_utils.h +++ b/libc/src/__support/str_conv_utils.h @@ -39,7 +39,8 @@ static inline int infer_base(const char *__restrict *__restrict src) { if (**src == '0') { ++(*src); - if ((**src | 32) == 'x') { + if ((**src | 32) == 'x' && isalnum(*(*src + 1)) && + b36_char_to_int(*(*src + 1)) < 16) { ++(*src); return 16; } @@ -71,7 +72,10 @@ if (base == 0) { base = infer_base(&src); - } else if (base == 16 && *src == '0' && (*(src + 1) | 32) == 'x') { + } else if (base == 16 && *src == '0' && (*(src + 1) | 32) == 'x' && + isalnum(*(src + 2)) && b36_char_to_int(*(src + 2)) < 16) { + // There has to be a hexadecimal digit after the "0x", else the number is + // just the 0. src = src + 2; } diff --git a/libc/test/src/stdlib/strtol_test.cpp b/libc/test/src/stdlib/strtol_test.cpp --- a/libc/test/src/stdlib/strtol_test.cpp +++ b/libc/test/src/stdlib/strtol_test.cpp @@ -261,6 +261,38 @@ ASSERT_EQ(__llvm_libc::strtol(yes_prefix, &str_end, 16), 0x456defl); ASSERT_EQ(errno, 0); EXPECT_EQ(str_end - yes_prefix, ptrdiff_t(8)); + + const char *letter_after_prefix = "0xabc123"; + errno = 0; + ASSERT_EQ(__llvm_libc::strtol(letter_after_prefix, &str_end, 16), 0xabc123l); + ASSERT_EQ(errno, 0); + EXPECT_EQ(str_end - letter_after_prefix, ptrdiff_t(8)); +} + +TEST(LlvmLibcStrToLTest, MessyBaseSixteenDecode) { + char *str_end = nullptr; + + const char *just_prefix = "0x"; + errno = 0; + ASSERT_EQ(__llvm_libc::strtol(just_prefix, &str_end, 16), 0l); + ASSERT_EQ(errno, 0); + EXPECT_EQ(str_end - just_prefix, ptrdiff_t(1)); + + errno = 0; + ASSERT_EQ(__llvm_libc::strtol(just_prefix, &str_end, 0), 0l); + ASSERT_EQ(errno, 0); + EXPECT_EQ(str_end - just_prefix, ptrdiff_t(1)); + + const char *prefix_with_x_after = "0xx"; + errno = 0; + ASSERT_EQ(__llvm_libc::strtol(prefix_with_x_after, &str_end, 16), 0l); + ASSERT_EQ(errno, 0); + EXPECT_EQ(str_end - prefix_with_x_after, ptrdiff_t(1)); + + errno = 0; + ASSERT_EQ(__llvm_libc::strtol(prefix_with_x_after, &str_end, 0), 0l); + ASSERT_EQ(errno, 0); + EXPECT_EQ(str_end - prefix_with_x_after, ptrdiff_t(1)); } TEST(LlvmLibcStrToLTest, AutomaticBaseSelection) { diff --git a/libc/test/src/stdlib/strtoll_test.cpp b/libc/test/src/stdlib/strtoll_test.cpp --- a/libc/test/src/stdlib/strtoll_test.cpp +++ b/libc/test/src/stdlib/strtoll_test.cpp @@ -287,6 +287,39 @@ ASSERT_EQ(__llvm_libc::strtoll(yes_prefix, &str_end, 16), 0x456defll); ASSERT_EQ(errno, 0); EXPECT_EQ(str_end - yes_prefix, ptrdiff_t(8)); + + const char *letter_after_prefix = "0xabc123"; + errno = 0; + ASSERT_EQ(__llvm_libc::strtoll(letter_after_prefix, &str_end, 16), + 0xabc123ll); + ASSERT_EQ(errno, 0); + EXPECT_EQ(str_end - letter_after_prefix, ptrdiff_t(8)); +} + +TEST(LlvmLibcStrToLLTest, MessyBaseSixteenDecode) { + char *str_end = nullptr; + + const char *just_prefix = "0x"; + errno = 0; + ASSERT_EQ(__llvm_libc::strtoll(just_prefix, &str_end, 16), 0ll); + ASSERT_EQ(errno, 0); + EXPECT_EQ(str_end - just_prefix, ptrdiff_t(1)); + + errno = 0; + ASSERT_EQ(__llvm_libc::strtoll(just_prefix, &str_end, 0), 0ll); + ASSERT_EQ(errno, 0); + EXPECT_EQ(str_end - just_prefix, ptrdiff_t(1)); + + const char *prefix_with_x_after = "0xx"; + errno = 0; + ASSERT_EQ(__llvm_libc::strtoll(prefix_with_x_after, &str_end, 16), 0ll); + ASSERT_EQ(errno, 0); + EXPECT_EQ(str_end - prefix_with_x_after, ptrdiff_t(1)); + + errno = 0; + ASSERT_EQ(__llvm_libc::strtoll(prefix_with_x_after, &str_end, 0), 0ll); + ASSERT_EQ(errno, 0); + EXPECT_EQ(str_end - prefix_with_x_after, ptrdiff_t(1)); } TEST(LlvmLibcStrToLLTest, AutomaticBaseSelection) { diff --git a/libc/test/src/stdlib/strtoul_test.cpp b/libc/test/src/stdlib/strtoul_test.cpp --- a/libc/test/src/stdlib/strtoul_test.cpp +++ b/libc/test/src/stdlib/strtoul_test.cpp @@ -255,6 +255,39 @@ ASSERT_EQ(__llvm_libc::strtoul(yes_prefix, &str_end, 16), 0x456deful); ASSERT_EQ(errno, 0); EXPECT_EQ(str_end - yes_prefix, ptrdiff_t(8)); + + const char *letter_after_prefix = "0xabc123"; + errno = 0; + ASSERT_EQ(__llvm_libc::strtoul(letter_after_prefix, &str_end, 16), + 0xabc123ul); + ASSERT_EQ(errno, 0); + EXPECT_EQ(str_end - letter_after_prefix, ptrdiff_t(8)); +} + +TEST(LlvmLibcStrToULTest, MessyBaseSixteenDecode) { + char *str_end = nullptr; + + const char *just_prefix = "0x"; + errno = 0; + ASSERT_EQ(__llvm_libc::strtoul(just_prefix, &str_end, 16), 0ul); + ASSERT_EQ(errno, 0); + EXPECT_EQ(str_end - just_prefix, ptrdiff_t(1)); + + errno = 0; + ASSERT_EQ(__llvm_libc::strtoul(just_prefix, &str_end, 0), 0ul); + ASSERT_EQ(errno, 0); + EXPECT_EQ(str_end - just_prefix, ptrdiff_t(1)); + + const char *prefix_with_x_after = "0xx"; + errno = 0; + ASSERT_EQ(__llvm_libc::strtoul(prefix_with_x_after, &str_end, 16), 0ul); + ASSERT_EQ(errno, 0); + EXPECT_EQ(str_end - prefix_with_x_after, ptrdiff_t(1)); + + errno = 0; + ASSERT_EQ(__llvm_libc::strtoul(prefix_with_x_after, &str_end, 0), 0ul); + ASSERT_EQ(errno, 0); + EXPECT_EQ(str_end - prefix_with_x_after, ptrdiff_t(1)); } TEST(LlvmLibcStrToULTest, AutomaticBaseSelection) { diff --git a/libc/test/src/stdlib/strtoull_test.cpp b/libc/test/src/stdlib/strtoull_test.cpp --- a/libc/test/src/stdlib/strtoull_test.cpp +++ b/libc/test/src/stdlib/strtoull_test.cpp @@ -263,6 +263,39 @@ ASSERT_EQ(__llvm_libc::strtoull(yes_prefix, &str_end, 16), 0x456defull); ASSERT_EQ(errno, 0); EXPECT_EQ(str_end - yes_prefix, ptrdiff_t(8)); + + const char *letter_after_prefix = "0xabc123"; + errno = 0; + ASSERT_EQ(__llvm_libc::strtoull(letter_after_prefix, &str_end, 16), + 0xabc123ull); + ASSERT_EQ(errno, 0); + EXPECT_EQ(str_end - letter_after_prefix, ptrdiff_t(8)); +} + +TEST(LlvmLibcStrToULLTest, MessyBaseSixteenDecode) { + char *str_end = nullptr; + + const char *just_prefix = "0x"; + errno = 0; + ASSERT_EQ(__llvm_libc::strtoull(just_prefix, &str_end, 16), 0ull); + ASSERT_EQ(errno, 0); + EXPECT_EQ(str_end - just_prefix, ptrdiff_t(1)); + + errno = 0; + ASSERT_EQ(__llvm_libc::strtoull(just_prefix, &str_end, 0), 0ull); + ASSERT_EQ(errno, 0); + EXPECT_EQ(str_end - just_prefix, ptrdiff_t(1)); + + const char *prefix_with_x_after = "0xx"; + errno = 0; + ASSERT_EQ(__llvm_libc::strtoull(prefix_with_x_after, &str_end, 16), 0ull); + ASSERT_EQ(errno, 0); + EXPECT_EQ(str_end - prefix_with_x_after, ptrdiff_t(1)); + + errno = 0; + ASSERT_EQ(__llvm_libc::strtoull(prefix_with_x_after, &str_end, 0), 0ull); + ASSERT_EQ(errno, 0); + EXPECT_EQ(str_end - prefix_with_x_after, ptrdiff_t(1)); } TEST(LlvmLibcStrToULLTest, AutomaticBaseSelection) {