authorgravatar for twostepted@gmail.comTravis Staloch <twostepted@gmail.com> 2023-11-28 13:48:49-08:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2023-12-11 17:49:22+02:00
log5bbacb0c8ce1aa59af5e898e255c544786059d06
treeadbfde693ae4952daabce2aea03a3c6216acc846
parenta817e27c7d459c8bba4b41931a842158bf3df2d0

fmt.parseWithSign(): prevent edge case overflows

previously when T was smaller than 8 bits, it was possible for base to overflow T (because base is a u8). this patch prevents this by accumulating into a U rather than T which is at least 8 bits wide. this is the best way i could think of to maintain performance. this will only affect parsing of integers less than 8 bits by adding one additional cast at return. additionally, this patch may be slightly slower to return an error for integers less than 8 bits which overflow because it will accumulate a few more digits before the overflow check at return. * add tests which previously overflowed when they shouldn't have closes #18157

1 files changed, 17 insertions(+), 6 deletions(-)

lib/std/fmt.zig+17-6
......@@ -1789,6 +1789,11 @@ test "parseInt" {
17891789 try std.testing.expectError(error.InvalidCharacter, parseInt(u32, "0b", 0));
17901790 try std.testing.expectError(error.InvalidCharacter, parseInt(u32, "0o", 0));
17911791 try std.testing.expectError(error.InvalidCharacter, parseInt(u32, "0x", 0));
1792
1793 // edge cases which previously errored due to base overflowing T
1794 try std.testing.expectEqual(@as(i2, -2), try std.fmt.parseInt(i2, "-10", 2));
1795 try std.testing.expectEqual(@as(i4, -8), try std.fmt.parseInt(i4, "-10", 8));
1796 try std.testing.expectEqual(@as(i5, -16), try std.fmt.parseInt(i5, "-10", 16));
17921797}
17931798
17941799fn parseWithSign(
......@@ -1829,27 +1834,33 @@ fn parseWithSign(
18291834 .neg => math.sub,
18301835 };
18311836
1832 var x: T = 0;
1837 // accumulate into U which is always 8 bits or larger. this prevents
1838 // `buf_base` from overflowing T.
1839 const info = @typeInfo(T);
1840 const U = std.meta.Int(info.Int.signedness, @max(8, info.Int.bits));
1841 var x: U = 0;
18331842
18341843 if (buf_start[0] == '_' or buf_start[buf_start.len - 1] == '_') return error.InvalidCharacter;
18351844
18361845 for (buf_start) |c| {
18371846 if (c == '_') continue;
18381847 const digit = try charToDigit(c, buf_base);
1839
18401848 if (x != 0) {
1841 x = try math.mul(T, x, math.cast(T, buf_base) orelse return error.Overflow);
1849 x = try math.mul(U, x, math.cast(U, buf_base) orelse return error.Overflow);
18421850 } else if (sign == .neg) {
18431851 // The first digit of a negative number.
18441852 // Consider parsing "-4" as an i3.
18451853 // This should work, but positive 4 overflows i3, so we can't cast the digit to T and subtract.
1846 x = math.cast(T, -@as(i8, @intCast(digit))) orelse return error.Overflow;
1854 x = math.cast(U, -@as(i8, @intCast(digit))) orelse return error.Overflow;
18471855 continue;
18481856 }
1849 x = try add(T, x, math.cast(T, digit) orelse return error.Overflow);
1857 x = try add(U, x, math.cast(U, digit) orelse return error.Overflow);
18501858 }
18511859
1852 return x;
1860 return if (T == U)
1861 x
1862 else
1863 math.cast(T, x) orelse return error.Overflow;
18531864}
18541865
18551866/// Parses the string `buf` as unsigned representation in the specified base