Skip to content

path: Throwing error on zero length strings #2105

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions lib/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
const util = require('util');
const isWindows = process.platform === 'win32';

function assertPath(path) {
function assertPath(path, allowEmpty) {
if (typeof path !== 'string') {
throw new TypeError('Path must be a string. Received ' +
util.inspect(path));
} else if (!path && !allowEmpty) {
throw new Error('path must not be a zero length string');
}
}

Expand Down Expand Up @@ -92,7 +94,7 @@ win32.resolve = function() {
}
}

assertPath(path);
assertPath(path, true);

// Skip empty entries
if (path === '') {
Expand Down Expand Up @@ -410,7 +412,7 @@ posix.resolve = function() {
for (var i = arguments.length - 1; i >= -1 && !resolvedAbsolute; i--) {
var path = (i >= 0) ? arguments[i] : process.cwd();

assertPath(path);
assertPath(path, true);

// Skip empty entries
if (path === '') {
Expand Down Expand Up @@ -463,9 +465,9 @@ posix.join = function() {
var path = '';
for (var i = 0; i < arguments.length; i++) {
var segment = arguments[i];
if (typeof segment !== 'string') {
throw new TypeError('Arguments to path.join must be strings');
}

assertPath(segment, true);

if (segment) {
if (!path) {
path += segment;
Expand Down
18 changes: 16 additions & 2 deletions test/parallel/test-path.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,6 @@ var joinTests =
[['/', '.'], '/'],
[['/', '..'], '/'],
[['/', '..', '..'], '/'],
[[''], '.'],
[['', ''], '.'],
[[' /foo'], ' /foo'],
[[' ', 'foo'], ' /foo'],
[[' ', '.'], ' '],
Expand Down Expand Up @@ -413,3 +411,19 @@ if (isWindows)
assert.deepEqual(path, path.win32, 'should be win32 path module');
else
assert.deepEqual(path, path.posix, 'should be posix path module');

const err = /path must not be a zero length string/;
const os = ['win32', 'posix'];
const functions = ['join', 'normalize', 'isAbsolute', 'relative'];
const params = [[['']], [['']], [['']], [['', ''], ['.', ''], ['', '.']]];

os.forEach(function(os) {
functions.forEach(function(fn, idx) {
params[idx].forEach(function(args) {
const call = `path.${os}.${fn}(${args.map(JSON.stringify).join(', ')})`;
assert.throws(function() {
path[os][fn].apply(null, args);
}, err, `${call} didn't throw expected Error`);
});
});
});