mirror of
https://github.com/CLIUtils/CLI11.git
synced 2025-05-05 06:33:52 +00:00
Fix possible UB in the sum_string_vector function (TypeTools.hpp) (#893)
Fixes #892 Undefined behavior in comparison of doubles.
This commit is contained in:
parent
a1135bb30c
commit
dce7983efa
@ -1652,13 +1652,10 @@ inline std::string sum_string_vector(const std::vector<std::string> &values) {
|
||||
output.append(arg);
|
||||
}
|
||||
} else {
|
||||
if(val <= static_cast<double>((std::numeric_limits<std::int64_t>::min)()) ||
|
||||
val >= static_cast<double>((std::numeric_limits<std::int64_t>::max)()) ||
|
||||
std::ceil(val) == std::floor(val)) {
|
||||
output = detail::value_string(static_cast<int64_t>(val));
|
||||
} else {
|
||||
output = detail::value_string(val);
|
||||
}
|
||||
std::ostringstream out;
|
||||
out.precision(16);
|
||||
out << val;
|
||||
output = out.str();
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
@ -11,6 +11,7 @@
|
||||
#include <complex>
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
#include <limits>
|
||||
|
||||
TEST_CASE_METHOD(TApp, "OneFlagShort", "[app]") {
|
||||
app.add_flag("-c,--count");
|
||||
@ -828,7 +829,7 @@ TEST_CASE_METHOD(TApp, "SumOptFloat", "[app]") {
|
||||
|
||||
run();
|
||||
|
||||
CHECK(0.6 == val);
|
||||
CHECK(std::fabs(0.6 - val) <= std::numeric_limits<double>::epsilon());
|
||||
}
|
||||
|
||||
TEST_CASE_METHOD(TApp, "SumOptString", "[app]") {
|
||||
|
Loading…
x
Reference in New Issue
Block a user