Skip to content

Commit

Permalink
NOMEM exceptions instead of panic on FileReader
Browse files Browse the repository at this point in the history
  • Loading branch information
cirospaciari committed Nov 26, 2024
1 parent 0d6d4fa commit 0b1318d
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/bun.js/webcore/streams.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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();
}
Expand Down

0 comments on commit 0b1318d

Please sign in to comment.