From d37602f316daada630bbf9664c18878d11e30df4 Mon Sep 17 00:00:00 2001 From: Ciro Spaciari Date: Wed, 13 Sep 2023 19:49:43 -0700 Subject: fix(BunFile.slice) fix slice when length is greater than the size (#5186) * check the limits for file, when slicing * check eof * undo test --- test/js/bun/io/bun-write.test.js | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'test/js/bun/io/bun-write.test.js') 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"); -- cgit v1.2.3 From d08e112d416ddee7af8d499524e40e7f193ae55a Mon Sep 17 00:00:00 2001 From: Ai Hoshino Date: Sat, 14 Oct 2023 08:51:36 +0800 Subject: fix(error): correct the `path` field in syscall error message. (#6370) * fix(error): correct the `path` field in syscall error message. Close: #6336 * fix pathlike union case --- src/bun.js/webcore/blob.zig | 29 +++++++++++++++++++++++------ test/js/bun/io/bun-write.test.js | 25 +++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 6 deletions(-) (limited to 'test/js/bun/io/bun-write.test.js') diff --git a/src/bun.js/webcore/blob.zig b/src/bun.js/webcore/blob.zig index 1e0720ea6..f9699cffc 100644 --- a/src/bun.js/webcore/blob.zig +++ b/src/bun.js/webcore/blob.zig @@ -1040,7 +1040,10 @@ pub const Blob = struct { break :brk result; }, .err => |err| { - return JSC.JSPromise.rejectedPromiseValue(globalThis, err.toJSC(globalThis)); + return JSC.JSPromise.rejectedPromiseValue( + globalThis, + err.withPath(pathlike.path.slice()).toJSC(globalThis), + ); }, } unreachable; @@ -1080,8 +1083,13 @@ pub const Blob = struct { needs_async.* = true; return .zero; } - - return JSC.JSPromise.rejectedPromiseValue(globalThis, err.toJSC(globalThis)); + if (comptime !needs_open) { + return JSC.JSPromise.rejectedPromiseValue(globalThis, err.toJSC(globalThis)); + } + return JSC.JSPromise.rejectedPromiseValue( + globalThis, + err.withPath(pathlike.path.slice()).toJSC(globalThis), + ); }, } } @@ -1110,7 +1118,10 @@ pub const Blob = struct { break :brk result; }, .err => |err| { - return JSC.JSPromise.rejectedPromiseValue(globalThis, err.toJSC(globalThis)); + return JSC.JSPromise.rejectedPromiseValue( + globalThis, + err.withPath(pathlike.path.slice()).toJSC(globalThis), + ); }, } unreachable; @@ -1145,7 +1156,13 @@ pub const Blob = struct { needs_async.* = true; return .zero; } - return JSC.JSPromise.rejectedPromiseValue(globalThis, err.toJSC(globalThis)); + if (comptime !needs_open) { + return JSC.JSPromise.rejectedPromiseValue(globalThis, err.toJSC(globalThis)); + } + return JSC.JSPromise.rejectedPromiseValue( + globalThis, + err.withPath(pathlike.path.slice()).toJSC(globalThis), + ); }, } } @@ -2289,7 +2306,7 @@ pub const Blob = struct { this.source_fd = 0; } - this.system_error = errno.toSystemError(); + this.system_error = errno.withPath(this.destination_file_store.pathlike.path.slice()).toSystemError(); return AsyncIO.asError(errno.errno); }, }; diff --git a/test/js/bun/io/bun-write.test.js b/test/js/bun/io/bun-write.test.js index a05fa283a..fbcc99b66 100644 --- a/test/js/bun/io/bun-write.test.js +++ b/test/js/bun/io/bun-write.test.js @@ -80,6 +80,31 @@ it("Bun.file not found returns ENOENT", async () => { await gcTick(); }); +it("Bun.write file not found returns ENOENT, issue#6336", async () => { + const dst = Bun.file(path.join(tmpdir(), "does/not/exist.txt")); + try { + await gcTick(); + await Bun.write(dst, ""); + await gcTick(); + } catch (exception) { + expect(exception.code).toBe("ENOENT"); + expect(exception.path).toBe(dst.name); + } + + const src = Bun.file(path.join(tmpdir(), `test-bun-write-${Date.now()}.txt`)); + await Bun.write(src, ""); + try { + await gcTick(); + await Bun.write(dst, src); + await gcTick(); + } catch (exception) { + expect(exception.code).toBe("ENOENT"); + expect(exception.path).toBe(dst.name); + } finally { + fs.unlinkSync(src.name); + } +}); + it("Bun.write('out.txt', 'string')", async () => { for (let erase of [true, false]) { if (erase) { -- cgit v1.2.3