-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Fix: std.json: Writing inf values returns InvalidJSON error #23258
base: master
Are you sure you want to change the base?
Changes from all commits
516e7b3
fa745c8
935b028
d107983
a8be9cd
3c83b3c
7a4724e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -453,6 +453,26 @@ test "nonportable numbers" { | |
try testStringify("\"9999999999999999\"", 9999999999999999, .{ .emit_nonportable_numbers_as_strings = true }); | ||
} | ||
|
||
test "invalid JSON values" { | ||
try std.testing.expectError(error.InvalidInput, stringifyAlloc(std.testing.allocator, std.math.inf(f32), .{})); | ||
try std.testing.expectError(error.InvalidInput, stringifyAlloc(std.testing.allocator, std.math.inf(f64), .{})); | ||
try std.testing.expectError(error.InvalidInput, stringifyAlloc(std.testing.allocator, std.math.nan(f32), .{})); | ||
try std.testing.expectError(error.InvalidInput, stringifyAlloc(std.testing.allocator, std.math.nan(f64), .{})); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think its worth adding an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doesn't look like there is test coverage of the does-not-fit-in-f64 path elsewhere, so for bonus points you could add some success-path coverage of that too. |
||
|
||
try std.testing.expectError(error.InvalidInput, stringifyAlloc(std.testing.allocator, .{ | ||
.a = std.math.inf(f32), | ||
}, .{})); | ||
try std.testing.expectError(error.InvalidInput, stringifyAlloc(std.testing.allocator, .{ | ||
.a = std.math.inf(f64), | ||
}, .{})); | ||
try std.testing.expectError(error.InvalidInput, stringifyAlloc(std.testing.allocator, .{ | ||
.a = std.math.nan(f32), | ||
}, .{})); | ||
try std.testing.expectError(error.InvalidInput, stringifyAlloc(std.testing.allocator, .{ | ||
.a = std.math.nan(f64), | ||
}, .{})); | ||
} | ||
|
||
test "stringify raw streaming" { | ||
var out_buf: [1024]u8 = undefined; | ||
var slice_stream = std.io.fixedBufferStream(&out_buf); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not 100% sure, but I think you can combine the
.float
and.comptime_float
cases back together if you do thef64
cast first (which also makes a comptime type a runtime type) and use that as the argument toisInf
/isNan
:(I think you should also use the
fval
in the first print too.... You might even refactor this code a bit and move the valueStart/valueDone such that they're done once? Not really your change though.)Also, I'm not sure ... but is the
fval == value
check redundant with the isNan check? Ah, no... this is for detecting floats that don't fit in an f64 and rendering them as strings.