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] https://github.com/nodejs/node/commit/4ae320f2

PR-URL: https://github.com/nodejs/node/pull/19520
Fixes: https://github.com/nodejs/node/issues/19519
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Michaël Zasso 2018-03-21 19:20:53 +01:00
parent b41ed29b80
commit a0adf56855
2 changed files with 17 additions and 7 deletions

View File

@ -44,13 +44,17 @@ function isPathSeparator(code) {
return code === CHAR_FORWARD_SLASH || code === CHAR_BACKWARD_SLASH; return code === CHAR_FORWARD_SLASH || code === CHAR_BACKWARD_SLASH;
} }
function isPosixPathSeparator(code) {
return code === CHAR_FORWARD_SLASH;
}
function isWindowsDeviceRoot(code) { function isWindowsDeviceRoot(code) {
return code >= CHAR_UPPERCASE_A && code <= CHAR_UPPERCASE_Z || return code >= CHAR_UPPERCASE_A && code <= CHAR_UPPERCASE_Z ||
code >= CHAR_LOWERCASE_A && code <= CHAR_LOWERCASE_Z; code >= CHAR_LOWERCASE_A && code <= CHAR_LOWERCASE_Z;
} }
// Resolves . and .. elements in a path with directory names // Resolves . and .. elements in a path with directory names
function normalizeString(path, allowAboveRoot, separator) { function normalizeString(path, allowAboveRoot, separator, isPathSeparator) {
var res = ''; var res = '';
var lastSegmentLength = 0; var lastSegmentLength = 0;
var lastSlash = -1; var lastSlash = -1;
@ -272,7 +276,8 @@ const win32 = {
// fails) // fails)
// Normalize the tail path // Normalize the tail path
resolvedTail = normalizeString(resolvedTail, !resolvedAbsolute, '\\'); resolvedTail = normalizeString(resolvedTail, !resolvedAbsolute, '\\',
isPathSeparator);
return (resolvedDevice + (resolvedAbsolute ? '\\' : '') + resolvedTail) || return (resolvedDevice + (resolvedAbsolute ? '\\' : '') + resolvedTail) ||
'.'; '.';
@ -363,10 +368,12 @@ const win32 = {
} }
var tail; var tail;
if (rootEnd < len) if (rootEnd < len) {
tail = normalizeString(path.slice(rootEnd), !isAbsolute, '\\'); tail = normalizeString(path.slice(rootEnd), !isAbsolute, '\\',
else isPathSeparator);
} else {
tail = ''; tail = '';
}
if (tail.length === 0 && !isAbsolute) if (tail.length === 0 && !isAbsolute)
tail = '.'; tail = '.';
if (tail.length > 0 && isPathSeparator(path.charCodeAt(len - 1))) if (tail.length > 0 && isPathSeparator(path.charCodeAt(len - 1)))
@ -1095,7 +1102,8 @@ const posix = {
// handle relative paths to be safe (might happen when process.cwd() fails) // handle relative paths to be safe (might happen when process.cwd() fails)
// Normalize the path // Normalize the path
resolvedPath = normalizeString(resolvedPath, !resolvedAbsolute, '/'); resolvedPath = normalizeString(resolvedPath, !resolvedAbsolute, '/',
isPosixPathSeparator);
if (resolvedAbsolute) { if (resolvedAbsolute) {
if (resolvedPath.length > 0) if (resolvedPath.length > 0)
@ -1121,7 +1129,7 @@ const posix = {
path.charCodeAt(path.length - 1) === CHAR_FORWARD_SLASH; path.charCodeAt(path.length - 1) === CHAR_FORWARD_SLASH;
// Normalize the path // Normalize the path
path = normalizeString(path, !isAbsolute, '/'); path = normalizeString(path, !isAbsolute, '/', isPosixPathSeparator);
if (path.length === 0 && !isAbsolute) if (path.length === 0 && !isAbsolute)
path = '.'; path = '.';

View File

@ -39,6 +39,7 @@ assert.strictEqual(
path.win32.normalize('../.../../foobar/../../../bar/../../baz'), path.win32.normalize('../.../../foobar/../../../bar/../../baz'),
'..\\..\\..\\..\\baz' '..\\..\\..\\..\\baz'
); );
assert.strictEqual(path.win32.normalize('foo/bar\\baz'), 'foo\\bar\\baz');
assert.strictEqual(path.posix.normalize('./fixtures///b/../b/c.js'), assert.strictEqual(path.posix.normalize('./fixtures///b/../b/c.js'),
'fixtures/b/c.js'); 'fixtures/b/c.js');
@ -68,3 +69,4 @@ assert.strictEqual(
path.posix.normalize('../.../../foobar/../../../bar/../../baz'), path.posix.normalize('../.../../foobar/../../../bar/../../baz'),
'../../../../baz' '../../../../baz'
); );
assert.strictEqual(path.posix.normalize('foo/bar\\baz'), 'foo/bar\\baz');