Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Json: stricter number parsing #21962

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/std/json/static.zig
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ pub fn innerParse(
const token = try source.nextAllocMax(allocator, .alloc_if_needed, options.max_value_len.?);
defer freeAllocated(allocator, token);
const slice = switch (token) {
inline .number, .allocated_number, .string, .allocated_string => |slice| slice,
inline .number, .allocated_number => |slice| slice,
else => return error.UnexpectedToken,
};
return try std.fmt.parseFloat(T, slice);
Expand All @@ -239,7 +239,7 @@ pub fn innerParse(
const token = try source.nextAllocMax(allocator, .alloc_if_needed, options.max_value_len.?);
defer freeAllocated(allocator, token);
const slice = switch (token) {
inline .number, .allocated_number, .string, .allocated_string => |slice| slice,
inline .number, .allocated_number => |slice| slice,
else => return error.UnexpectedToken,
};
return sliceToInt(T, slice);
Expand Down
7 changes: 5 additions & 2 deletions lib/std/json/static_test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -384,8 +384,11 @@ test "parse" {
try testing.expectEqual("foo".*, try parseFromSliceLeaky([3]u8, testing.allocator, "[102, 111, 111]", .{}));
try testing.expectEqual(undefined, try parseFromSliceLeaky([0]u8, testing.allocator, "[]", .{}));

try testing.expectEqual(12345678901234567890, try parseFromSliceLeaky(u64, testing.allocator, "\"12345678901234567890\"", .{}));
try testing.expectEqual(123.456, try parseFromSliceLeaky(f64, testing.allocator, "\"123.456\"", .{}));
try testing.expectEqual("12345678901234567890".*, try parseFromSliceLeaky([20]u8, testing.allocator, "\"12345678901234567890\"", .{}));
try testing.expectEqual(12345678901234567890, try parseFromSliceLeaky(u64, testing.allocator, "12345678901234567890", .{}));

try testing.expectEqual("123.456".*, try parseFromSliceLeaky([7]u8, testing.allocator, "\"123.456\"", .{}));
try testing.expectEqual(123.456, try parseFromSliceLeaky(f64, testing.allocator, "123.456", .{}));
}

test "parse into enum" {
Expand Down
Loading