Skip to content

Commit

Permalink
path: fix win32 relative() when "to" is a prefix
Browse files Browse the repository at this point in the history
when the basename of "to" was a prefix of the basename of "from" win32
relative() would miss including it in the result

Fixes: #5447
PR-URL: #5456
Reviewed-By: Brian White <[email protected]>
Reviewed-By: Roman Reiss <[email protected]>
  • Loading branch information
omsmith authored and rvagg committed Feb 28, 2016
1 parent dfe45f1 commit 91782f1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
28 changes: 21 additions & 7 deletions lib/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -603,14 +603,28 @@ const win32 = {
var i = 0;
for (; i <= length; ++i) {
if (i === length) {
if (lastCommonSep > 2 && // ignore separator match following device root
toLen > length &&
to.charCodeAt(i) === 92/*\*/) {
// We get here if `from` is the exact base path for `to`.
// For example: from='C:\\foo\\bar'; to='C:\\foo\\bar\\baz'
return toOrig.slice(i + 1);
if (toLen > length) {
if (to.charCodeAt(toStart + i) === 92/*\*/) {
// We get here if `from` is the exact base path for `to`.
// For example: from='C:\\foo\\bar'; to='C:\\foo\\bar\\baz'
return toOrig.slice(toStart + i + 1);
} else if (lastCommonSep === 2) {
// We get here if `from` is the device root.
// For example: from='C:\\'; to='C:\\foo'
return toOrig.slice(toStart + i);
}
}
if (fromLen > length) {
if (from.charCodeAt(fromStart + i) === 92/*\*/) {
// We get here if `to` is the exact base path for `from`.
// For example: from='C:\\foo\\bar'; to='C:\\foo'
lastCommonSep = i;
} else if (lastCommonSep === 2) {
// We get here if `to` is the device root.
// For example: from='C:\\foo\\bar'; to='C:\\'
lastCommonSep = 3;
}
}
lastCommonSep = i;
break;
}
var fromCode = from.charCodeAt(fromStart + i);
Expand Down
3 changes: 2 additions & 1 deletion test/parallel/test-path.js
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,8 @@ const relativeTests = [
['c:/AaAa/bbbb', 'c:/aaaa/bbbb', ''],
['c:/aaaaa/', 'c:/aaaa/cccc', '..\\aaaa\\cccc'],
['C:\\foo\\bar\\baz\\quux', 'C:\\', '..\\..\\..\\..'],
['C:\\foo\\test', 'C:\\foo\\test\\bar\\package.json', 'bar\\package.json']
['C:\\foo\\test', 'C:\\foo\\test\\bar\\package.json', 'bar\\package.json'],
['C:\\foo\\bar\\baz-quux', 'C:\\foo\\bar\\baz', '..\\baz']
]
],
[ path.posix.relative,
Expand Down

0 comments on commit 91782f1

Please sign in to comment.