Skip to content

Commit 40916a2

Browse files
committed
path: fix regression in posix.normalize
Fixes a regression introduced in [1]. The posix version of normalize should not treat backslash as a path separator. [1] 4ae320f2 PR-URL: #19520 Fixes: #19519 Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Tobias Nießen <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 49c0efd commit 40916a2

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

lib/path.js

+15-7
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,17 @@ function isPathSeparator(code) {
4444
return code === CHAR_FORWARD_SLASH || code === CHAR_BACKWARD_SLASH;
4545
}
4646

47+
function isPosixPathSeparator(code) {
48+
return code === CHAR_FORWARD_SLASH;
49+
}
50+
4751
function isWindowsDeviceRoot(code) {
4852
return code >= CHAR_UPPERCASE_A && code <= CHAR_UPPERCASE_Z ||
4953
code >= CHAR_LOWERCASE_A && code <= CHAR_LOWERCASE_Z;
5054
}
5155

5256
// Resolves . and .. elements in a path with directory names
53-
function normalizeString(path, allowAboveRoot, separator) {
57+
function normalizeString(path, allowAboveRoot, separator, isPathSeparator) {
5458
var res = '';
5559
var lastSegmentLength = 0;
5660
var lastSlash = -1;
@@ -272,7 +276,8 @@ const win32 = {
272276
// fails)
273277

274278
// Normalize the tail path
275-
resolvedTail = normalizeString(resolvedTail, !resolvedAbsolute, '\\');
279+
resolvedTail = normalizeString(resolvedTail, !resolvedAbsolute, '\\',
280+
isPathSeparator);
276281

277282
return (resolvedDevice + (resolvedAbsolute ? '\\' : '') + resolvedTail) ||
278283
'.';
@@ -363,10 +368,12 @@ const win32 = {
363368
}
364369

365370
var tail;
366-
if (rootEnd < len)
367-
tail = normalizeString(path.slice(rootEnd), !isAbsolute, '\\');
368-
else
371+
if (rootEnd < len) {
372+
tail = normalizeString(path.slice(rootEnd), !isAbsolute, '\\',
373+
isPathSeparator);
374+
} else {
369375
tail = '';
376+
}
370377
if (tail.length === 0 && !isAbsolute)
371378
tail = '.';
372379
if (tail.length > 0 && isPathSeparator(path.charCodeAt(len - 1)))
@@ -1096,7 +1103,8 @@ const posix = {
10961103
// handle relative paths to be safe (might happen when process.cwd() fails)
10971104

10981105
// Normalize the path
1099-
resolvedPath = normalizeString(resolvedPath, !resolvedAbsolute, '/');
1106+
resolvedPath = normalizeString(resolvedPath, !resolvedAbsolute, '/',
1107+
isPosixPathSeparator);
11001108

11011109
if (resolvedAbsolute) {
11021110
if (resolvedPath.length > 0)
@@ -1122,7 +1130,7 @@ const posix = {
11221130
path.charCodeAt(path.length - 1) === CHAR_FORWARD_SLASH;
11231131

11241132
// Normalize the path
1125-
path = normalizeString(path, !isAbsolute, '/');
1133+
path = normalizeString(path, !isAbsolute, '/', isPosixPathSeparator);
11261134

11271135
if (path.length === 0 && !isAbsolute)
11281136
path = '.';

test/parallel/test-path-normalize.js

+2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ assert.strictEqual(
3939
path.win32.normalize('../.../../foobar/../../../bar/../../baz'),
4040
'..\\..\\..\\..\\baz'
4141
);
42+
assert.strictEqual(path.win32.normalize('foo/bar\\baz'), 'foo\\bar\\baz');
4243

4344
assert.strictEqual(path.posix.normalize('./fixtures///b/../b/c.js'),
4445
'fixtures/b/c.js');
@@ -68,3 +69,4 @@ assert.strictEqual(
6869
path.posix.normalize('../.../../foobar/../../../bar/../../baz'),
6970
'../../../../baz'
7071
);
72+
assert.strictEqual(path.posix.normalize('foo/bar\\baz'), 'foo/bar\\baz');

0 commit comments

Comments
 (0)