Compare commits

...

1 Commits

Author SHA1 Message Date
Claude Bot
ed48917d19 fix(windows): handle edge cases in dirname() for Windows paths
Fixes a crash when installing pm2-windows-service globally on Windows.
The crash occurred because the Windows implementation of dirname() in
resolve_path.zig didn't handle edge cases that the POSIX version does:

1. When separator == 0 (paths starting with separator like "\foo"):
   Previously returned empty string, now correctly returns root "\"

2. When path ends with separator (like "C:\foo\"):
   Previously included the trailing separator, now recursively strips it

This aligns the Windows dirname() behavior with POSIX, preventing
downstream code from receiving unexpected empty strings or malformed
paths that could trigger unreachable code paths.
2025-10-12 19:14:40 +00:00
2 changed files with 31 additions and 0 deletions

View File

@@ -485,6 +485,10 @@ pub fn dirname(str: []const u8, comptime platform: Platform) []const u8 {
},
.windows => {
const separator = lastIndexOfSeparatorWindows(str) orelse return std.fs.path.diskDesignatorWindows(str);
// Handle root paths like "\foo" - return the root separator
if (separator == 0) return str[0..1];
// Handle trailing separators like "C:\foo\" - recursively strip them
if (separator == str.len - 1) return dirname(str[0 .. str.len - 1], platform);
return str[0..separator];
},
else => @compileError("not implemented"),

View File

@@ -0,0 +1,27 @@
import { expect, test } from "bun:test";
import { bunEnv, bunExe, tempDir } from "harness";
// https://github.com/oven-sh/bun/issues/[TODO: Add issue number]
// Test for crash in bun install -g pm2-windows-service
// The crash occurred in resolve_path.zig dirname() function when handling
// Windows paths with edge cases like paths starting with separator or trailing separators
test("bun install -g pm2-windows-service should not crash", async () => {
using dir = tempDir("windows-dirname-crash", {});
await using proc = Bun.spawn({
cmd: [bunExe(), "add", "-g", "pm2-windows-service"],
env: bunEnv,
cwd: String(dir),
stderr: "pipe",
stdout: "pipe",
});
const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]);
// Should not crash with "panic: reached unreachable code"
expect(stderr).not.toContain("panic");
expect(stderr).not.toContain("unreachable");
expect(exitCode).toBe(0);
expect(stdout).toContain("pm2-windows-service");
});