Skip to content

Commit

Permalink
fix issue 13163 - std.conv.parse misses overflow when it doesn't resu…
Browse files Browse the repository at this point in the history
…lt in a smaller value
  • Loading branch information
Nils Boßung committed Jul 23, 2014
1 parent 5d7c4c0 commit efe0d17
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 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,16 @@ body
c -= 'a'-10-'0';
}
}
auto blah = cast(Target) (v * radix + c - '0');
if (blah < v)

bool overflow = false;
static if (isSigned!Target)
auto nextv = v.muls(radix, overflow).adds(c - '0', overflow);
else
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 +2252,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 efe0d17

Please sign in to comment.