From f93880c356108cfdbc8f9ebe318d18f256d7128d Mon Sep 17 00:00:00 2001 From: Philip Chimento Date: Sat, 5 Nov 2022 18:01:36 -0700 Subject: [PATCH] tests: Avoid using char type in uniform_int_distribution template This is undefined behaviour. GCC and pre-15.x Clang accept it, so we didn't notice it before. Closes: #514 --- test/gjs-tests.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/test/gjs-tests.cpp b/test/gjs-tests.cpp index 49011bba1..e48734aa4 100644 --- a/test/gjs-tests.cpp +++ b/test/gjs-tests.cpp @@ -55,12 +55,30 @@ static unsigned cpp_random_seed = 0; using Gjs::Test::assert_equal; +template +struct is_char_helper : public std::false_type {}; +template <> +struct is_char_helper : public std::true_type {}; +template <> +struct is_char_helper : public std::true_type {}; +template <> +struct is_char_helper : public std::true_type {}; +template <> +struct is_char_helper : public std::true_type {}; +template +struct is_char : public is_char_helper>::type {}; +template +inline constexpr bool is_char_v = is_char::value; + template T get_random_number() { std::mt19937_64 gen(cpp_random_seed); if constexpr (std::is_same_v) { return g_random_boolean(); + } else if constexpr (is_char_v) { + return std::char_traits::to_char_type( + get_random_number::int_type>()); } else if constexpr (std::is_integral_v) { T lowest_value = std::numeric_limits::lowest(); -- GitLab