From 207e48c93459da5e47f2efd408cfad6328bb0e25 Mon Sep 17 00:00:00 2001 From: Yosuke Furukawa Date: Tue, 27 Jan 2015 02:28:18 +0900 Subject: [PATCH] dgram: check close callback is function PR-URL: https://github.com/iojs/io.js/pull/609 Reviewed-By: Ben Noordhuis Reviewed-By: Evan Lucas --- lib/dgram.js | 2 +- .../test-dgram-close-is-not-callback.js | 21 +++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 test/parallel/test-dgram-close-is-not-callback.js diff --git a/lib/dgram.js b/lib/dgram.js index a919e16497..f005b8839d 100644 --- a/lib/dgram.js +++ b/lib/dgram.js @@ -341,7 +341,7 @@ function afterSend(err) { Socket.prototype.close = function(callback) { - if (callback) + if (typeof callback === 'function') this.on('close', callback); this._healthCheck(); this._stopReceiving(); diff --git a/test/parallel/test-dgram-close-is-not-callback.js b/test/parallel/test-dgram-close-is-not-callback.js new file mode 100644 index 0000000000..94035af970 --- /dev/null +++ b/test/parallel/test-dgram-close-is-not-callback.js @@ -0,0 +1,21 @@ +var assert = require('assert'); +var common = require('../common'); +var dgram = require('dgram'); + +var buf = new Buffer(1024); +buf.fill(42); + +var socket = dgram.createSocket('udp4'); +var closeEvents = 0; +socket.send(buf, 0, buf.length, common.PORT, 'localhost'); + +// if close callback is not function, ignore the argument. +socket.close('bad argument'); + +socket.on('close', function() { + ++closeEvents; +}); + +process.on('exit', function() { + assert.equal(closeEvents, 1); +});