From 3072546feb9d7f78f12d75bec28ef00e5958f7be Mon Sep 17 00:00:00 2001 From: Brian White Date: Sun, 3 Apr 2016 08:37:04 -0400 Subject: [PATCH] path: fix win32.isAbsolute() inconsistency This commit fixes an inconsistency in absolute path checking compared to the absolute path detection used by the other path.win32 functions. Fixes: https://github.com/nodejs/node/issues/6027 PR-URL: https://github.com/nodejs/node/pull/6028 Reviewed-By: Benjamin Gruenbaum Reviewed-By: James M Snell --- lib/path.js | 60 +++++++------------------------------- test/parallel/test-path.js | 10 +++++++ 2 files changed, 20 insertions(+), 50 deletions(-) diff --git a/lib/path.js b/lib/path.js index 000dd2359d6f26..cad86ab9a21731 100644 --- a/lib/path.js +++ b/lib/path.js @@ -439,57 +439,17 @@ const win32 = { if (len === 0) return false; var code = path.charCodeAt(0); - if (len > 1) { - if (code === 47/*/*/ || code === 92/*\*/) { - // Possible UNC root - - code = path.charCodeAt(1); - if (code === 47/*/*/ || code === 92/*\*/) { - // Matched double path separator at beginning - var j = 2; - var last = j; - // Match 1 or more non-path separators - for (; j < len; ++j) { - code = path.charCodeAt(j); - if (code === 47/*/*/ || code === 92/*\*/) - break; - } - if (j < len && j !== last) { - // Matched! - last = j; - // Match 1 or more path separators - for (; j < len; ++j) { - code = path.charCodeAt(j); - if (code !== 47/*/*/ && code !== 92/*\*/) - break; - } - if (j < len && j !== last) { - // Matched! - last = j; - // Match 1 or more non-path separators - for (; j < len; ++j) { - code = path.charCodeAt(j); - if (code === 47/*/*/ || code === 92/*\*/) - break; - } - if (j !== last) - return true; - } - } - } - } else if ((code >= 65/*A*/ && code <= 90/*Z*/) || - (code >= 97/*a*/ && code <= 122/*z*/)) { - // Possible device root - - code = path.charCodeAt(1); - if (path.charCodeAt(1) === 58/*:*/ && len > 2) { - code = path.charCodeAt(2); - if (code === 47/*/*/ || code === 92/*\*/) - return true; - } - } - } else if (code === 47/*/*/ || code === 92/*\*/) { + if (code === 47/*/*/ || code === 92/*\*/) { return true; + } else if ((code >= 65/*A*/ && code <= 90/*Z*/) || + (code >= 97/*a*/ && code <= 122/*z*/)) { + // Possible device root + + if (len > 2 && path.charCodeAt(1) === 58/*:*/) { + code = path.charCodeAt(2); + if (code === 47/*/*/ || code === 92/*\*/) + return true; + } } return false; }, diff --git a/test/parallel/test-path.js b/test/parallel/test-path.js index 4d0628a4c3f586..267b0ac14de3d0 100644 --- a/test/parallel/test-path.js +++ b/test/parallel/test-path.js @@ -442,8 +442,18 @@ assert.equal(failures.length, 0, failures.join('')); // path.isAbsolute tests +assert.equal(path.win32.isAbsolute('/'), true); +assert.equal(path.win32.isAbsolute('//'), true); +assert.equal(path.win32.isAbsolute('//server'), true); assert.equal(path.win32.isAbsolute('//server/file'), true); assert.equal(path.win32.isAbsolute('\\\\server\\file'), true); +assert.equal(path.win32.isAbsolute('\\\\server'), true); +assert.equal(path.win32.isAbsolute('\\\\'), true); +assert.equal(path.win32.isAbsolute('c'), false); +assert.equal(path.win32.isAbsolute('c:'), false); +assert.equal(path.win32.isAbsolute('c:\\'), true); +assert.equal(path.win32.isAbsolute('c:/'), true); +assert.equal(path.win32.isAbsolute('c://'), true); assert.equal(path.win32.isAbsolute('C:/Users/'), true); assert.equal(path.win32.isAbsolute('C:\\Users\\'), true); assert.equal(path.win32.isAbsolute('C:cwd/another'), false);