aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Ciro Spaciari <ciro.spaciari@gmail.com> 2023-09-13 19:49:43 -0700
committerGravatar GitHub <noreply@github.com> 2023-09-13 19:49:43 -0700
commitd37602f316daada630bbf9664c18878d11e30df4 (patch)
tree8fe2336d8e3610d7b3538a10219816c72213e445
parentcb52556bd1669d0b23e705d82284b673f8f1cd4c (diff)
downloadbun-d37602f316daada630bbf9664c18878d11e30df4.tar.gz
bun-d37602f316daada630bbf9664c18878d11e30df4.tar.zst
bun-d37602f316daada630bbf9664c18878d11e30df4.zip
fix(BunFile.slice) fix slice when length is greater than the size (#5186)
* check the limits for file, when slicing * check eof * undo test
-rw-r--r--src/bun.js/webcore/blob.zig8
-rw-r--r--test/js/bun/io/bun-write.test.js10
2 files changed, 15 insertions, 3 deletions
diff --git a/src/bun.js/webcore/blob.zig b/src/bun.js/webcore/blob.zig
index 54c37e679..7ce254ca8 100644
--- a/src/bun.js/webcore/blob.zig
+++ b/src/bun.js/webcore/blob.zig
@@ -1680,6 +1680,7 @@ pub const Blob = struct {
read_completion: HTTPClient.NetworkThread.Completion = undefined,
read_len: SizeType = 0,
read_off: SizeType = 0,
+ read_eof: bool = false,
size: SizeType = 0,
buffer: []u8 = undefined,
task: HTTPClient.NetworkThread.Task = undefined,
@@ -1797,8 +1798,7 @@ pub const Blob = struct {
pub fn onRead(this: *ReadFile, completion: *HTTPClient.NetworkThread.Completion, result: AsyncIO.ReadError!usize) void {
defer this.doReadLoop();
-
- this.read_len = @as(SizeType, @truncate(result catch |err| {
+ const read_len = @as(SizeType, @truncate(result catch |err| {
if (@hasField(HTTPClient.NetworkThread.Completion, "result")) {
this.errno = AsyncIO.asError(-completion.result);
this.system_error = (bun.sys.Error{
@@ -1821,6 +1821,8 @@ pub const Blob = struct {
this.read_len = 0;
return;
}));
+ this.read_eof = read_len == 0;
+ this.read_len = read_len;
}
fn runAsync(this: *ReadFile, task: *ReadFileTask) void {
@@ -1930,7 +1932,7 @@ pub const Blob = struct {
this.read_off += this.read_len;
var remain = this.buffer[@min(this.read_off, @as(Blob.SizeType, @truncate(this.buffer.len)))..];
- if (remain.len > 0 and this.errno == null) {
+ if (remain.len > 0 and this.errno == null and !this.read_eof) {
this.doRead();
return;
}
diff --git a/test/js/bun/io/bun-write.test.js b/test/js/bun/io/bun-write.test.js
index f435d2ceb..a05fa283a 100644
--- a/test/js/bun/io/bun-write.test.js
+++ b/test/js/bun/io/bun-write.test.js
@@ -300,6 +300,16 @@ it("offset should work #4963", async () => {
expect(contents).toBe("ntents");
});
+it("length should be limited by file size #5080", async () => {
+ const filename = tmpdir() + "/bun.test.offset2.txt";
+ await Bun.write(filename, "contents");
+ const file = Bun.file(filename);
+ const slice = file.slice(2, 1024);
+ const contents = await slice.text();
+ expect(contents).toBe("ntents");
+ expect(contents.length).toBeLessThanOrEqual(file.size);
+});
+
it("#2674", async () => {
const file = path.join(import.meta.dir, "big-stdout.js");