diff options
author | 2023-01-18 01:16:13 -0800 | |
---|---|---|
committer | 2023-01-18 01:16:13 -0800 | |
commit | 2db04ef9d9ddf6531b0a77c3b0867bc80388a039 (patch) | |
tree | 6430e9e6f8043897ddab77f07515ddb4212d94c7 | |
parent | 4cb6a34432899df08ac4174334ab7d6a374a48e2 (diff) | |
download | bun-2db04ef9d9ddf6531b0a77c3b0867bc80388a039.tar.gz bun-2db04ef9d9ddf6531b0a77c3b0867bc80388a039.tar.zst bun-2db04ef9d9ddf6531b0a77c3b0867bc80388a039.zip |
Fix crash with invalid arguments in readdir()bun-v0.5.0
-rw-r--r-- | src/bun.js/node/node_fs_binding.zig | 7 | ||||
-rw-r--r-- | test/bun.js/fs.test.js | 30 |
2 files changed, 35 insertions, 2 deletions
diff --git a/src/bun.js/node/node_fs_binding.zig b/src/bun.js/node/node_fs_binding.zig index 07ecf4619..219f85cd8 100644 --- a/src/bun.js/node/node_fs_binding.zig +++ b/src/bun.js/node/node_fs_binding.zig @@ -38,11 +38,16 @@ fn callSync(comptime FunctionEnum: NodeFSFunctionEnum) NodeFSFunction { var exceptionref: JSC.C.JSValueRef = null; var arguments = callframe.arguments(8); + var slice = ArgumentsSlice.init(globalObject.bunVM(), arguments.ptr[0..arguments.len]); defer slice.deinit(); const args = if (comptime Arguments != void) - (Arguments.fromJS(globalObject, &slice, &exceptionref) orelse return .zero) + (Arguments.fromJS(globalObject, &slice, &exceptionref) orelse { + std.debug.assert(exceptionref != null); + globalObject.throwValue(JSC.JSValue.c(exceptionref)); + return .zero; + }) else Arguments{}; diff --git a/test/bun.js/fs.test.js b/test/bun.js/fs.test.js index ac2357143..058a40828 100644 --- a/test/bun.js/fs.test.js +++ b/test/bun.js/fs.test.js @@ -548,7 +548,9 @@ describe("fs/promises", () => { const { readFile, stat, writeFile } = promises; it("should not segfault on exception", async () => { - stat("foo/bar"); + try { + await stat("foo/bar"); + } catch (e) {} }); it("readFile", async () => { @@ -561,4 +563,30 @@ describe("fs/promises", () => { await writeFile(path, "File written successfully"); expect(readFileSync(path, "utf8")).toBe("File written successfully"); }); + + it("readdir()", async () => { + const files = await promises.readdir(import.meta.dir); + expect(files.length).toBeGreaterThan(0); + }); + + it("readdir() no args doesnt segfault", async () => { + const fizz = [ + [], + [Symbol("ok")], + [Symbol("ok"), Symbol("ok")], + [Symbol("ok"), Symbol("ok"), Symbol("ok")], + [Infinity, -NaN, -Infinity], + "\0\0\0\0", + "\r\n", + ]; + for (const args of fizz) { + try { + // check it doens't segfault when called with invalid arguments + await promises.readdir(...args); + } catch (e) { + // check that producing the error doesn't cause any crashes + Bun.inspect(e); + } + } + }); }); |