Skip to content

Commit

Permalink
fix(node/path): crash when joining long paths (#16019)
Browse files Browse the repository at this point in the history
  • Loading branch information
DonIsaac authored Dec 27, 2024
1 parent 4bcc5b2 commit d8e644f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/bun.js/node/path.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1237,7 +1237,7 @@ pub inline fn joinWindowsJS_T(comptime T: type, globalObject: *JSC.JSGlobalObjec
pub fn joinJS_T(comptime T: type, globalObject: *JSC.JSGlobalObject, allocator: std.mem.Allocator, isWindows: bool, paths: []const []const T) JSC.JSValue {
// Adding 8 bytes when Windows for the possible UNC root.
var bufLen: usize = if (isWindows) 8 else 0;
for (paths) |path| bufLen += if (bufLen > 0 and path.len > 0) path.len + 1 else path.len;
for (paths) |path| bufLen += if (path.len > 0) path.len + 1 else path.len;
bufLen = @max(bufLen, PATH_SIZE(T));
const buf = allocator.alloc(T, bufLen) catch bun.outOfMemory();
defer allocator.free(buf);
Expand Down
10 changes: 10 additions & 0 deletions test/js/node/path/15704.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import path from "path";
import assert from "assert";

test("too-long path names do not crash when joined", () => {
const length = 4096;
const tooLengthyFolderName = Array.from({ length }).fill("b").join("");
assert.equal(path.join(tooLengthyFolderName), "b".repeat(length));
assert.equal(path.win32.join(tooLengthyFolderName), "b".repeat(length));
assert.equal(path.posix.join(tooLengthyFolderName), "b".repeat(length));
});

0 comments on commit d8e644f

Please sign in to comment.