Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

path: fix regression in posix.normalize #19520

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
path: fix regression in posix.normalize
Fixes a regression introduced in
4ae320f
The posix version of normalize should not treat backslash as a path
separator.

Fixes: #19519
targos committed Mar 21, 2018
commit 3b978c62a56ad154aabc92f3bf8864c58a1b54b8
17 changes: 12 additions & 5 deletions lib/path.js
Original file line number Diff line number Diff line change
@@ -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;
@@ -272,7 +276,8 @@ const win32 = {
// fails)

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

return (resolvedDevice + (resolvedAbsolute ? '\\' : '') + resolvedTail) ||
'.';
@@ -364,7 +369,8 @@ const win32 = {

var tail;
if (rootEnd < len)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can braces be added for the if and else here if we start having multi-line conditional bodies?

tail = normalizeString(path.slice(rootEnd), !isAbsolute, '\\');
tail = normalizeString(path.slice(rootEnd), !isAbsolute, '\\',
isPathSeparator);
else
tail = '';
if (tail.length === 0 && !isAbsolute)
@@ -1095,7 +1101,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)
@@ -1121,7 +1128,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 = '.';
2 changes: 2 additions & 0 deletions test/parallel/test-path-normalize.js
Original file line number Diff line number Diff line change
@@ -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');
@@ -68,3 +69,4 @@ assert.strictEqual(
path.posix.normalize('../.../../foobar/../../../bar/../../baz'),
'../../../../baz'
);
assert.strictEqual(path.posix.normalize('foo/bar\\baz'), 'foo/bar\\baz');