From 8f7131ea37fb531b66ea7dc7a9c42389b0872f49 Mon Sep 17 00:00:00 2001 From: Matt Borland Date: Thu, 16 Feb 2023 08:17:04 -0800 Subject: [PATCH 1/4] Remove assertion in from_chars --- include/boost/charconv/from_chars.hpp | 3 +-- test/from_chars.cpp | 8 ++++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/include/boost/charconv/from_chars.hpp b/include/boost/charconv/from_chars.hpp index a37e668..6f94d90 100644 --- a/include/boost/charconv/from_chars.hpp +++ b/include/boost/charconv/from_chars.hpp @@ -87,8 +87,7 @@ BOOST_CXX14_CONSTEXPR from_chars_result from_chars_integer_impl(const char* firs Unsigned_Integer max_digit = 0; // Check pre-conditions - BOOST_CHARCONV_ASSERT_MSG(base >= 2 && base <= 36, "Base must be between 2 and 36 (inclusive)"); - if (!(first <= last)) + if (!((first <= last) && (base >= 2 && base <= 36))) { return {first, EINVAL}; } diff --git a/test/from_chars.cpp b/test/from_chars.cpp index 23ac5ad..04b9451 100644 --- a/test/from_chars.cpp +++ b/test/from_chars.cpp @@ -124,6 +124,14 @@ void invalid_argument_test() auto r4 = boost::charconv::from_chars(buffer4, buffer4 + std::strlen(buffer4), v4); BOOST_TEST_EQ(r4.ec, EINVAL); } + + // Bases outside 2-36 inclusive return EINVAL + const char* buffer5 = "23"; + T v5 = 0; + auto r5 = boost::charconv::from_chars(buffer5, buffer5 + std::strlen(buffer5), v5, 1); + BOOST_TEST_EQ(r5.ec, EINVAL); + auto r6 = boost::charconv::from_chars(buffer5, buffer5 + std::strlen(buffer5), v5, 50); + BOOST_TEST_EQ(r6.ec, EINVAL); } // No overflows, negative numbers, locales, etc. From 7c9e7263fbbca5afe223b4ff31b24c56527872f4 Mon Sep 17 00:00:00 2001 From: Matt Borland Date: Thu, 16 Feb 2023 08:24:31 -0800 Subject: [PATCH 2/4] Remove assertions in to_chars --- include/boost/charconv/to_chars.hpp | 8 +------- test/to_chars.cpp | 8 ++++++++ 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/include/boost/charconv/to_chars.hpp b/include/boost/charconv/to_chars.hpp index d20f8a1..06db8c1 100644 --- a/include/boost/charconv/to_chars.hpp +++ b/include/boost/charconv/to_chars.hpp @@ -222,10 +222,6 @@ BOOST_CHARCONV_CONSTEXPR to_chars_result to_chars_integer_impl(char* first, char } } } - else - { - BOOST_CHARCONV_ASSERT_MSG(sizeof(Integer) < 1, "Your type is unsupported. Use a built-in integral type"); - } return {first + converted_value_digits, 0}; } @@ -322,11 +318,9 @@ BOOST_CHARCONV_CONSTEXPR to_chars_result to_chars_128integer_impl(char* first, c template BOOST_CHARCONV_CONSTEXPR to_chars_result to_chars_integer_impl(char* first, char* last, Integer value, int base) noexcept { - BOOST_CHARCONV_ASSERT_MSG(base >= 2 && base <= 36, "Base must be between 2 and 36 (inclusive)"); - const std::ptrdiff_t output_length = last - first; - if (!(first <= last)) + if (!((first <= last) && (base >= 2 && base <= 36))) { return {last, EINVAL}; } diff --git a/test/to_chars.cpp b/test/to_chars.cpp index 82df20a..4abca3b 100644 --- a/test/to_chars.cpp +++ b/test/to_chars.cpp @@ -252,6 +252,14 @@ void simple_test() BOOST_TEST(r1 != r2); BOOST_TEST_EQ(r2.ec, 0); BOOST_TEST_CSTR_EQ(buffer2, "12"); + + // If the base is not 2-36 inclusive return invalid value + char buffer3[64] {}; + T v3 = 12; + auto r3 = boost::charconv::to_chars(buffer3, buffer3 + sizeof(buffer3) - 1, v3, -2); + BOOST_TEST_EQ(r3.ec, EINVAL); + auto r4 = boost::charconv::to_chars(buffer3, buffer3 + sizeof(buffer3) - 1, v3, 90); + BOOST_TEST_EQ(r4.ec, EINVAL); } int main() From 9342b67f2a474456f1d1e00009732e5d4bdf92ae Mon Sep 17 00:00:00 2001 From: Matt Borland Date: Thu, 16 Feb 2023 08:24:47 -0800 Subject: [PATCH 3/4] Add TODO in config as Boost.Assert is now unused --- include/boost/charconv/config.hpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/include/boost/charconv/config.hpp b/include/boost/charconv/config.hpp index 1eaeae7..f240b9b 100644 --- a/include/boost/charconv/config.hpp +++ b/include/boost/charconv/config.hpp @@ -36,6 +36,8 @@ #endif +// TODO: BOOST_ASSERT is currently unused. +// Once library is complete remove this block, and Boost.Assert from the CML if still unused. #ifndef BOOST_CHARCONV_STANDALONE # include # define BOOST_CHARCONV_ASSERT(expr) BOOST_ASSERT(expr) From 68897e314046cafc3502d106b2742a2ce4f44abf Mon Sep 17 00:00:00 2001 From: Matt Borland Date: Thu, 16 Feb 2023 08:48:21 -0800 Subject: [PATCH 4/4] Move macros from config to detail/config --- include/boost/charconv/config.hpp | 32 -------------- include/boost/charconv/detail/config.hpp | 42 +++++++++++++++++++ .../charconv/detail/integer_conversion.hpp | 2 +- .../charconv/detail/integer_search_trees.hpp | 2 +- .../charconv/detail/is_constant_evaluated.hpp | 2 +- include/boost/charconv/detail/memcpy.hpp | 1 + include/boost/charconv/from_chars.hpp | 1 + include/boost/charconv/to_chars.hpp | 1 + 8 files changed, 48 insertions(+), 35 deletions(-) create mode 100644 include/boost/charconv/detail/config.hpp diff --git a/include/boost/charconv/config.hpp b/include/boost/charconv/config.hpp index f240b9b..0d94ccd 100644 --- a/include/boost/charconv/config.hpp +++ b/include/boost/charconv/config.hpp @@ -36,36 +36,4 @@ #endif -// TODO: BOOST_ASSERT is currently unused. -// Once library is complete remove this block, and Boost.Assert from the CML if still unused. -#ifndef BOOST_CHARCONV_STANDALONE -# include -# define BOOST_CHARCONV_ASSERT(expr) BOOST_ASSERT(expr) -# define BOOST_CHARCONV_ASSERT_MSG(expr, msg) BOOST_ASSERT_MSG(expr, msg) -#else // Use plain asserts -# include -# define BOOST_CHARCONV_ASSERT(expr) assert(expr) -# define BOOST_CHARCONV_ASSERT_MSG(expr, msg) assert((expr)&&(msg)) -#endif - -// Use 128 bit integers and supress warnings for using extensions -#if defined(BOOST_HAS_INT128) -# define BOOST_CHARCONV_HAS_INT128 -# define BOOST_CHARCONV_INT128_MAX (boost::int128_type)(((boost::uint128_type) 1 << 127) - 1) -# define BOOST_CHARCONV_INT128_MIN (-BOOST_CHARCONV_INT128_MAX - 1) -# define BOOST_CHARCONV_UINT128_MAX ((2 * (boost::uint128_type) BOOST_CHARCONV_INT128_MAX) + 1) -#endif - -#ifndef BOOST_NO_CXX14_CONSTEXPR -# define BOOST_CHARCONV_CXX14_CONSTEXPR BOOST_CXX14_CONSTEXPR -#else -# define BOOST_CHARCONV_CXX14_CONSTEXPR inline -#endif - -#if defined(__GNUC__) && __GNUC__ == 5 -# define BOOST_CHARCONV_GCC5_CONSTEXPR inline -#else -# define BOOST_CHARCONV_GCC5_CONSTEXPR BOOST_CHARCONV_CXX14_CONSTEXPR -#endif - #endif // BOOST_CHARCONV_CONFIG_HPP_INCLUDED diff --git a/include/boost/charconv/detail/config.hpp b/include/boost/charconv/detail/config.hpp new file mode 100644 index 0000000..85ab269 --- /dev/null +++ b/include/boost/charconv/detail/config.hpp @@ -0,0 +1,42 @@ +// Copyright 2023 Matt Borland +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt + +#ifndef BOOST_CHARCONV_DETAIL_CONFIG_HPP +#define BOOST_CHARCONV_DETAIL_CONFIG_HPP + +#include + +// TODO: BOOST_ASSERT is currently unused. +// Once library is complete remove this block, and Boost.Assert from the CML if still unused. +#ifndef BOOST_CHARCONV_STANDALONE +# include +# define BOOST_CHARCONV_ASSERT(expr) BOOST_ASSERT(expr) +# define BOOST_CHARCONV_ASSERT_MSG(expr, msg) BOOST_ASSERT_MSG(expr, msg) +#else // Use plain asserts +# include +# define BOOST_CHARCONV_ASSERT(expr) assert(expr) +# define BOOST_CHARCONV_ASSERT_MSG(expr, msg) assert((expr)&&(msg)) +#endif + +// Use 128 bit integers and supress warnings for using extensions +#if defined(BOOST_HAS_INT128) +# define BOOST_CHARCONV_HAS_INT128 +# define BOOST_CHARCONV_INT128_MAX (boost::int128_type)(((boost::uint128_type) 1 << 127) - 1) +# define BOOST_CHARCONV_INT128_MIN (-BOOST_CHARCONV_INT128_MAX - 1) +# define BOOST_CHARCONV_UINT128_MAX ((2 * (boost::uint128_type) BOOST_CHARCONV_INT128_MAX) + 1) +#endif + +#ifndef BOOST_NO_CXX14_CONSTEXPR +# define BOOST_CHARCONV_CXX14_CONSTEXPR BOOST_CXX14_CONSTEXPR +#else +# define BOOST_CHARCONV_CXX14_CONSTEXPR inline +#endif + +#if defined(__GNUC__) && __GNUC__ == 5 +# define BOOST_CHARCONV_GCC5_CONSTEXPR inline +#else +# define BOOST_CHARCONV_GCC5_CONSTEXPR BOOST_CHARCONV_CXX14_CONSTEXPR +#endif + +#endif // BOOST_CHARCONV_DETAIL_CONFIG_HPP diff --git a/include/boost/charconv/detail/integer_conversion.hpp b/include/boost/charconv/detail/integer_conversion.hpp index 811769e..b26b5c5 100644 --- a/include/boost/charconv/detail/integer_conversion.hpp +++ b/include/boost/charconv/detail/integer_conversion.hpp @@ -5,7 +5,7 @@ #ifndef BOOST_CHARCONV_DETAIL_CONCATENATE_HPP #define BOOST_CHARCONV_DETAIL_CONCATENATE_HPP -#include +#include #include #include #include diff --git a/include/boost/charconv/detail/integer_search_trees.hpp b/include/boost/charconv/detail/integer_search_trees.hpp index 39cac2f..0609646 100644 --- a/include/boost/charconv/detail/integer_search_trees.hpp +++ b/include/boost/charconv/detail/integer_search_trees.hpp @@ -8,7 +8,7 @@ // https://stackoverflow.com/questions/1489830/efficient-way-to-determine-number-of-digits-in-an-integer?page=1&tab=scoredesc#tab-top // https://graphics.stanford.edu/~seander/bithacks.html -#include "../config.hpp" +#include #include #include #include diff --git a/include/boost/charconv/detail/is_constant_evaluated.hpp b/include/boost/charconv/detail/is_constant_evaluated.hpp index 046c47a..3c73ac2 100644 --- a/include/boost/charconv/detail/is_constant_evaluated.hpp +++ b/include/boost/charconv/detail/is_constant_evaluated.hpp @@ -7,7 +7,7 @@ #ifndef BOOST_CHARCONV_TOOLS_IS_CONSTANT_EVALUATED_HPP #define BOOST_CHARCONV_TOOLS_IS_CONSTANT_EVALUATED_HPP -#include +#include #include #ifdef __cpp_lib_is_constant_evaluated diff --git a/include/boost/charconv/detail/memcpy.hpp b/include/boost/charconv/detail/memcpy.hpp index 8bfa50b..a7ad995 100644 --- a/include/boost/charconv/detail/memcpy.hpp +++ b/include/boost/charconv/detail/memcpy.hpp @@ -5,6 +5,7 @@ #ifndef BOOST_CHARCONV_DETAIL_MEMCPY_HPP #define BOOST_CHARCONV_DETAIL_MEMCPY_HPP +#include #include #include #include diff --git a/include/boost/charconv/from_chars.hpp b/include/boost/charconv/from_chars.hpp index 6f94d90..da81b47 100644 --- a/include/boost/charconv/from_chars.hpp +++ b/include/boost/charconv/from_chars.hpp @@ -7,6 +7,7 @@ #define BOOST_CHARCONV_FROM_CHARS_HPP_INCLUDED #include +#include #include #include #include diff --git a/include/boost/charconv/to_chars.hpp b/include/boost/charconv/to_chars.hpp index 06db8c1..a0f3ab2 100644 --- a/include/boost/charconv/to_chars.hpp +++ b/include/boost/charconv/to_chars.hpp @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include