Skip to content

Commit 0c91a83

Browse files
TooTallNatebnoordhuis
authored andcommitted
util: isRegExp() should not call toString() on its argument
An overloaded toString() method may have side effects so don't call it for a simple type check. Back-port of 54b409d to the v0.4 branch.
1 parent 827b9c5 commit 0c91a83

File tree

2 files changed

+35
-9
lines changed

2 files changed

+35
-9
lines changed

lib/util.js

+2-9
Original file line numberDiff line numberDiff line change
@@ -284,15 +284,8 @@ function isArray(ar) {
284284

285285

286286
function isRegExp(re) {
287-
var s = '' + re;
288-
return re instanceof RegExp || // easy case
289-
// duck-type for context-switching evalcx case
290-
typeof(re) === 'function' &&
291-
re.constructor.name === 'RegExp' &&
292-
re.compile &&
293-
re.test &&
294-
re.exec &&
295-
s.match(/^\/.*\/[gim]{0,3}$/);
287+
return re instanceof RegExp ||
288+
(typeof re === 'object' && Object.prototype.toString.call(re) === '[object RegExp]');
296289
}
297290

298291

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright Joyent, Inc. and other Node contributors.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the
5+
// "Software"), to deal in the Software without restriction, including
6+
// without limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to permit
8+
// persons to whom the Software is furnished to do so, subject to the
9+
// following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
var common = require('../common');
23+
var assert = require('assert');
24+
25+
var func = function () {};
26+
var toStringCalled = false;
27+
func.toString = function () {
28+
toStringCalled = true;
29+
};
30+
31+
require('util').inspect(func);
32+
33+
assert.ok(!toStringCalled);

0 commit comments

Comments
 (0)