authorgravatar for thejoshwolfe@gmail.comJosh Wolfe <thejoshwolfe@gmail.com> 2023-06-07 10:34:25-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2023-06-07 10:34:25-04:00
logcb7040154e7c9a381951d0f5d969a9dd97be1ec0
tree0777a110421ac6d77b69c87c539397ca33734f6f
parentc16d4ab9e41be6b5c560d15eaa145ff3a0ffce6c
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

std: fix parseInt for single-digit signed minInt (#15966)


1 files changed, 10 insertions(+), 1 deletions(-)

lib/std/fmt.zig+10-1
...@@ -1766,6 +1766,7 @@ test "parseInt" {...@@ -1766,6 +1766,7 @@ test "parseInt" {
1766 try std.testing.expect((try parseInt(u8, "+0", 10)) == 0);1766 try std.testing.expect((try parseInt(u8, "+0", 10)) == 0);
17671767
1768 // ensure minInt is parsed correctly1768 // ensure minInt is parsed correctly
1769 try std.testing.expect((try parseInt(i1, "-1", 10)) == math.minInt(i1));
1769 try std.testing.expect((try parseInt(i8, "-128", 10)) == math.minInt(i8));1770 try std.testing.expect((try parseInt(i8, "-128", 10)) == math.minInt(i8));
1770 try std.testing.expect((try parseInt(i43, "-4398046511104", 10)) == math.minInt(i43));1771 try std.testing.expect((try parseInt(i43, "-4398046511104", 10)) == math.minInt(i43));
17711772
...@@ -1847,7 +1848,15 @@ fn parseWithSign(...@@ -1847,7 +1848,15 @@ fn parseWithSign(
1847 if (c == '_') continue;1848 if (c == '_') continue;
1848 const digit = try charToDigit(c, buf_base);1849 const digit = try charToDigit(c, buf_base);
18491850
1850 if (x != 0) x = try math.mul(T, x, math.cast(T, buf_base) orelse return error.Overflow);1851 if (x != 0) {
1852 x = try math.mul(T, x, math.cast(T, buf_base) orelse return error.Overflow);
1853 } else if (sign == .neg) {
1854 // The first digit of a negative number.
1855 // Consider parsing "-4" as an i3.
1856 // This should work, but positive 4 overflows i3, so we can't cast the digit to T and subtract.
1857 x = math.cast(T, -@intCast(i8, digit)) orelse return error.Overflow;
1858 continue;
1859 }
1851 x = try add(T, x, math.cast(T, digit) orelse return error.Overflow);1860 x = try add(T, x, math.cast(T, digit) orelse return error.Overflow);
1852 }1861 }
18531862