Opened 8 years ago

Closed 8 years ago

#10755 closed Bugs (fixed)

Wrong overflow checking with uint_parser<uint8_t, 10, 1, 3>

Reported by: hoernse <die_hoernse@…> Owned by: Joel de Guzman
Milestone: To Be Determined Component: spirit
Version: Boost 1.54.0 Severity: Problem
Keywords: Cc:

Description

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.

Change History (1)

comment:1 by Joel de Guzman, 8 years ago

Resolution: fixed
Status: newclosed

Confirmed and fixed in develop branch. Overflow now correctly fails parse.

parsing 255 returned 255 parsing 256 failed parsing 257 failed parsing 258 failed parsing 259 failed parsing 260 failed

Note: See TracTickets for help on using tickets.