authorgravatar for 67233402+Raiden1411@users.noreply.github.comRaiden1411 <67233402+Raiden1411@users.noreply.github.com> 2025-08-21 10:38:08+01:00
committergravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2025-08-22 02:50:26+02:00
log55daefdb108ea0bf0406dcc3e0569f7b72c639b3
tree76388e614570ec73b2424b7ea799642d016750e0
parentba726ab65a1a6d33823d7bb8a155d8e379f58ba7

std: remove lossy int to float coercion on json parse


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

lib/std/json/static.zig+3-3
......@@ -567,8 +567,8 @@ pub fn innerParseFromValue(
567567 switch (source) {
568568 .float => |f| {
569569 if (@round(f) != f) return error.InvalidNumber;
570 if (f > std.math.maxInt(T)) return error.Overflow;
571 if (f < std.math.minInt(T)) return error.Overflow;
570 if (f > @as(@TypeOf(f), @floatFromInt(std.math.maxInt(T)))) return error.Overflow;
571 if (f < @as(@TypeOf(f), @floatFromInt(std.math.minInt(T)))) return error.Overflow;
572572 return @as(T, @intFromFloat(f));
573573 },
574574 .integer => |i| {
......@@ -770,7 +770,7 @@ fn sliceToInt(comptime T: type, slice: []const u8) !T {
770770 // Try to coerce a float to an integer.
771771 const float = try std.fmt.parseFloat(f128, slice);
772772 if (@round(float) != float) return error.InvalidNumber;
773 if (float > std.math.maxInt(T) or float < std.math.minInt(T)) return error.Overflow;
773 if (float > @as(f128, @floatFromInt(std.math.maxInt(T))) or float < @as(f128, @floatFromInt(std.math.minInt(T)))) return error.Overflow;
774774 return @as(T, @intCast(@as(i128, @intFromFloat(float))));
775775}
776776