Skip to content

Commit 038ac01

Browse files
huseyinacacak-janeaaduh95
authored andcommitted
path,win: fix bug in resolve and normalize
Fixes: #54025 PR-URL: #55623 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent bffbaa1 commit 038ac01

File tree

6 files changed

+45
-19
lines changed

6 files changed

+45
-19
lines changed

lib/path.js

+26-15
Original file line numberDiff line numberDiff line change
@@ -268,10 +268,16 @@ const win32 = {
268268
j++;
269269
}
270270
if (j === len || j !== last) {
271-
// We matched a UNC root
272-
device =
273-
`\\\\${firstPart}\\${StringPrototypeSlice(path, last, j)}`;
274-
rootEnd = j;
271+
if (firstPart !== '.' && firstPart !== '?') {
272+
// We matched a UNC root
273+
device =
274+
`\\\\${firstPart}\\${StringPrototypeSlice(path, last, j)}`;
275+
rootEnd = j;
276+
} else {
277+
// We matched a device root (e.g. \\\\.\\PHYSICALDRIVE0)
278+
device = `\\\\${firstPart}`;
279+
rootEnd = 4;
280+
}
275281
}
276282
}
277283
}
@@ -381,17 +387,22 @@ const win32 = {
381387
!isPathSeparator(StringPrototypeCharCodeAt(path, j))) {
382388
j++;
383389
}
384-
if (j === len) {
385-
// We matched a UNC root only
386-
// Return the normalized version of the UNC root since there
387-
// is nothing left to process
388-
return `\\\\${firstPart}\\${StringPrototypeSlice(path, last)}\\`;
389-
}
390-
if (j !== last) {
391-
// We matched a UNC root with leftovers
392-
device =
393-
`\\\\${firstPart}\\${StringPrototypeSlice(path, last, j)}`;
394-
rootEnd = j;
390+
if (j === len || j !== last) {
391+
if (firstPart === '.' || firstPart === '?') {
392+
// We matched a device root (e.g. \\\\.\\PHYSICALDRIVE0)
393+
device = `\\\\${firstPart}`;
394+
rootEnd = 4;
395+
} else if (j === len) {
396+
// We matched a UNC root only
397+
// Return the normalized version of the UNC root since there
398+
// is nothing left to process
399+
return `\\\\${firstPart}\\${StringPrototypeSlice(path, last)}\\`;
400+
} else {
401+
// We matched a UNC root with leftovers
402+
device =
403+
`\\\\${firstPart}\\${StringPrototypeSlice(path, last, j)}`;
404+
rootEnd = j;
405+
}
395406
}
396407
}
397408
}

src/path.cc

+10-3
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,16 @@ std::string PathResolve(Environment* env,
170170
j++;
171171
}
172172
if (j == len || j != last) {
173-
// We matched a UNC root
174-
device = "\\\\" + firstPart + "\\" + path.substr(last, j - last);
175-
rootEnd = j;
173+
if (firstPart != "." && firstPart != "?") {
174+
// We matched a UNC root
175+
device =
176+
"\\\\" + firstPart + "\\" + path.substr(last, j - last);
177+
rootEnd = j;
178+
} else {
179+
// We matched a device root (e.g. \\\\.\\PHYSICALDRIVE0)
180+
device = "\\\\" + firstPart;
181+
rootEnd = 4;
182+
}
176183
}
177184
}
178185
}

test/cctest/test_path.cc

+4
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ TEST_F(PathTest, PathResolve) {
3939
EXPECT_EQ(
4040
PathResolve(*env, {"C:\\foo\\tmp.3\\", "..\\tmp.3\\cycles\\root.js"}),
4141
"C:\\foo\\tmp.3\\cycles\\root.js");
42+
EXPECT_EQ(PathResolve(*env, {"\\\\.\\PHYSICALDRIVE0"}),
43+
"\\\\.\\PHYSICALDRIVE0");
44+
EXPECT_EQ(PathResolve(*env, {"\\\\?\\PHYSICALDRIVE0"}),
45+
"\\\\?\\PHYSICALDRIVE0");
4246
#else
4347
EXPECT_EQ(PathResolve(*env, {"/var/lib", "../", "file/"}), "/var/file");
4448
EXPECT_EQ(PathResolve(*env, {"/var/lib", "/../", "file/"}), "/file");

test/parallel/test-path-makelong.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ assert.strictEqual(path.win32.toNamespacedPath('\\\\foo\\bar'),
7979
'\\\\?\\UNC\\foo\\bar\\');
8080
assert.strictEqual(path.win32.toNamespacedPath('//foo//bar'),
8181
'\\\\?\\UNC\\foo\\bar\\');
82-
assert.strictEqual(path.win32.toNamespacedPath('\\\\?\\foo'), '\\\\?\\foo\\');
82+
assert.strictEqual(path.win32.toNamespacedPath('\\\\?\\foo'), '\\\\?\\foo');
8383
assert.strictEqual(path.win32.toNamespacedPath('\\\\?\\c:\\Windows/System'), '\\\\?\\c:\\Windows\\System');
8484
assert.strictEqual(path.win32.toNamespacedPath(null), null);
8585
assert.strictEqual(path.win32.toNamespacedPath(true), true);

test/parallel/test-path-normalize.js

+2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ assert.strictEqual(
4040
'..\\..\\..\\..\\baz'
4141
);
4242
assert.strictEqual(path.win32.normalize('foo/bar\\baz'), 'foo\\bar\\baz');
43+
assert.strictEqual(path.win32.normalize('\\\\.\\foo'), '\\\\.\\foo');
44+
assert.strictEqual(path.win32.normalize('\\\\.\\foo\\'), '\\\\.\\foo\\');
4345

4446
assert.strictEqual(path.posix.normalize('./fixtures///b/../b/c.js'),
4547
'fixtures/b/c.js');

test/parallel/test-path-resolve.js

+2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ const resolveTests = [
3333
[['c:/', '///some//dir'], 'c:\\some\\dir'],
3434
[['C:\\foo\\tmp.3\\', '..\\tmp.3\\cycles\\root.js'],
3535
'C:\\foo\\tmp.3\\cycles\\root.js'],
36+
[['\\\\.\\PHYSICALDRIVE0'], '\\\\.\\PHYSICALDRIVE0'],
37+
[['\\\\?\\PHYSICALDRIVE0'], '\\\\?\\PHYSICALDRIVE0'],
3638
],
3739
],
3840
[ path.posix.resolve,

0 commit comments

Comments
 (0)