test: cover INT64_MIN parse and run parser tests under UBSan

This commit is contained in:
rootvector2 2026-05-27 00:40:22 +05:30
parent a63865f58a
commit 079a9f98d2
2 changed files with 12 additions and 0 deletions

View File

@ -27,6 +27,15 @@ target_link_libraries(options_test_noregex cxxopts)
target_compile_definitions(options_test_noregex PRIVATE CXXOPTS_NO_REGEX)
add_test(options_no_regex options_test_noregex)
# Run the parser tests under UBSan so undefined behaviour in integer parsing
# (e.g. std::abs(INT64_MIN)) fails the build instead of going unnoticed.
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang|GNU")
foreach(test_target options_test options_test_noregex)
target_compile_options(${test_target} PRIVATE -fsanitize=undefined -fno-sanitize-recover=undefined)
set_target_properties(${test_target} PROPERTIES LINK_FLAGS "-fsanitize=undefined -fno-sanitize-recover=undefined")
endforeach()
endif()
# test if the targets are findable from the build directory
add_test(find-package-test ${CMAKE_CTEST_COMMAND}
-C ${CMAKE_BUILD_TYPE}

View File

@ -2,6 +2,7 @@
#include <iostream>
#include <initializer_list>
#include <limits>
#include "cxxopts.hpp"
@ -921,6 +922,8 @@ TEST_CASE("Overflow on boundary", "[integer]")
CHECK_THROWS_AS((integer_parser("-42769", si16)), cxxopts::exceptions::incorrect_argument_type);
CHECK_THROWS_AS((integer_parser("-75536", si16)), cxxopts::exceptions::incorrect_argument_type);
CHECK_NOTHROW((integer_parser("-9223372036854775808", si64)));
CHECK(si64 == (std::numeric_limits<int64_t>::min)());
CHECK_THROWS_AS((integer_parser("18446744073709551616", ui64)), cxxopts::exceptions::incorrect_argument_type);
CHECK_THROWS_AS((integer_parser("28446744073709551616", ui64)), cxxopts::exceptions::incorrect_argument_type);
CHECK_THROWS_AS((integer_parser("9223372036854775808", si64)), cxxopts::exceptions::incorrect_argument_type);