• yutak's avatar
    StringToNumber: Fix integer overflow on parsing INT_MIN. · 957b5b30
    yutak authored
    This patch adjusts the algorithm of toIntegralType(), which parses an integer
    value out of a string. Previously, toIntegralType() used a positive number
    as an accumulator regardless of whether we had seen the negative sign or not,
    and it inverted the accumulator value if there had been a negative sign. This
    causes an integer overflow when INT_MIN is specified as the source string,
    because the absolute value of INT_MIN is one larger than that of INT_MAX.
    
    This patch fixes this issue by accumulating a negative value if the result
    will be negative. Also, the detection algorithm of overflows is updated so
    that it can detect overflows correctly in every possible case (it used to have
    a small bug that the function fails to parse INT_MIN in base 16).
    
    Additionally, unit tests are added.
    
    BUG=665110
    
    Review-Url: https://codereview.chromium.org/2563643002
    Cr-Commit-Position: refs/heads/master@{#437508}
    957b5b30
StringToNumberTest.cpp 4.78 KB