open_file: fix possible underflow in delete_to_previous_path_segment

When opening a file, if one writes only "/" then presses ctrl+backspace the "path" slice will end up [0 .. -1].

This crashes in debug build, and possibly in some rare circumstances in release build.
This commit is contained in:
gensss 2024-08-27 20:30:15 +02:00 committed by CJ van den Berg
parent d11ea9dde4
commit c3603443f9

View file

@ -228,6 +228,10 @@ fn process_project_manager(self: *Self, m: tp.message) !void {
fn delete_to_previous_path_segment(self: *Self) void { fn delete_to_previous_path_segment(self: *Self) void {
self.complete_trigger_count = 0; self.complete_trigger_count = 0;
if (self.file_path.items.len == 0) return; if (self.file_path.items.len == 0) return;
if (self.file_path.items.len == 1) {
self.file_path.clearRetainingCapacity();
return;
}
const path = if (self.file_path.items[self.file_path.items.len - 1] == std.fs.path.sep) const path = if (self.file_path.items[self.file_path.items.len - 1] == std.fs.path.sep)
self.file_path.items[0 .. self.file_path.items.len - 2] self.file_path.items[0 .. self.file_path.items.len - 2]
else else