authorgravatar for joneric.cook@gmail.comJon-Eric Cook <joneric.cook@gmail.com> 2023-01-28 08:26:36-08:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2023-01-28 16:26:36+00:00
log4c116841840bfa7f0068bef23984dd832da8a54d
treeeef882bbd8b87ab483b47b7e5b01951fab1a3c81
parentf68d3c63df0486c7039732ef442c87fcd7c6fc57
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

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


2 files changed, 8 insertions(+), 1 deletions(-)

lib/std/json.zig+2-1
......@@ -1384,7 +1384,7 @@ fn ParseInternalErrorImpl(comptime T: type, comptime inferred_types: []const typ
13841384 return errors;
13851385 },
13861386 .Array => |arrayInfo| {
1387 return error{ UnexpectedEndOfJson, UnexpectedToken } || TokenStream.Error ||
1387 return error{ UnexpectedEndOfJson, UnexpectedToken, LengthMismatch } || TokenStream.Error ||
13881388 UnescapeValidStringError ||
13891389 ParseInternalErrorImpl(arrayInfo.child, inferred_types ++ [_]type{T});
13901390 },
......@@ -1625,6 +1625,7 @@ fn parseInternal(
16251625 if (arrayInfo.child != u8) return error.UnexpectedToken;
16261626 var r: T = undefined;
16271627 const source_slice = stringToken.slice(tokens.slice, tokens.i - 1);
1628 if (r.len != stringToken.decodedLength()) return error.LengthMismatch;
16281629 switch (stringToken.escapes) {
16291630 .None => mem.copy(u8, &r, source_slice),
16301631 .Some => try unescapeValidString(&r, source_slice),
lib/std/json/test.zig+6
......@@ -2238,6 +2238,12 @@ test "parse into struct with no fields" {
22382238 try testing.expectEqual(T{}, try parse(T, &ts, ParseOptions{}));
22392239}
22402240
2241test "parse into struct where destination and source lengths mismatch" {
2242 const T = struct { a: [2]u8 };
2243 var ts = TokenStream.init("{\"a\": \"bbb\"}");
2244 try testing.expectError(error.LengthMismatch, parse(T, &ts, ParseOptions{}));
2245}
2246
22412247test "parse into struct with misc fields" {
22422248 @setEvalBranchQuota(10000);
22432249 const options = ParseOptions{ .allocator = testing.allocator };