diff options
author | 2022-09-16 03:20:28 -0700 | |
---|---|---|
committer | 2022-09-16 03:20:28 -0700 | |
commit | 453eaf6871afdde6f1fe85c0b1e3a44cbd980b10 (patch) | |
tree | 7f9da162cb3255894a3e8f0f7dc0ef1de2800c5a /src/json_parser.zig | |
parent | 0ce709d96abb48c747f5c93033c9a80fe79ee3bc (diff) | |
download | bun-453eaf6871afdde6f1fe85c0b1e3a44cbd980b10.tar.gz bun-453eaf6871afdde6f1fe85c0b1e3a44cbd980b10.tar.zst bun-453eaf6871afdde6f1fe85c0b1e3a44cbd980b10.zip |
Fix crash when parsing empty JSON file
Diffstat (limited to 'src/json_parser.zig')
-rw-r--r-- | src/json_parser.zig | 16 |
1 files changed, 11 insertions, 5 deletions
diff --git a/src/json_parser.zig b/src/json_parser.zig index cecb26ba8..79c07257e 100644 --- a/src/json_parser.zig +++ b/src/json_parser.zig @@ -738,8 +738,9 @@ pub fn ParseJSONUTF8( log: *logger.Log, allocator: std.mem.Allocator, ) !Expr { - var parser = try JSONParser.init(allocator, source.*, log); - switch (source.contents.len) { + const len = source.contents.len; + + switch (len) { // This is to be consisntent with how disabled JS files are handled 0 => { return Expr{ .loc = logger.Loc{ .start = 0 }, .data = empty_object_data }; @@ -757,11 +758,13 @@ pub fn ParseJSONUTF8( else => {}, } + var parser = try JSONParser.init(allocator, source.*, log); + std.debug.assert(parser.source().contents.len > 0); + return try parser.parseExpr(false, true); } pub fn ParseJSONForMacro(source: *const logger.Source, log: *logger.Log, allocator: std.mem.Allocator) !Expr { - var parser = try JSONParserForMacro.init(allocator, source.*, log); switch (source.contents.len) { // This is to be consisntent with how disabled JS files are handled 0 => { @@ -780,6 +783,8 @@ pub fn ParseJSONForMacro(source: *const logger.Source, log: *logger.Log, allocat else => {}, } + var parser = try JSONParserForMacro.init(allocator, source.*, log); + return try parser.parseExpr(false, false); } @@ -795,7 +800,6 @@ pub const JSONParseResult = struct { }; pub fn ParseJSONForBundling(source: *const logger.Source, log: *logger.Log, allocator: std.mem.Allocator) !JSONParseResult { - var parser = try JSONParser.init(allocator, source.*, log); switch (source.contents.len) { // This is to be consisntent with how disabled JS files are handled 0 => { @@ -814,6 +818,7 @@ pub fn ParseJSONForBundling(source: *const logger.Source, log: *logger.Log, allo else => {}, } + var parser = try JSONParser.init(allocator, source.*, log); const result = try parser.parseExpr(false, true); return JSONParseResult{ .tag = if (!LEXER_DEBUGGER_WORKAROUND and parser.lexer.is_ascii_only) JSONParseResult.Tag.ascii else JSONParseResult.Tag.expr, @@ -824,7 +829,6 @@ pub fn ParseJSONForBundling(source: *const logger.Source, log: *logger.Log, allo // threadlocal var env_json_auto_quote_buffer: MutableString = undefined; // threadlocal var env_json_auto_quote_buffer_loaded: bool = false; pub fn ParseEnvJSON(source: *const logger.Source, log: *logger.Log, allocator: std.mem.Allocator) !Expr { - var parser = try DotEnvJSONParser.init(allocator, source.*, log); switch (source.contents.len) { // This is to be consisntent with how disabled JS files are handled 0 => { @@ -843,6 +847,8 @@ pub fn ParseEnvJSON(source: *const logger.Source, log: *logger.Log, allocator: s else => {}, } + var parser = try DotEnvJSONParser.init(allocator, source.*, log); + switch (source.contents[0]) { '{', '[', '0'...'9', '"', '\'' => { return try parser.parseExpr(false, false); |