authorgravatar for 14938807+xackus@users.noreply.github.comxackus <14938807+xackus@users.noreply.github.com> 2019-10-27 10:45:54+01:00
committergravatar for 14938807+xackus@users.noreply.github.comxackus <14938807+xackus@users.noreply.github.com> 2019-10-27 10:45:54+01:00
loga0abd3be85a6c24a913ee650e1fd51bf3f457c68
treea2289356723223a1eddeb65cb7277209dbd81d76
parent05003d533a8a4f4c49f7a251e26b5dfff5f278b3

fix json parser crashing on empty input

remove unreachable code

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

lib/std/json.zig+12-9
......@@ -867,6 +867,8 @@ pub const TokenStream = struct {
867867 parser: StreamingParser,
868868 token: ?Token,
869869
870 pub const Error = StreamingParser.Error || error.Incomplete;
871
870872 pub fn init(slice: []const u8) TokenStream {
871873 return TokenStream{
872874 .i = 0,
......@@ -896,16 +898,11 @@ pub const TokenStream = struct {
896898 }
897899 }
898900
899 if (self.i > self.slice.len) {
900 try self.parser.feed(' ', &t1, &t2);
901 self.i += 1;
902
903 if (t1) |token| {
904 return token;
905 }
901 if(self.parser.complete){
902 return null;
903 } else {
904 return error.Incomplete;
906905 }
907
908 return null;
909906 }
910907};
911908
......@@ -1455,3 +1452,9 @@ test "write json then parse it" {
14551452 testing.expect(tree.root.Object.get("array").?.value.Array.at(1).Float == 12.34);
14561453 testing.expect(mem.eql(u8, tree.root.Object.get("str").?.value.String, "hello"));
14571454}
1455
1456test "parsing empty string does not crash" {
1457 var p = Parser.init(debug.global_allocator, false);
1458 defer p.deinit();
1459 testing.expectError(error.Incomplete, p.parse(""));
1460}