Skip to content

Commit 37071b8

Browse files
starkwangMylesBorins
authored andcommitted
path: fix path.normalize for relative paths
After slicing, the `lastSegmentLength` should be calculated again, instead of assigning value `j`. PR-URL: #17974 Fixes: #17928 Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Gibson Fahnestock <[email protected]>
1 parent 17c88c4 commit 37071b8

File tree

2 files changed

+34
-20
lines changed

2 files changed

+34
-20
lines changed

lib/path.js

+10-20
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,14 @@ function normalizeStringWin32(path, allowAboveRoot) {
3030
res.charCodeAt(res.length - 1) !== 46/*.*/ ||
3131
res.charCodeAt(res.length - 2) !== 46/*.*/) {
3232
if (res.length > 2) {
33-
const start = res.length - 1;
34-
var j = start;
35-
for (; j >= 0; --j) {
36-
if (res.charCodeAt(j) === 92/*\*/)
37-
break;
38-
}
39-
if (j !== start) {
40-
if (j === -1) {
33+
const lastSlashIndex = res.lastIndexOf('\\');
34+
if (lastSlashIndex !== res.length - 1) {
35+
if (lastSlashIndex === -1) {
4136
res = '';
4237
lastSegmentLength = 0;
4338
} else {
44-
res = res.slice(0, j);
45-
lastSegmentLength = j;
39+
res = res.slice(0, lastSlashIndex);
40+
lastSegmentLength = res.length - 1 - res.lastIndexOf('\\');
4641
}
4742
lastSlash = i;
4843
dots = 0;
@@ -103,19 +98,14 @@ function normalizeStringPosix(path, allowAboveRoot) {
10398
res.charCodeAt(res.length - 1) !== 46/*.*/ ||
10499
res.charCodeAt(res.length - 2) !== 46/*.*/) {
105100
if (res.length > 2) {
106-
const start = res.length - 1;
107-
var j = start;
108-
for (; j >= 0; --j) {
109-
if (res.charCodeAt(j) === 47/*/*/)
110-
break;
111-
}
112-
if (j !== start) {
113-
if (j === -1) {
101+
const lastSlashIndex = res.lastIndexOf('/');
102+
if (lastSlashIndex !== res.length - 1) {
103+
if (lastSlashIndex === -1) {
114104
res = '';
115105
lastSegmentLength = 0;
116106
} else {
117-
res = res.slice(0, j);
118-
lastSegmentLength = j;
107+
res = res.slice(0, lastSlashIndex);
108+
lastSegmentLength = res.length - 1 - res.lastIndexOf('/');
119109
}
120110
lastSlash = i;
121111
dots = 0;

test/parallel/test-path-normalize.js

+24
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,18 @@ assert.strictEqual(path.win32.normalize('..\\foo..\\..\\..\\bar'),
2727
'..\\..\\bar');
2828
assert.strictEqual(path.win32.normalize('..\\...\\..\\.\\...\\..\\..\\bar'),
2929
'..\\..\\bar');
30+
assert.strictEqual(path.win32.normalize('../../../foo/../../../bar'),
31+
'..\\..\\..\\..\\..\\bar');
32+
assert.strictEqual(path.win32.normalize('../../../foo/../../../bar/../../'),
33+
'..\\..\\..\\..\\..\\..\\');
34+
assert.strictEqual(
35+
path.win32.normalize('../foobar/barfoo/foo/../../../bar/../../'),
36+
'..\\..\\'
37+
);
38+
assert.strictEqual(
39+
path.win32.normalize('../.../../foobar/../../../bar/../../baz'),
40+
'..\\..\\..\\..\\baz'
41+
);
3042

3143
assert.strictEqual(path.posix.normalize('./fixtures///b/../b/c.js'),
3244
'fixtures/b/c.js');
@@ -44,3 +56,15 @@ assert.strictEqual(path.posix.normalize('bar/foo..'), 'bar/foo..');
4456
assert.strictEqual(path.posix.normalize('../foo../../../bar'), '../../bar');
4557
assert.strictEqual(path.posix.normalize('../.../.././.../../../bar'),
4658
'../../bar');
59+
assert.strictEqual(path.posix.normalize('../../../foo/../../../bar'),
60+
'../../../../../bar');
61+
assert.strictEqual(path.posix.normalize('../../../foo/../../../bar/../../'),
62+
'../../../../../../');
63+
assert.strictEqual(
64+
path.posix.normalize('../foobar/barfoo/foo/../../../bar/../../'),
65+
'../../'
66+
);
67+
assert.strictEqual(
68+
path.posix.normalize('../.../../foobar/../../../bar/../../baz'),
69+
'../../../../baz'
70+
);

0 commit comments

Comments
 (0)