authorgravatar for quae@daurnimator.comdaurnimator <quae@daurnimator.com> 2021-05-30 17:27:42+10:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2021-05-31 14:09:59+03:00
log57cf9f7ea6d018714cf4afa711799c67ff730f12
tree94633d543a4b4260fa6c51a24c3341bffbfe7aa3
parent556d3e3d800378efcf9d7d35ad63879cc06c5e88

std: by default, disallow trailing data when parsing json


1 files changed, 25 insertions(+), 6 deletions(-)

lib/std/json.zig+25-6
......@@ -1464,6 +1464,8 @@ pub const ParseOptions = struct {
14641464
14651465 /// If false, finding an unknown field returns an error.
14661466 ignore_unknown_fields: bool = false,
1467
1468 allow_trailing_data: bool = false,
14671469};
14681470
14691471fn skipValue(tokens: *TokenStream) !void {
......@@ -1627,6 +1629,8 @@ fn parseInternal(comptime T: type, token: Token, tokens: *TokenStream, options:
16271629 .ObjectEnd => break,
16281630 .String => |stringToken| {
16291631 const key_source_slice = stringToken.slice(tokens.slice, tokens.i - 1);
1632 var child_options = options;
1633 child_options.allow_trailing_data = true;
16301634 var found = false;
16311635 inline for (structInfo.fields) |field, i| {
16321636 // TODO: using switches here segfault the compiler (#2727?)
......@@ -1643,24 +1647,24 @@ fn parseInternal(comptime T: type, token: Token, tokens: *TokenStream, options:
16431647 // }
16441648 if (options.duplicate_field_behavior == .UseFirst) {
16451649 // unconditonally ignore value. for comptime fields, this skips check against default_value
1646 parseFree(field.field_type, try parse(field.field_type, tokens, options), options);
1650 parseFree(field.field_type, try parse(field.field_type, tokens, child_options), child_options);
16471651 found = true;
16481652 break;
16491653 } else if (options.duplicate_field_behavior == .Error) {
16501654 return error.DuplicateJSONField;
16511655 } else if (options.duplicate_field_behavior == .UseLast) {
16521656 if (!field.is_comptime) {
1653 parseFree(field.field_type, @field(r, field.name), options);
1657 parseFree(field.field_type, @field(r, field.name), child_options);
16541658 }
16551659 fields_seen[i] = false;
16561660 }
16571661 }
16581662 if (field.is_comptime) {
1659 if (!try parsesTo(field.field_type, field.default_value.?, tokens, options)) {
1663 if (!try parsesTo(field.field_type, field.default_value.?, tokens, child_options)) {
16601664 return error.UnexpectedValue;
16611665 }
16621666 } else {
1663 @field(r, field.name) = try parse(field.field_type, tokens, options);
1667 @field(r, field.name) = try parse(field.field_type, tokens, child_options);
16641668 }
16651669 fields_seen[i] = true;
16661670 found = true;
......@@ -1697,6 +1701,8 @@ fn parseInternal(comptime T: type, token: Token, tokens: *TokenStream, options:
16971701 .ArrayBegin => {
16981702 var r: T = undefined;
16991703 var i: usize = 0;
1704 var child_options = options;
1705 child_options.allow_trailing_data = true;
17001706 errdefer {
17011707 // Without the r.len check `r[i]` is not allowed
17021708 if (r.len > 0) while (true) : (i -= 1) {
......@@ -1705,7 +1711,7 @@ fn parseInternal(comptime T: type, token: Token, tokens: *TokenStream, options:
17051711 };
17061712 }
17071713 while (i < r.len) : (i += 1) {
1708 r[i] = try parse(arrayInfo.child, tokens, options);
1714 r[i] = try parse(arrayInfo.child, tokens, child_options);
17091715 }
17101716 const tok = (try tokens.next()) orelse return error.UnexpectedEndOfJson;
17111717 switch (tok) {
......@@ -1786,7 +1792,13 @@ fn parseInternal(comptime T: type, token: Token, tokens: *TokenStream, options:
17861792
17871793pub fn parse(comptime T: type, tokens: *TokenStream, options: ParseOptions) !T {
17881794 const token = (try tokens.next()) orelse return error.UnexpectedEndOfJson;
1789 return parseInternal(T, token, tokens, options);
1795 const r = try parseInternal(T, token, tokens, options);
1796 errdefer parseFree(T, r, options);
1797 if (!options.allow_trailing_data) {
1798 if ((try tokens.next()) != null) unreachable;
1799 assert(tokens.i >= tokens.slice.len);
1800 }
1801 return r;
17901802}
17911803
17921804/// Releases resources created by `parse`.
......@@ -1871,6 +1883,13 @@ test "parse into enum" {
18711883 try testing.expectError(error.InvalidEnumTag, parse(T, &TokenStream.init("\"Qux\""), ParseOptions{}));
18721884}
18731885
1886test "parse with trailing data" {
1887 try testing.expectEqual(false, try parse(bool, &TokenStream.init("falsed"), ParseOptions{ .allow_trailing_data = true }));
1888 try testing.expectError(error.InvalidTopLevelTrailing, parse(bool, &TokenStream.init("falsed"), ParseOptions{ .allow_trailing_data = false }));
1889 // trailing whitespace is okay
1890 try testing.expectEqual(false, try parse(bool, &TokenStream.init("false \n"), ParseOptions{ .allow_trailing_data = false }));
1891}
1892
18741893test "parse into that allocates a slice" {
18751894 try testing.expectError(error.AllocatorRequired, parse([]u8, &TokenStream.init("\"foo\""), ParseOptions{}));
18761895