diff options
| author | daurnimator <quae@daurnimator.com> | 2021-05-30 17:08:27 +1000 |
|---|---|---|
| committer | Veikka Tuominen <git@vexu.eu> | 2021-05-31 14:09:59 +0300 |
| commit | 556d3e3d800378efcf9d7d35ad63879cc06c5e88 (patch) | |
| tree | d7a6e09c0a76d9ad3c0b8a969064303548b8ef6f /lib/std/json.zig | |
| parent | 11ae6c42c1851fc44b286e5a76f73d55ea0bca5e (diff) | |
| download | zig-556d3e3d800378efcf9d7d35ad63879cc06c5e88.tar.gz zig-556d3e3d800378efcf9d7d35ad63879cc06c5e88.zip | |
std: fix json.parse with 0 length arrays
Diffstat (limited to 'lib/std/json.zig')
| -rw-r--r-- | lib/std/json.zig | 6 |
1 files changed, 4 insertions, 2 deletions
diff --git a/lib/std/json.zig b/lib/std/json.zig index 68d7023b2c..5b4203501b 100644 --- a/lib/std/json.zig +++ b/lib/std/json.zig @@ -1698,10 +1698,11 @@ fn parseInternal(comptime T: type, token: Token, tokens: *TokenStream, options: var r: T = undefined; var i: usize = 0; errdefer { - while (true) : (i -= 1) { + // Without the r.len check `r[i]` is not allowed + if (r.len > 0) while (true) : (i -= 1) { parseFree(arrayInfo.child, r[i], options); if (i == 0) break; - } + }; } while (i < r.len) : (i += 1) { r[i] = try parse(arrayInfo.child, tokens, options); @@ -1854,6 +1855,7 @@ test "parse" { try testing.expectEqual(@as([3]u8, "foo".*), try parse([3]u8, &TokenStream.init("\"foo\""), ParseOptions{})); try testing.expectEqual(@as([3]u8, "foo".*), try parse([3]u8, &TokenStream.init("[102, 111, 111]"), ParseOptions{})); + try testing.expectEqual(@as([0]u8, undefined), try parse([0]u8, &TokenStream.init("[]"), ParseOptions{})); } test "parse into enum" { |
