From d1783bbbc3090582f1496a76fd4589911f6f0874 Mon Sep 17 00:00:00 2001 From: Denis Shelomovskij Date: Sun, 4 Jan 2015 16:14:16 +0300 Subject: [PATCH] Issue 13931 - Missing overflow checks in `std.conv` for negative numbers which start from the most negative number digits Issue URL: https://issues.dlang.org/show_bug.cgi?id=13931 --- std/conv.d | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/std/conv.d b/std/conv.d index 2dff1ef6d0c..040e545ee1c 100644 --- a/std/conv.d +++ b/std/conv.d @@ -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(); } @@ -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) &&