Skip to content

Commit

Permalink
path: fix regression in posix.normalize
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
targos committed Mar 23, 2018
1 parent b41ed29 commit a0adf56
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
22 changes: 15 additions & 7 deletions lib/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,17 @@ function isPathSeparator(code) {
return code === CHAR_FORWARD_SLASH || code === CHAR_BACKWARD_SLASH;
}

function isPosixPathSeparator(code) {
return code === CHAR_FORWARD_SLASH;
}

function isWindowsDeviceRoot(code) {
return code >= CHAR_UPPERCASE_A && code <= CHAR_UPPERCASE_Z ||
code >= CHAR_LOWERCASE_A && code <= CHAR_LOWERCASE_Z;
}

// Resolves . and .. elements in a path with directory names
function normalizeString(path, allowAboveRoot, separator) {
function normalizeString(path, allowAboveRoot, separator, isPathSeparator) {
var res = '';
var lastSegmentLength = 0;
var lastSlash = -1;
Expand Down Expand Up @@ -272,7 +276,8 @@ const win32 = {
// fails)

// Normalize the tail path
resolvedTail = normalizeString(resolvedTail, !resolvedAbsolute, '\\');
resolvedTail = normalizeString(resolvedTail, !resolvedAbsolute, '\\',
isPathSeparator);

return (resolvedDevice + (resolvedAbsolute ? '\\' : '') + resolvedTail) ||
'.';
Expand Down Expand Up @@ -363,10 +368,12 @@ const win32 = {
}

var tail;
if (rootEnd < len)
tail = normalizeString(path.slice(rootEnd), !isAbsolute, '\\');
else
if (rootEnd < len) {
tail = normalizeString(path.slice(rootEnd), !isAbsolute, '\\',
isPathSeparator);
} else {
tail = '';
}
if (tail.length === 0 && !isAbsolute)
tail = '.';
if (tail.length > 0 && isPathSeparator(path.charCodeAt(len - 1)))
Expand Down Expand Up @@ -1095,7 +1102,8 @@ const posix = {
// handle relative paths to be safe (might happen when process.cwd() fails)

// Normalize the path
resolvedPath = normalizeString(resolvedPath, !resolvedAbsolute, '/');
resolvedPath = normalizeString(resolvedPath, !resolvedAbsolute, '/',
isPosixPathSeparator);

if (resolvedAbsolute) {
if (resolvedPath.length > 0)
Expand All @@ -1121,7 +1129,7 @@ const posix = {
path.charCodeAt(path.length - 1) === CHAR_FORWARD_SLASH;

// Normalize the path
path = normalizeString(path, !isAbsolute, '/');
path = normalizeString(path, !isAbsolute, '/', isPosixPathSeparator);

if (path.length === 0 && !isAbsolute)
path = '.';
Expand Down
2 changes: 2 additions & 0 deletions test/parallel/test-path-normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ assert.strictEqual(
path.win32.normalize('../.../../foobar/../../../bar/../../baz'),
'..\\..\\..\\..\\baz'
);
assert.strictEqual(path.win32.normalize('foo/bar\\baz'), 'foo\\bar\\baz');

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

0 comments on commit a0adf56

Please sign in to comment.