Browse Source

path: fix win32 relative() when "to" is a prefix

when the basename of "to" was a prefix of the basename of "from" win32
relative() would miss including it in the result

Fixes: https://github.com/nodejs/node/issues/5447
PR-URL: https://github.com/nodejs/node/pull/5456
Reviewed-By: Brian White <mscdex@mscdex.net>
Reviewed-By: Roman Reiss <me@silverwind.io>
process-exit-stdio-flushing
Owen Smith 9 years ago
committed by Roman Reiss
parent
commit
b33879d9e2
  1. 22
      lib/path.js
  2. 3
      test/parallel/test-path.js

22
lib/path.js

@ -603,14 +603,28 @@ const win32 = {
var i = 0; var i = 0;
for (; i <= length; ++i) { for (; i <= length; ++i) {
if (i === length) { if (i === length) {
if (lastCommonSep > 2 && // ignore separator match following device root if (toLen > length) {
toLen > length && if (to.charCodeAt(toStart + i) === 92/*\*/) {
to.charCodeAt(i) === 92/*\*/) {
// We get here if `from` is the exact base path for `to`. // We get here if `from` is the exact base path for `to`.
// For example: from='C:\\foo\\bar'; to='C:\\foo\\bar\\baz' // For example: from='C:\\foo\\bar'; to='C:\\foo\\bar\\baz'
return toOrig.slice(i + 1); 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; 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;
}
}
break; break;
} }
var fromCode = from.charCodeAt(fromStart + i); var fromCode = from.charCodeAt(fromStart + i);

3
test/parallel/test-path.js

@ -470,7 +470,8 @@ const relativeTests = [
['c:/AaAa/bbbb', 'c:/aaaa/bbbb', ''], ['c:/AaAa/bbbb', 'c:/aaaa/bbbb', ''],
['c:/aaaaa/', 'c:/aaaa/cccc', '..\\aaaa\\cccc'], ['c:/aaaaa/', 'c:/aaaa/cccc', '..\\aaaa\\cccc'],
['C:\\foo\\bar\\baz\\quux', 'C:\\', '..\\..\\..\\..'], ['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, [ path.posix.relative,

Loading…
Cancel
Save