Skip to content

Commit a870228

Browse files
committed
self-hosted: use std.event.fs.readFile
1 parent cc45527 commit a870228

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

src-self-hosted/compilation.zig

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ const Package = @import("package.zig").Package;
3030
const link = @import("link.zig").link;
3131
const LibCInstallation = @import("libc_installation.zig").LibCInstallation;
3232
const CInt = @import("c_int.zig").CInt;
33+
const fs = event.fs;
34+
35+
const max_src_size = 2 * 1024 * 1024 * 1024; // 2 GiB
3336

3437
/// Data that is local to the event loop.
3538
pub const EventLoopLocal = struct {
@@ -757,8 +760,11 @@ pub const Compilation = struct {
757760
const root_scope = blk: {
758761
errdefer self.gpa().free(root_src_real_path);
759762

760-
// TODO async/await readFileAlloc()
761-
const source_code = io.readFileAlloc(self.gpa(), root_src_real_path) catch |err| {
763+
const source_code = (await (async fs.readFile(
764+
self.loop,
765+
root_src_real_path,
766+
max_src_size,
767+
) catch unreachable)) catch |err| {
762768
try printError("unable to open '{}': {}", root_src_real_path, err);
763769
return err;
764770
};

std/event/fs.zig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@ pub async fn writeFileMode(loop: *event.Loop, path: []const u8, contents: []cons
273273

274274
/// The promise resumes when the last data has been confirmed written, but before the file handle
275275
/// is closed.
276+
/// Caller owns returned memory.
276277
pub async fn readFile(loop: *event.Loop, file_path: []const u8, max_size: usize) ![]u8 {
277278
var close_op = try CloseOperation.create(loop);
278279
defer close_op.deinit();
@@ -292,6 +293,9 @@ pub async fn readFile(loop: *event.Loop, file_path: []const u8, max_size: usize)
292293
const buf_array = [][]u8{buf};
293294
const amt = try await (async preadv(loop, fd, list.len, buf_array) catch unreachable);
294295
list.len += amt;
296+
if (list.len > max_size) {
297+
return error.FileTooBig;
298+
}
295299
if (amt < buf.len) {
296300
return list.toOwnedSlice();
297301
}

0 commit comments

Comments
 (0)