authorgravatar for john.schmidt.h@gmail.comJohn Schmidt <john.schmidt.h@gmail.com> 2023-02-04 18:28:36+01:00
committergravatar for john.schmidt.h@gmail.comJohn Schmidt <john.schmidt.h@gmail.com> 2023-02-04 18:28:36+01:00
log73c857415eafc1d0856b6917cada94d8acde41e0
tree7278b0ccd1a7b0d74ff84c701238a72fd563bdd6
parentc1f71963a9b8c913acee0fc167e0a4ecdecd2890

std.json: fix parsing of structs with default value const pointers


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

lib/std/json.zig+1-1
...@@ -1639,7 +1639,7 @@ fn parseInternal(...@@ -1639,7 +1639,7 @@ fn parseInternal(
1639 const allocator = options.allocator orelse return error.AllocatorRequired;1639 const allocator = options.allocator orelse return error.AllocatorRequired;
1640 switch (ptrInfo.size) {1640 switch (ptrInfo.size) {
1641 .One => {1641 .One => {
1642 const r: T = try allocator.create(ptrInfo.child);1642 const r: *ptrInfo.child = try allocator.create(ptrInfo.child);
1643 errdefer allocator.destroy(r);1643 errdefer allocator.destroy(r);
1644 r.* = try parseInternal(ptrInfo.child, token, tokens, options);1644 r.* = try parseInternal(ptrInfo.child, token, tokens, options);
1645 return r;1645 return r;
lib/std/json/test.zig+8
...@@ -2238,6 +2238,14 @@ test "parse into struct with no fields" {...@@ -2238,6 +2238,14 @@ test "parse into struct with no fields" {
2238 try testing.expectEqual(T{}, try parse(T, &ts, ParseOptions{}));2238 try testing.expectEqual(T{}, try parse(T, &ts, ParseOptions{}));
2239}2239}
22402240
2241const test_const_value: usize = 123;
2242
2243test "parse into struct with default const pointer field" {
2244 const T = struct { a: *const usize = &test_const_value };
2245 var ts = TokenStream.init("{}");
2246 try testing.expectEqual(T{}, try parse(T, &ts, .{}));
2247}
2248
2241test "parse into struct where destination and source lengths mismatch" {2249test "parse into struct where destination and source lengths mismatch" {
2242 const T = struct { a: [2]u8 };2250 const T = struct { a: [2]u8 };
2243 var ts = TokenStream.init("{\"a\": \"bbb\"}");2251 var ts = TokenStream.init("{\"a\": \"bbb\"}");