diff options
author | 2023-03-01 18:07:21 -0300 | |
---|---|---|
committer | 2023-03-01 13:07:21 -0800 | |
commit | 56ca48ece88c0de854ec20c5e71a639fee6ccb0f (patch) | |
tree | 6161cccbd4bc5ce3f72e2ccfea9e25a8513a26cf | |
parent | 530cf4caf8abe4960d833efc6cce71188dea40b2 (diff) | |
download | bun-56ca48ece88c0de854ec20c5e71a639fee6ccb0f.tar.gz bun-56ca48ece88c0de854ec20c5e71a639fee6ccb0f.tar.zst bun-56ca48ece88c0de854ec20c5e71a639fee6ccb0f.zip |
fix Bun.file.arrayBuffer() segmentation fault on empty file #2248 (#2249)
* fix Bun.file.arrayBuffer() segmentation fault on empty file #2248
* cleanner this.iotask check
-rw-r--r-- | src/bun.js/webcore/blob.zig | 22 | ||||
-rw-r--r-- | test/bun.js/bun-write.test.js | 8 | ||||
-rw-r--r-- | test/bun.js/emptyFile | 0 |
3 files changed, 23 insertions, 7 deletions
diff --git a/src/bun.js/webcore/blob.zig b/src/bun.js/webcore/blob.zig index c0a865d5d..ae60ef01f 100644 --- a/src/bun.js/webcore/blob.zig +++ b/src/bun.js/webcore/blob.zig @@ -73,6 +73,8 @@ const PathOrBlob = union(enum) { }; pub const Blob = struct { + const bloblog = Output.scoped(.Blob, false); + pub usingnamespace JSC.Codegen.JSBlob; size: SizeType = 0, @@ -1431,9 +1433,10 @@ pub const Blob = struct { this.doClose(); } - var io_task = this.io_task.?; - this.io_task = null; - io_task.onFinish(); + if (this.io_task) |io_task| { + io_task.onFinish(); + this.io_task = null; + } } fn resolveSize(this: *ReadFile, fd: bun.FileDescriptor) void { @@ -1655,9 +1658,10 @@ pub const Blob = struct { this.doClose(); } - var io_task = this.io_task.?; - this.io_task = null; - io_task.onFinish(); + if (this.io_task) |io_task| { + io_task.onFinish(); + this.io_task = null; + } } fn runWithFD(this: *WriteFile, fd: bun.FileDescriptor) void { @@ -2825,6 +2829,8 @@ pub const Blob = struct { } pub fn doReadFile(this: *Blob, comptime Function: anytype, global: *JSGlobalObject) JSValue { + bloblog("doReadFile", .{}); + const Handler = NewReadFileHandler(Function); var promise = JSPromise.create(global); @@ -2849,6 +2855,7 @@ pub const Blob = struct { ) catch unreachable; var read_file_task = Store.ReadFile.ReadFileTask.createOnJSThread(bun.default_allocator, global, file_read) catch unreachable; read_file_task.schedule(); + bloblog("doReadFile: read_file_task scheduled", .{}); return promise_value; } @@ -3009,12 +3016,13 @@ pub const Blob = struct { } pub fn toArrayBuffer(this: *Blob, global: *JSGlobalObject, comptime lifetime: Lifetime) JSValue { + bloblog("toArrayBuffer", .{}); if (this.needsToReadFile()) { return this.doReadFile(toArrayBufferWithBytes, global); } var view_ = this.sharedView(); - + bloblog("sharedView {d}", .{view_.len}); if (view_.len == 0) return JSC.ArrayBuffer.create(global, "", .ArrayBuffer); diff --git a/test/bun.js/bun-write.test.js b/test/bun.js/bun-write.test.js index 7e1d43059..68e221d51 100644 --- a/test/bun.js/bun-write.test.js +++ b/test/bun.js/bun-write.test.js @@ -139,6 +139,14 @@ it("Bun.file", async () => { await gcTick(); }); +it("Bun.file empty file", async () => { + const file = path.join(import.meta.dir, "emptyFile"); + await gcTick(); + const buffer = await Bun.file(file).arrayBuffer() + expect(buffer.byteLength).toBe(0); + await gcTick(); +}); + it("Bun.file as a Blob", async () => { const filePath = path.join(import.meta.path, "../fetch.js.txt"); const fixture = fs.readFileSync(filePath, "utf8"); diff --git a/test/bun.js/emptyFile b/test/bun.js/emptyFile new file mode 100644 index 000000000..e69de29bb --- /dev/null +++ b/test/bun.js/emptyFile |