fix: avoid dangling Project pointer crash in LSP and git handlers

This commit is contained in:
CJ van den Berg 2026-03-23 13:05:26 +01:00
parent 76e4a843c7
commit 11d0e9e7e3
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
2 changed files with 21 additions and 17 deletions

View file

@ -1059,7 +1059,6 @@ fn send_goto_request(self: *Self, from: tp.pid_ref, file_path: []const u8, row:
const handler: struct {
from: tp.pid,
name: []const u8,
project: *Self,
pub fn deinit(self_: *@This()) void {
std.heap.c_allocator.free(self_.name);
@ -1084,7 +1083,6 @@ fn send_goto_request(self: *Self, from: tp.pid_ref, file_path: []const u8, row:
} = .{
.from = from.clone(),
.name = try std.heap.c_allocator.dupe(u8, self.name),
.project = self,
};
lsp.send_request(self.allocator, method, .{
@ -1157,7 +1155,6 @@ pub fn references(self: *Self, from: tp.pid_ref, file_path: []const u8, row: usi
const handler: struct {
from: tp.pid,
name: []const u8,
project: *Self,
pub fn deinit(self_: *@This()) void {
std.heap.c_allocator.free(self_.name);
@ -1170,13 +1167,12 @@ pub fn references(self: *Self, from: tp.pid_ref, file_path: []const u8, row: usi
return;
} else if (try cbor.match(response.buf, .{ "child", tp.string, "result", tp.extract_cbor(&locations) })) {
const count = try send_reference_list("REF", self_.from.ref(), locations, self_.name);
self_.project.logger_lsp.print("found {d} references", .{count});
std.log.info("found {d} references", .{count});
}
}
} = .{
.from = from.clone(),
.name = try std.heap.c_allocator.dupe(u8, self.name),
.project = self,
};
lsp.send_request(self.allocator, "textDocument/references", .{
@ -2314,7 +2310,6 @@ fn send_lsp_init_request(self: *Self, from: tp.pid_ref, lsp: *const LSP, project
from: tp.pid,
language_server: []const u8,
lsp: LSP,
project: *Self,
project_path: []const u8,
pub fn deinit(self_: *@This()) void {
@ -2327,7 +2322,7 @@ fn send_lsp_init_request(self: *Self, from: tp.pid_ref, lsp: *const LSP, project
pub fn receive(self_: @This(), response: tp.message) !void {
self_.lsp.send_notification("initialized", .{}) catch return error.LspFailed;
if (self_.lsp.pid.expired()) return error.LspFailed;
self_.project.logger_lsp.print("initialized LSP: {f}", .{fmt_lsp_name_func(self_.language_server)});
std.log.info("initialized LSP: {f}", .{fmt_lsp_name_func(self_.language_server)});
var result: []const u8 = undefined;
if (try cbor.match(response.buf, .{ "child", tp.string, "result", tp.null_ })) {
@ -2344,7 +2339,6 @@ fn send_lsp_init_request(self: *Self, from: tp.pid_ref, lsp: *const LSP, project
.allocator = lsp.allocator,
.pid = lsp.pid.clone(),
},
.project = self,
.project_path = try std.heap.c_allocator.dupe(u8, project_path),
};
@ -2929,7 +2923,7 @@ pub fn request_vcs_id(self: *Self, file_path: []const u8) error{OutOfMemory}!voi
const request = try self.allocator.create(VcsIdRequest);
request.* = .{
.allocator = self.allocator,
.project = self,
.project = @intFromPtr(self),
.file_path = try self.allocator.dupe(u8, file_path),
};
git.rev_parse(@intFromPtr(request), "HEAD", file_path) catch |e|
@ -2938,7 +2932,7 @@ pub fn request_vcs_id(self: *Self, file_path: []const u8) error{OutOfMemory}!voi
pub const VcsIdRequest = struct {
allocator: std.mem.Allocator,
project: *Self,
project: usize,
file_path: []const u8,
pub fn deinit(self: *@This()) void {
@ -2951,7 +2945,7 @@ pub fn request_vcs_content(self: *Self, file_path: []const u8, vcs_id: []const u
const request = try self.allocator.create(VcsContentRequest);
request.* = .{
.allocator = self.allocator,
.project = self,
.project = @intFromPtr(self),
.file_path = try self.allocator.dupe(u8, file_path),
.vcs_id = try self.allocator.dupe(u8, vcs_id),
};
@ -2961,7 +2955,7 @@ pub fn request_vcs_content(self: *Self, file_path: []const u8, vcs_id: []const u
pub const VcsContentRequest = struct {
allocator: std.mem.Allocator,
project: *Self,
project: usize,
file_path: []const u8,
vcs_id: []const u8,
@ -3007,7 +3001,7 @@ pub fn request_vcs_blame(self: *Self, file_path: []const u8) error{OutOfMemory}!
const request = try self.allocator.create(GitBlameRequest);
request.* = .{
.allocator = self.allocator,
.project = self,
.project = @intFromPtr(self),
.file_path = try self.allocator.dupe(u8, file_path),
};
git.blame(@intFromPtr(request), file_path) catch |e|
@ -3016,7 +3010,7 @@ pub fn request_vcs_blame(self: *Self, file_path: []const u8) error{OutOfMemory}!
pub const GitBlameRequest = struct {
allocator: std.mem.Allocator,
project: *Self,
project: usize,
file_path: []const u8,
pub fn deinit(self: *@This()) void {

View file

@ -429,13 +429,16 @@ const Process = struct {
project.walk_tree_done(self.parent.ref()) catch |e| return from.forward_error(e, @errorReturnTrace()) catch error.ClientFailed;
} else if (try cbor.match(m.buf, .{ "git", tp.extract(&context), "rev_parse", tp.more })) {
const request: *Project.VcsIdRequest = @ptrFromInt(context);
request.project.process_git_response(self.parent.ref(), m) catch |e| self.logger.err("git-rev-parse", e);
if (self.project_from_ref(request.project)) |project|
project.process_git_response(self.parent.ref(), m) catch |e| self.logger.err("git-rev-parse", e);
} else if (try cbor.match(m.buf, .{ "git", tp.extract(&context), "cat_file", tp.more })) {
const request: *Project.VcsContentRequest = @ptrFromInt(context);
request.project.process_git_response(self.parent.ref(), m) catch |e| self.logger.err("git-cat-file", e);
if (self.project_from_ref(request.project)) |project|
project.process_git_response(self.parent.ref(), m) catch |e| self.logger.err("git-cat-file", e);
} else if (try cbor.match(m.buf, .{ "git", tp.extract(&context), "blame", tp.more })) {
const request: *Project.GitBlameRequest = @ptrFromInt(context);
request.project.process_git_response(self.parent.ref(), m) catch |e| self.logger.err("git-blame", e);
if (self.project_from_ref(request.project)) |project|
project.process_git_response(self.parent.ref(), m) catch |e| self.logger.err("git-blame", e);
} else if (try cbor.match(m.buf, .{ "git", tp.extract(&context), tp.more })) {
const project: *Project = @ptrFromInt(context);
project.process_git(self.parent.ref(), m) catch {};
@ -941,6 +944,13 @@ const Process = struct {
}.less_fn;
std.mem.sort(RecentProject, recent_projects.items, {}, less_fn);
}
fn project_from_ref(self: *const Process, project_ref: usize) ?*Project {
var iter = self.projects.valueIterator();
while (iter.next()) |project| if (@intFromPtr(project.*) == project_ref)
return project.*;
return null;
}
};
fn request_path_files_async(a_: std.mem.Allocator, parent_: tp.pid_ref, project_: *Project, max_: usize, path_: []const u8) (SpawnError || std.fs.Dir.OpenError)!void {