zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit 4c116841840bfa7f0068bef23984dd832da8a54d (tree)
parent f68d3c63df0486c7039732ef442c87fcd7c6fc57
Author: Jon-Eric Cook <joneric.cook@gmail.com>
Date:   Sat, 28 Jan 2023 08:26:36 -0800

std.json: check output and source lengths in `std.json`


Diffstat:
Mlib/std/json.zig | 3++-
Mlib/std/json/test.zig | 6++++++
2 files changed, 8 insertions(+), 1 deletion(-)

diff --git a/lib/std/json.zig b/lib/std/json.zig @@ -1384,7 +1384,7 @@ fn ParseInternalErrorImpl(comptime T: type, comptime inferred_types: []const typ return errors; }, .Array => |arrayInfo| { - return error{ UnexpectedEndOfJson, UnexpectedToken } || TokenStream.Error || + return error{ UnexpectedEndOfJson, UnexpectedToken, LengthMismatch } || TokenStream.Error || UnescapeValidStringError || ParseInternalErrorImpl(arrayInfo.child, inferred_types ++ [_]type{T}); }, @@ -1625,6 +1625,7 @@ fn parseInternal( if (arrayInfo.child != u8) return error.UnexpectedToken; var r: T = undefined; const source_slice = stringToken.slice(tokens.slice, tokens.i - 1); + if (r.len != stringToken.decodedLength()) return error.LengthMismatch; switch (stringToken.escapes) { .None => mem.copy(u8, &r, source_slice), .Some => try unescapeValidString(&r, source_slice), diff --git a/lib/std/json/test.zig b/lib/std/json/test.zig @@ -2238,6 +2238,12 @@ test "parse into struct with no fields" { try testing.expectEqual(T{}, try parse(T, &ts, ParseOptions{})); } +test "parse into struct where destination and source lengths mismatch" { + const T = struct { a: [2]u8 }; + var ts = TokenStream.init("{\"a\": \"bbb\"}"); + try testing.expectError(error.LengthMismatch, parse(T, &ts, ParseOptions{})); +} + test "parse into struct with misc fields" { @setEvalBranchQuota(10000); const options = ParseOptions{ .allocator = testing.allocator };