Skip to content

Commit

Permalink
timers: extract enroll() validation into a fn
Browse files Browse the repository at this point in the history
This should help keep everything consistent.

PR-URL: #17704
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anatoli Papirovski <apapirovski@mac.com>
  • Loading branch information
Fishrock123 committed Dec 20, 2017
1 parent 24dd92e commit 593941a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 39 deletions.
24 changes: 24 additions & 0 deletions lib/internal/timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ module.exports = {
trigger_async_id_symbol,
Timeout,
setUnrefTimeout,
validateTimerDuration
};

// Timer constructor function.
Expand Down Expand Up @@ -105,3 +106,26 @@ function setUnrefTimeout(callback, after, arg1, arg2, arg3) {

return timer;
}

// Type checking used by timers.enroll() and Socket#setTimeout()
function validateTimerDuration(msecs) {
if (typeof msecs !== 'number') {
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'msecs',
'number', msecs);
}

if (msecs < 0 || !isFinite(msecs)) {
throw new errors.RangeError('ERR_VALUE_OUT_OF_RANGE', 'msecs',
'a non-negative finite number', msecs);
}

// Ensure that msecs fits into signed int32
if (msecs > TIMEOUT_MAX) {
process.emitWarning(`${msecs} does not fit into a 32-bit signed integer.` +
`\nTimer duration was truncated to ${TIMEOUT_MAX}.`,
'TimeoutOverflowWarning');
return TIMEOUT_MAX;
}

return msecs;
}
24 changes: 6 additions & 18 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ var cluster = null;
const errnoException = util._errnoException;
const exceptionWithHostPort = util._exceptionWithHostPort;

const { kTimeout, TIMEOUT_MAX, setUnrefTimeout } = require('internal/timers');
const {
kTimeout,
setUnrefTimeout,
validateTimerDuration
} = require('internal/timers');

function noop() {}

Expand Down Expand Up @@ -394,23 +398,7 @@ Socket.prototype.read = function(n) {

Socket.prototype.setTimeout = function(msecs, callback) {
// Type checking identical to timers.enroll()
if (typeof msecs !== 'number') {
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'msecs',
'number', msecs);
}

if (msecs < 0 || !isFinite(msecs)) {
throw new errors.RangeError('ERR_VALUE_OUT_OF_RANGE', 'msecs',
'a non-negative finite number', msecs);
}

// Ensure that msecs fits into signed int32
if (msecs > TIMEOUT_MAX) {
process.emitWarning(`${msecs} does not fit into a 32-bit signed integer.` +
`\nTimer duration was truncated to ${TIMEOUT_MAX}.`,
'TimeoutOverflowWarning');
msecs = TIMEOUT_MAX;
}
msecs = validateTimerDuration(msecs);

// Attempt to clear an existing timer lear in both cases -
// even if it will be rescheduled we don't want to leak an existing timer.
Expand Down
22 changes: 1 addition & 21 deletions lib/timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,6 @@ delete process._scheduledImmediateCount;
const activateImmediateCheck = process._activateImmediateCheck;
delete process._activateImmediateCheck;

// Timeout values > TIMEOUT_MAX are set to 1.
const TIMEOUT_MAX = timerInternals.TIMEOUT_MAX;

// The Timeout class
const Timeout = timerInternals.Timeout;

Expand Down Expand Up @@ -392,29 +389,12 @@ const unenroll = exports.unenroll = function(item) {
// This function does not start the timer, see `active()`.
// Using existing objects as timers slightly reduces object overhead.
exports.enroll = function(item, msecs) {
if (typeof msecs !== 'number') {
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'msecs',
'number', msecs);
}

if (msecs < 0 || !isFinite(msecs)) {
throw new errors.RangeError('ERR_VALUE_OUT_OF_RANGE', 'msecs',
'a non-negative finite number', msecs);
}
item._idleTimeout = timerInternals.validateTimerDuration(msecs);

// if this item was already in a list somewhere
// then we should unenroll it from that
if (item._idleNext) unenroll(item);

// Ensure that msecs fits into signed int32
if (msecs > TIMEOUT_MAX) {
process.emitWarning(`${msecs} does not fit into a 32-bit signed integer.` +
`\nTimer duration was truncated to ${TIMEOUT_MAX}.`,
'TimeoutOverflowWarning');
msecs = TIMEOUT_MAX;
}

item._idleTimeout = msecs;
L.init(item);
};

Expand Down

0 comments on commit 593941a

Please sign in to comment.