diff options
Diffstat (limited to 'lib/std')
| -rw-r--r-- | lib/std/json.zig | 3 | ||||
| -rw-r--r-- | lib/std/json/test.zig | 6 |
2 files changed, 8 insertions, 1 deletions
diff --git a/lib/std/json.zig b/lib/std/json.zig index f16d70da80..92afeead90 100644 --- 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 index 2a590fdf15..0bf0797587 100644 --- 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 }; |
