[Boost-bugs] [Boost C++ Libraries] #10755: Wrong overflow checking with uint_parser<uint8_t, 10, 1, 3>

Subject: [Boost-bugs] [Boost C++ Libraries] #10755: Wrong overflow checking with uint_parser<uint8_t, 10, 1, 3>
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2014-11-05 17:04:28


#10755: Wrong overflow checking with uint_parser<uint8_t, 10, 1, 3>
-------------------------------------+---------------------
 Reporter: hoernse <die_hoernse@…> | Owner: djowel
     Type: Bugs | Status: new
Milestone: To Be Determined | Component: spirit
  Version: Boost 1.54.0 | Severity: Problem
 Keywords: |
-------------------------------------+---------------------
 Lacking a default unsigned integer parser for one uint_8t I have defined
 {{{uint_parser<uint8_t, 10, 1, 3>}}} to parse IPs.

 If an octet goes beyond 255 it is to be expected that the parser only
 matches the first two digits leaving the third digit (and further digits)
 unconsumed. However, if the first two digits happen to be "25" and the
 third digit is greater than 5, the output attribute is incorrectly set to
 250 instead of 25. The third digit is (correctly) not consumed, meaning we
 get a three digit result from consuming two digits.

 The Example
 {{{
 #include <iostream>
 #include <vector>
 #include <string>
 #include "boost/spirit/include/qi.hpp"

 int main() {
     using namespace boost::spirit::qi;

     unsigned char octet;
     std::vector<std::string> v;
     uint_parser<uint8_t, 10, 1, 3> uchar_;

     v.push_back("255");
     v.push_back("256");
     v.push_back("257");
     v.push_back("258");
     v.push_back("259");
     v.push_back("260");

     for (std::vector<std::string>::iterator i = v.begin(); i < v.end();
 i++) {
         std::string::const_iterator ib = i->begin();
         std::string::const_iterator ie = i->end();

         std::cout << "parsing " << *i;

         if (parse(ib, ie, uchar_, octet)) {
             std::cout << " returned " << (unsigned int)octet << "\n";
         }
         else {
             std::cout << " failed\n";
         }
     }

     getch();
     return 0;
 }
 }}}

 outputs
 {{{
 parsing 255 returned 255
 parsing 256 returned 250
 parsing 257 returned 250
 parsing 258 returned 250
 parsing 259 returned 250
 parsing 260 returned 26
 }}}

 This bug might extend to other numeric parsers. I have not checked that
 yet.

-- 
Ticket URL: <https://svn.boost.org/trac/boost/ticket/10755>
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:17 UTC