Fix undefined-behavior in copyValueFromJValue

Issue highlighted by asan activated in PR osquery/osquery#5628

Imprecisions between float -> double -> json -> double -> float
lead to out of range values been saved into a float variable.
Since json has only the notion of doubles as floating point numbers,
it's better to require to use them.

Also forced the json parser to parse floating point numbers
with full precision, otherwise the test testing for precision would fail.

PR: osquery/osquery#5665
This commit is contained in:
Stefano Bonicatti 2019-07-25 22:08:40 +02:00 committed by Alessandro Gario
parent 0a302cd08e
commit ea17c51bb8
3 changed files with 5 additions and 5 deletions

View File

@ -73,7 +73,7 @@ Expected<std::string, JsonError> toJson(Type const& value) {
template <typename Type, typename RapidJsonInStream>
ExpectedSuccess<JsonError> fromJson(Type& value, RapidJsonInStream& is) {
auto dom = rapidjson::Document{};
dom.ParseStream(is);
dom.ParseStream<rapidjson::kParseFullPrecisionFlag>(is);
if (dom.HasParseError()) {
return createError(JsonError::Syntax)
<< "Can not parse value of type "

View File

@ -174,7 +174,7 @@ class JsonReader final {
template <typename KeyType,
typename ValueType,
typename std::enable_if<std::is_floating_point<ValueType>::value,
typename std::enable_if<std::is_same<ValueType, double>::value,
int>::type = 0>
void copyValueFromJValue(const KeyType& key,
ValueType& value,

View File

@ -97,7 +97,7 @@ class SecondTestClass {
return second_;
}
float const& getThird() const {
double const& getThird() const {
return third_;
}
@ -108,7 +108,7 @@ class SecondTestClass {
private:
std::string first_ = __FILE__;
int second_ = __LINE__;
float third_ = -1;
double third_ = -1;
bool fourth_ = false;
};
@ -189,7 +189,7 @@ struct ThirdTestClass {
std::string first = "";
unsigned second = 0u;
float third = 0.;
double third = 0.;
std::int64_t fourth = 0;
};