Skip to content

Commit

Permalink
Issue 13931 - Missing overflow checks in std.conv for negative numb…
Browse files Browse the repository at this point in the history
…ers which start from the most negative number digits

Issue URL: https://issues.dlang.org/show_bug.cgi?id=13931
  • Loading branch information
denis-sh committed Jan 4, 2015
1 parent 79d5b09 commit d1783bb
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions std/conv.d
Expand Up @@ -1984,9 +1984,11 @@ Target parse(Target, Source)(ref Source s)
if (c > 9)
break;

if (v < Target.max/10 ||
(v == Target.max/10 && c <= maxLastDigit + sign))
if (v >= 0 && (v < Target.max/10 ||
(v == Target.max/10 && c <= maxLastDigit + sign)))
{
// Note: `v` can become negative here in case of parsing
// the most negative value:
v = cast(Target) (v * 10 + c);
s.popFront();
}
Expand Down Expand Up @@ -2186,6 +2188,15 @@ Lerr:
assertCTFEable!({ string s = "1234abc"; assert(parse!uint(s) == 1234 && s == "abc"); });
}

// Issue 13931
@safe pure unittest
{
import std.exception;

assertThrown!ConvOverflowException("-21474836480".to!int());
assertThrown!ConvOverflowException("-92233720368547758080".to!long());
}

/// ditto
Target parse(Target, Source)(ref Source s, uint radix)
if (isSomeChar!(ElementType!Source) &&
Expand Down

0 comments on commit d1783bb

Please sign in to comment.