Skip to content

Commit

Permalink
ensure that documents do not exceed std.math.maxInt(u32)
Browse files Browse the repository at this point in the history
  • Loading branch information
Techatrix committed Nov 6, 2023
1 parent 2f9cb7c commit fd2ec14
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
1 change: 0 additions & 1 deletion src/DocumentScope.zig
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ pub const Scope = struct {
},
// offsets.Loc store `usize` instead of `u32`
// zig only allows files up to `std.math.maxInt(u32)` bytes to do this kind of optimization.
// TODO ZLS should check this.
loc: SmallLoc,
parent_scope: OptionalIndex,
// child scopes have contiguous indices
Expand Down
19 changes: 14 additions & 5 deletions src/DocumentStore.zig
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ pub const Uri = []const u8;
pub const Hasher = std.crypto.auth.siphash.SipHash128(1, 3);
pub const Hash = [Hasher.mac_length]u8;

pub const max_document_size = std.math.maxInt(u32);

pub fn computeHash(bytes: []const u8) Hash {
var hasher: Hasher = Hasher.init(&[_]u8{0} ** Hasher.key_length);
hasher.update(bytes);
Expand Down Expand Up @@ -521,13 +523,20 @@ pub fn getOrLoadHandle(self: *DocumentStore, uri: Uri) ?*Handle {

if (self.getHandle(uri)) |handle| return handle;

const file_path = URI.parse(self.allocator, uri) catch return null;
const file_path = URI.parse(self.allocator, uri) catch |err| {
log.err("failed to parse URI `{s}`: {}", .{ uri, err });
return null;
};
defer self.allocator.free(file_path);

var file = std.fs.openFileAbsolute(file_path, .{}) catch return null;
defer file.close();

const file_contents = file.readToEndAllocOptions(self.allocator, std.math.maxInt(usize), null, @alignOf(u8), 0) catch return null;
if (!std.fs.path.isAbsolute(file_path)) {
log.err("file path is not absolute `{s}`", .{file_path});
return null;
}
const file_contents = std.fs.cwd().readFileAllocOptions(self.allocator, file_path, max_document_size, null, @alignOf(u8), 0) catch |err| {
log.err("failed to load document `{s}`: {}", .{ file_path, err });
return null;
};

const handle = self.createAndStoreDocument(uri, file_contents, false) catch return null;

Expand Down
18 changes: 18 additions & 0 deletions src/Server.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1139,6 +1139,15 @@ fn resolveConfiguration(server: *Server, config_arena: std.mem.Allocator, config
}

fn openDocumentHandler(server: *Server, _: std.mem.Allocator, notification: types.DidOpenTextDocumentParams) Error!void {
if (notification.textDocument.text.len > DocumentStore.max_document_size) {
log.err("open document `{s}` failed: text size ({d}) is above maximum length ({d})", .{
notification.textDocument.uri,
notification.textDocument.text.len,
DocumentStore.max_document_size,
});
return error.InternalError;
}

try server.document_store.openDocument(notification.textDocument.uri, notification.textDocument.text);

if (server.client_capabilities.supports_publish_diagnostics) {
Expand All @@ -1153,6 +1162,15 @@ fn changeDocumentHandler(server: *Server, _: std.mem.Allocator, notification: ty

const new_text = try diff.applyContentChanges(server.allocator, handle.tree.source, notification.contentChanges, server.offset_encoding);

if (new_text.len > DocumentStore.max_document_size) {
log.err("change document `{s}` failed: text size ({d}) is above maximum length ({d})", .{
notification.textDocument.uri,
new_text.len,
DocumentStore.max_document_size,
});
return error.InternalError;
}

try server.document_store.refreshDocument(handle.uri, new_text);

if (server.client_capabilities.supports_publish_diagnostics) {
Expand Down

0 comments on commit fd2ec14

Please sign in to comment.