Opened 13 years ago

Closed 13 years ago

Last modified 11 years ago

#3831 closed Bugs (fixed)

property_tree::read_json unable to handle large integers.

Reported by: Michael Anderson <drmikeando@…> Owned by: Sebastian Redl
Milestone: Boost 1.42.0 Component: property_tree
Version: Boost Development Trunk Severity: Problem
Keywords: Cc:

Description

Trying to read json that contains a value outside the range of an int into a property tree produces the following error

terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::property_tree::json_parser::json_parser_error> >'
  what():  <unspecified file>(1): expected value

This is due to the use of a int_p parser in the spirit parsing code. Applying the following patch

Index: boost/property_tree/detail/json_parser_read.hpp
===================================================================
--- boost/property_tree/detail/json_parser_read.hpp	(revision 58794)
+++ boost/property_tree/detail/json_parser_read.hpp	(working copy)
@@ -233,7 +233,7 @@
                 
                 number 
                     =   strict_real_p 
-                        | int_p
+                        | ( ! ( (ch_p('+') | ch_p('-') ) ) ) >> *digit_p 
                         ;
                 
                 string 

Seems to fix this.

The following example demonstrates the issue.

#include "boost/property_tree/json_parser.hpp"
#include "boost/property_tree/info_parser.hpp"

int main()
{
  boost::property_tree::ptree pt;
  std::string s("{\"v1\":124567890123,\"v2\":+124567890123,\"v3\":-124567890123}");
  std::stringstream ss;
  ss<<s;
  boost::property_tree::read_json( ss, pt );
  boost::property_tree::write_json( std::cout, pt );
}

This outputs

{
    "v1": "124567890123",
    "v2": "+124567890123",
    "v3": "-124567890123"
}

When the patch is applied.

Change History (2)

comment:1 by Sebastian Redl, 13 years ago

Resolution: fixed
Status: newclosed

(In [59740]) Make the JSON parser's number production fit the JSON spec instead of doing stupid things. Fixes bug 3831.

comment:2 by Marshall Clow, 11 years ago

This fix was copied to the release branch in [65975], and was part of boost 1.45.0

Note: See TracTickets for help on using tickets.