From 73c857415eafc1d0856b6917cada94d8acde41e0 Mon Sep 17 00:00:00 2001 From: John Schmidt Date: Sat, 4 Feb 2023 18:28:36 +0100 Subject: [PATCH] std.json: fix parsing of structs with default value const pointers --- lib/std/json.zig | 2 +- lib/std/json/test.zig | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/std/json.zig b/lib/std/json.zig index 92afeead90..41bfb30bc5 100644 --- a/lib/std/json.zig +++ b/lib/std/json.zig @@ -1639,7 +1639,7 @@ fn parseInternal( const allocator = options.allocator orelse return error.AllocatorRequired; switch (ptrInfo.size) { .One => { - const r: T = try allocator.create(ptrInfo.child); + const r: *ptrInfo.child = try allocator.create(ptrInfo.child); errdefer allocator.destroy(r); r.* = try parseInternal(ptrInfo.child, token, tokens, options); return r; diff --git a/lib/std/json/test.zig b/lib/std/json/test.zig index 0bf0797587..70385acfb5 100644 --- a/lib/std/json/test.zig +++ b/lib/std/json/test.zig @@ -2238,6 +2238,14 @@ test "parse into struct with no fields" { try testing.expectEqual(T{}, try parse(T, &ts, ParseOptions{})); } +const test_const_value: usize = 123; + +test "parse into struct with default const pointer field" { + const T = struct { a: *const usize = &test_const_value }; + var ts = TokenStream.init("{}"); + try testing.expectEqual(T{}, try parse(T, &ts, .{})); +} + test "parse into struct where destination and source lengths mismatch" { const T = struct { a: [2]u8 }; var ts = TokenStream.init("{\"a\": \"bbb\"}");