[Boost-bugs] [Boost C++ Libraries] #10109: Wrong overflow checking with uint_parser<int>

Subject: [Boost-bugs] [Boost C++ Libraries] #10109: Wrong overflow checking with uint_parser<int>
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2014-06-10 13:29:09

#10109: Wrong overflow checking with uint_parser<int>
 Reporter: Giulio Franco <giulio_franco@…> | Owner: djowel
     Type: Bugs | Status: new
Milestone: To Be Determined | Component: spirit
  Version: Boost Release Branch | Severity: Problem
 Keywords: |
 Running the following code will produce "`Parsed -2147483648`" on the
 standard output.
 #include <iostream>
 #include <boost/spirit/include/qi.hpp>
 #include <boost/spirit/include/qi_parse_attr.hpp>
 using namespace boost::spirit::qi;
 int main() {
     char const test[] = "2147483648"; //INT_MAX + 1
     char const* begin = test;
     int n;
     if ( parse( begin, test + sizeof(test) - 1, uint_parser<int>(), n ) )
         std::cout << "Parsed " << n << std::endl;
     } else {
         std::cout << "Parse failed" << std::endl;
     return 0;

 Since I'm requesting a `uint_parser`, I assume I should never get a
 negative value.
 This is what happens, for instance, with `uint_parser<double>`, which will
 reject negative values.

 The issue probably lies in `spirit/home/qi/numeric/numeric_utils.hpp`,
 where `extract_uint::call` invokes
 extract_type::parse(first, last, detail::cast_unsigned<T>::call(attr_))

 but later `positive_accumulator::add`, will check the overflow condition
 for the type of its argument (which will be `unsigned int`, due to the
 previous cast), rather than for the template type `T` of the
 `extract_uint` struct, which is correctly `int`.

 The call stack I'm talking about is: `parse -> any_uint_parser::parse ->
 extract_uint::call -> extract_int::parse -> extract_int::parse_main ->
 int_extractor::call -> int_extractor::call (with mpl::true_) ->

Ticket URL: <https://svn.boost.org/trac/boost/ticket/10109>
Boost C++ Libraries <http://www.boost.org/>
Boost provides free peer-reviewed portable C++ source libraries.

This archive was generated by hypermail 2.1.7 : 2017-02-16 18:50:16 UTC