Skip to content

Commit

Permalink
Fixing real precision by limiting precision to 15 digits. (osquery#8355)
Browse files Browse the repository at this point in the history
Fixes osquery#8301 

The previous fix was incomplete -- there were still some numbers that were being improperly converted into string.

This fix limits double-to-string conversion to significant digits only, without an extra non-significant digit.
  • Loading branch information
getvictor authored Jun 27, 2024
1 parent a56ce1e commit ca14540
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion osquery/utils/conversions/castvariant.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@ class CastVisitor : public boost::static_visitor<std::string> {

std::string operator()(const double& d) const {
std::ostringstream ss;
ss << std::setprecision(std::numeric_limits<double>::digits10 + 1) << d;
// SQLite supports 15 significant digits.
// The value of std::numeric_limits<T>::digits10 is the number of base-10
// digits that can be represented by the type T without change, that is, any
// number with this many significant decimal digits can be converted to a
// value of type T and back to decimal form, without change due to rounding
// or overflow. (from cppreference.com)
ss << std::setprecision(std::numeric_limits<double>::digits10) << d;
std::string s = ss.str();
if (s.find('.') == std::string::npos) {
s += ".0";
Expand Down

0 comments on commit ca14540

Please sign in to comment.