Skip to content

Commit

Permalink
Merge pull request #2355 from NilsBossung/13163
Browse files Browse the repository at this point in the history
fix issue 13163 - std.conv.parse misses overflow when it doesn't result in a smaller value
  • Loading branch information
DmitryOlshansky committed Jul 30, 2014
2 parents 72aa8a9 + b4e31a5 commit 087b2f7
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions std/conv.d
Expand Up @@ -21,7 +21,7 @@ WIKI = Phobos/StdConv
*/
module std.conv;

import core.stdc.string;
import core.checkedint, core.stdc.string;
import std.algorithm, std.array, std.ascii, std.exception, std.range,
std.string, std.traits, std.typecons, std.typetuple, std.uni,
std.utf;
Expand Down Expand Up @@ -2188,10 +2188,13 @@ body
c -= 'a'-10-'0';
}
}
auto blah = cast(Target) (v * radix + c - '0');
if (blah < v)

bool overflow = false;
auto nextv = v.mulu(radix, overflow).addu(c - '0', overflow);
if (overflow || nextv > Target.max)
goto Loverflow;
v = blah;
v = cast(Target) nextv;

atStart = false;
}
if (atStart)
Expand Down Expand Up @@ -2246,6 +2249,12 @@ Lerr:
assert(r.front == '!');
}

@safe pure unittest // bugzilla 13163
{
foreach (s; ["fff", "123"])
assertThrown!ConvOverflowException(s.parse!ubyte(16));
}

Target parse(Target, Source)(ref Source s)
if (isExactSomeString!Source &&
is(Target == enum))
Expand Down

0 comments on commit 087b2f7

Please sign in to comment.