From 0b1318d308e34b029a09c377bad05baabf7c0493 Mon Sep 17 00:00:00 2001 From: Ciro Spaciari Date: Sun, 24 Nov 2024 12:06:49 -0800 Subject: [PATCH] NOMEM exceptions instead of panic on FileReader --- src/bun.js/webcore/streams.zig | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/bun.js/webcore/streams.zig b/src/bun.js/webcore/streams.zig index 2ab7e4ddd6161d..62c04ce432e7b0 100644 --- a/src/bun.js/webcore/streams.zig +++ b/src/bun.js/webcore/streams.zig @@ -3877,12 +3877,20 @@ pub const FileReader = struct { } else if (in_progress.len > 0 and !hasMore) { this.read_inside_on_pull = .{ .temporary = buf }; } else if (hasMore and !bun.isSliceInBuffer(buf, this.buffered.allocatedSlice())) { - this.buffered.appendSlice(bun.default_allocator, buf) catch bun.outOfMemory(); + this.buffered.appendSlice(bun.default_allocator, buf) catch { + this.onReaderError(bun.sys.Error.fromCode(.NOMEM, .read)); + this.reader.close(); + return false; + }; this.read_inside_on_pull = .{ .use_buffered = buf.len }; } }, .use_buffered => |original| { - this.buffered.appendSlice(bun.default_allocator, buf) catch bun.outOfMemory(); + this.buffered.appendSlice(bun.default_allocator, buf) catch { + this.onReaderError(bun.sys.Error.fromCode(.NOMEM, .read)); + this.reader.close(); + return false; + }; this.read_inside_on_pull = .{ .use_buffered = buf.len + original }; }, .none => unreachable, @@ -3989,7 +3997,11 @@ pub const FileReader = struct { this.pending.run(); return !was_done; } else if (!bun.isSliceInBuffer(buf, this.buffered.allocatedSlice())) { - this.buffered.appendSlice(bun.default_allocator, buf) catch bun.outOfMemory(); + this.buffered.appendSlice(bun.default_allocator, buf) catch { + this.onReaderError(bun.sys.Error.fromCode(.NOMEM, .read)); + this.reader.close(); + return false; + }; if (bun.isSliceInBuffer(buf, this.reader.buffer().allocatedSlice())) { this.reader.buffer().clearRetainingCapacity(); }