Skip to content

Commit b233131

Browse files
author
Julien Gilli
committed
src: fix builtin modules failing with --use-strict
Currently, lib/_tls_legacy.js and lib/crypto.js cannot be loaded when --use-strict is passed to node. In addition to that, console.trace throws because it uses arguments.callee. This change fixes these issues and adds a test that makes sure every external built-in module can be loaded with require when --use-strict is enabled. Please note that this change does not fix all issues with built-in modules' code running with --use-strict. It is very likely that some code in the built-in modules still fails when passing this flag. However, fixing all code would require us to enable strict mode by default in all builtins modules, which would certainly break existing applications. Fixes #9187. PR: #9237 PR-URL: nodejs/node-v0.x-archive#9237 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Trevor Norris <trev.norris@gmail.com>
1 parent 3e40e12 commit b233131

File tree

4 files changed

+64
-21
lines changed

4 files changed

+64
-21
lines changed

lib/_tls_legacy.js

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,24 @@ CryptoStream.prototype.end = function(chunk, encoding) {
426426
stream.Duplex.prototype.end.call(this, chunk, encoding);
427427
};
428428

429+
/*
430+
* Wait for both `finish` and `end` events to ensure that all data that
431+
* was written on this side was read from the other side.
432+
*/
433+
function _destroyWhenReadAndWriteEndsDone(cryptoStream) {
434+
var waiting = 1;
435+
436+
function finish() {
437+
if (--waiting === 0) cryptoStream.destroy();
438+
}
439+
440+
cryptoStream._opposite.once('end', finish);
441+
442+
if (!cryptoStream._finished) {
443+
cryptoStream.once('finish', finish);
444+
++waiting;
445+
}
446+
}
429447

430448
CryptoStream.prototype.destroySoon = function(err) {
431449
if (this === this.pair.cleartext) {
@@ -440,18 +458,7 @@ CryptoStream.prototype.destroySoon = function(err) {
440458
if (this._writableState.finished && this._opposite._ended) {
441459
this.destroy();
442460
} else {
443-
// Wait for both `finish` and `end` events to ensure that all data that
444-
// was written on this side was read from the other side.
445-
var self = this;
446-
var waiting = 1;
447-
function finish() {
448-
if (--waiting === 0) self.destroy();
449-
}
450-
this._opposite.once('end', finish);
451-
if (!this._finished) {
452-
this.once('finish', finish);
453-
++waiting;
454-
}
461+
_destroyWhenReadAndWriteEndsDone(this);
455462
}
456463
};
457464

lib/console.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,13 @@ Console.prototype.timeEnd = function(label) {
8989
};
9090

9191

92-
Console.prototype.trace = function() {
92+
Console.prototype.trace = function trace() {
9393
// TODO probably can to do this better with V8's debug object once that is
9494
// exposed.
9595
var err = new Error;
9696
err.name = 'Trace';
9797
err.message = util.format.apply(this, arguments);
98-
Error.captureStackTrace(err, arguments.callee);
98+
Error.captureStackTrace(err, trace);
9999
this.error(err.stack);
100100
};
101101

lib/crypto.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -592,20 +592,22 @@ exports.pbkdf2Sync = function(password, salt, iterations, keylen, digest) {
592592

593593

594594
function pbkdf2(password, salt, iterations, keylen, digest, callback) {
595+
var encoding = exports.DEFAULT_ENCODING;
596+
597+
function next(er, ret) {
598+
if (ret)
599+
ret = ret.toString(encoding);
600+
callback(er, ret);
601+
}
602+
595603
password = toBuf(password);
596604
salt = toBuf(salt);
597605

598-
if (exports.DEFAULT_ENCODING === 'buffer')
606+
if (encoding === 'buffer')
599607
return binding.PBKDF2(password, salt, iterations, keylen, digest, callback);
600608

601609
// at this point, we need to handle encodings.
602-
var encoding = exports.DEFAULT_ENCODING;
603610
if (callback) {
604-
function next(er, ret) {
605-
if (ret)
606-
ret = ret.toString(encoding);
607-
callback(er, ret);
608-
}
609611
binding.PBKDF2(password, salt, iterations, keylen, digest, next);
610612
} else {
611613
var ret = binding.PBKDF2(password, salt, iterations, keylen, digest);
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
/*
23+
* This test makes sure that every builtin module can be loaded
24+
* when the V8's --use-strict command line option is passed to node.
25+
*/
26+
27+
var child_process = require('child_process');
28+
29+
if (process.argv[2] !== 'child') {
30+
child_process.fork(__filename, ['child'], { execArgv: ['--use-strict']});
31+
} else {
32+
var natives = process.binding('natives');
33+
Object.keys(natives).forEach(require);
34+
}

0 commit comments

Comments
 (0)