fix crashes found through fuzzing (#866)

This commit is contained in:
Techatrix 2022-12-30 23:42:28 +00:00 committed by GitHub
parent c88562ca78
commit f473088b64
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 23 additions and 16 deletions

View File

@ -2486,14 +2486,14 @@ fn foldingRangeHandler(server: *Server, request: types.FoldingRangeParams) !?[]t
.@"if", .if_simple => {
const if_full = ast.ifFull(handle.tree, node);
const start_tok_1 = handle.tree.lastToken(if_full.ast.cond_expr);
const end_tok_1 = handle.tree.lastToken(if_full.ast.then_expr);
const start_tok_1 = ast.lastToken(handle.tree, if_full.ast.cond_expr);
const end_tok_1 = ast.lastToken(handle.tree, if_full.ast.then_expr);
_ = try helper.maybeAddTokRange(&ranges, handle.tree, start_tok_1, end_tok_1, .inclusive, server.offset_encoding);
if (if_full.ast.else_expr == 0) continue;
const start_tok_2 = if_full.else_token;
const end_tok_2 = handle.tree.lastToken(if_full.ast.else_expr);
const end_tok_2 = ast.lastToken(handle.tree, if_full.ast.else_expr);
_ = try helper.maybeAddTokRange(&ranges, handle.tree, start_tok_2, end_tok_2, .inclusive, server.offset_encoding);
},
@ -2507,14 +2507,14 @@ fn foldingRangeHandler(server: *Server, request: types.FoldingRangeParams) !?[]t
=> {
const loop_full = ast.whileAst(handle.tree, node).?;
const start_tok_1 = handle.tree.lastToken(loop_full.ast.cond_expr);
const end_tok_1 = handle.tree.lastToken(loop_full.ast.then_expr);
const start_tok_1 = ast.lastToken(handle.tree, loop_full.ast.cond_expr);
const end_tok_1 = ast.lastToken(handle.tree, loop_full.ast.then_expr);
_ = try helper.maybeAddTokRange(&ranges, handle.tree, start_tok_1, end_tok_1, .inclusive, server.offset_encoding);
if (loop_full.ast.else_expr == 0) continue;
const start_tok_2 = loop_full.else_token;
const end_tok_2 = handle.tree.lastToken(loop_full.ast.else_expr);
const end_tok_2 = ast.lastToken(handle.tree, loop_full.ast.else_expr);
_ = try helper.maybeAddTokRange(&ranges, handle.tree, start_tok_2, end_tok_2, .inclusive, server.offset_encoding);
},
@ -2552,7 +2552,7 @@ fn foldingRangeHandler(server: *Server, request: types.FoldingRangeParams) !?[]t
break :decl_node_blk;
const list_start_tok: Ast.TokenIndex = fn_proto.lparen;
const list_end_tok: Ast.TokenIndex = handle.tree.lastToken(fn_proto.ast.proto_node);
const list_end_tok: Ast.TokenIndex = ast.lastToken(handle.tree, fn_proto.ast.proto_node);
if (handle.tree.tokensOnSameLine(list_start_tok, list_end_tok)) break :decl_node_blk;
try ranges.ensureUnusedCapacity(1 + fn_proto.ast.params.len); // best guess, doesn't include anytype params
@ -2579,7 +2579,7 @@ fn foldingRangeHandler(server: *Server, request: types.FoldingRangeParams) !?[]t
// .grouped_expression,
=> {
const start_tok = handle.tree.firstToken(node);
const end_tok = handle.tree.lastToken(node);
const end_tok = ast.lastToken(handle.tree, node);
_ = try helper.maybeAddTokRange(&ranges, handle.tree, start_tok, end_tok, .inclusive, server.offset_encoding);
},
@ -2626,7 +2626,7 @@ fn foldingRangeHandler(server: *Server, request: types.FoldingRangeParams) !?[]t
}
const start_tok = handle.tree.firstToken(node);
const end_tok = handle.tree.lastToken(node);
const end_tok = ast.lastToken(handle.tree, node);
_ = try helper.maybeAddTokRange(&ranges, handle.tree, start_tok, end_tok, .exclusive, server.offset_encoding);
},
}

View File

@ -1466,6 +1466,8 @@ pub fn getImportStr(tree: Ast, node: Ast.Node.Index, source_index: usize) ?[]con
if (params.len != 1) return null;
if(node_tags[params[0]] != .string_literal) return null;
const import_str = tree.tokenSlice(tree.nodes.items(.main_token)[params[0]]);
return import_str[1 .. import_str.len - 1];
}

View File

@ -539,10 +539,15 @@ pub fn lastToken(tree: Ast, node: Ast.Node.Index) Ast.TokenIndex {
.container_decl_arg_trailing,
.switch_comma,
=> {
const members = tree.extraData(datas[n].rhs, Node.SubRange);
std.debug.assert(members.end - members.start > 0);
end_offset += 2; // for the comma + rbrace
n = tree.extra_data[members.end - 1]; // last parameter
if(datas[n].rhs != 0) {
const members = tree.extraData(datas[n].rhs, Node.SubRange);
std.debug.assert(members.end - members.start > 0);
end_offset += 2; // for the comma + rbrace
n = tree.extra_data[members.end - 1]; // last parameter
} else {
end_offset += 1;
n = datas[n].lhs;
}
},
.array_init_dot,
.struct_init_dot,

View File

@ -22,7 +22,7 @@ pub const inlay_hints_max_inline_children = 12;
/// checks whether node is inside the range
fn isNodeInRange(tree: Ast, node: Ast.Node.Index, range: types.Range) bool {
const endLocation = tree.tokenLocation(0, tree.lastToken(node));
const endLocation = tree.tokenLocation(0, ast.lastToken(tree, node));
if (endLocation.line < range.start.line) return false;
const beginLocation = tree.tokenLocation(0, tree.firstToken(node));

View File

@ -289,7 +289,7 @@ fn writeNodeTokens(builder: *Builder, maybe_node: ?Ast.Node.Index) WriteTokensEr
const token_tags = tree.tokens.items(.tag);
const node_data = tree.nodes.items(.data);
const main_tokens = tree.nodes.items(.main_token);
if (node == 0 or node > node_data.len) return;
if (node == 0 or node >= node_data.len) return;
var allocator = builder.arena.allocator();

View File

@ -34,7 +34,7 @@ test "foldingRange - #801" {
\\ };
\\}
,
\\[]
\\[{"startLine":1,"endLine":4},{"startLine":0,"endLine":5}]
);
}