mirror of
https://github.com/ziglang/zig.git
synced 2026-01-20 22:35:24 +00:00
fix json parser crashing on empty input
remove unreachable code
This commit is contained in:
parent
05003d533a
commit
a0abd3be85
@ -867,6 +867,8 @@ pub const TokenStream = struct {
|
||||
parser: StreamingParser,
|
||||
token: ?Token,
|
||||
|
||||
pub const Error = StreamingParser.Error || error.Incomplete;
|
||||
|
||||
pub fn init(slice: []const u8) TokenStream {
|
||||
return TokenStream{
|
||||
.i = 0,
|
||||
@ -896,16 +898,11 @@ pub const TokenStream = struct {
|
||||
}
|
||||
}
|
||||
|
||||
if (self.i > self.slice.len) {
|
||||
try self.parser.feed(' ', &t1, &t2);
|
||||
self.i += 1;
|
||||
|
||||
if (t1) |token| {
|
||||
return token;
|
||||
}
|
||||
if(self.parser.complete){
|
||||
return null;
|
||||
} else {
|
||||
return error.Incomplete;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
@ -1455,3 +1452,9 @@ test "write json then parse it" {
|
||||
testing.expect(tree.root.Object.get("array").?.value.Array.at(1).Float == 12.34);
|
||||
testing.expect(mem.eql(u8, tree.root.Object.get("str").?.value.String, "hello"));
|
||||
}
|
||||
|
||||
test "parsing empty string does not crash" {
|
||||
var p = Parser.init(debug.global_allocator, false);
|
||||
defer p.deinit();
|
||||
testing.expectError(error.Incomplete, p.parse(""));
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user