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 04:19:22+02:00
logc8ec7c1294db52edb0b2feb8a32ba0ff8ff03146
treef4aa71a0b65ca3e1b31503ec89fd985c5d7f84b3
parent913db1b1ca31df83cec0fbb9bd7e42751c0547b7
signaturebadge-check Signed by SSH key SHA256:7B/LJ7bpR1eX8aCXSr4mtd5M45VMPKcx9zY8e95b5QM

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(...@@ -567,8 +567,8 @@ pub fn innerParseFromValue(
567 switch (source) {567 switch (source) {
568 .float => |f| {568 .float => |f| {
569 if (@round(f) != f) return error.InvalidNumber;569 if (@round(f) != f) return error.InvalidNumber;
570 if (f > std.math.maxInt(T)) return error.Overflow;570 if (f > @as(@TypeOf(f), @floatFromInt(std.math.maxInt(T)))) return error.Overflow;
571 if (f < std.math.minInt(T)) return error.Overflow;571 if (f < @as(@TypeOf(f), @floatFromInt(std.math.minInt(T)))) return error.Overflow;
572 return @as(T, @intFromFloat(f));572 return @as(T, @intFromFloat(f));
573 },573 },
574 .integer => |i| {574 .integer => |i| {
...@@ -770,7 +770,7 @@ fn sliceToInt(comptime T: type, slice: []const u8) !T {...@@ -770,7 +770,7 @@ fn sliceToInt(comptime T: type, slice: []const u8) !T {
770 // Try to coerce a float to an integer.770 // Try to coerce a float to an integer.
771 const float = try std.fmt.parseFloat(f128, slice);771 const float = try std.fmt.parseFloat(f128, slice);
772 if (@round(float) != float) return error.InvalidNumber;772 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;
774 return @as(T, @intCast(@as(i128, @intFromFloat(float))));774 return @as(T, @intCast(@as(i128, @intFromFloat(float))));
775}775}
776776