summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTrevor Norris <trev.norris@gmail.com>2015-10-09 16:51:42 -0600
committerJames M Snell <jasnell@gmail.com>2015-10-12 10:25:49 -0700
commit01cb3fc36b0ca5f6e24ccb47915487e7c6a0e609 (patch)
tree28e7c3c5317dd010f35790ccc544e53d47269a06
parent42b936e78ddb0131ad620c8cc87acf1f1f5325d9 (diff)
downloadnode-new-01cb3fc36b0ca5f6e24ccb47915487e7c6a0e609.tar.gz
net: don't throw on bytesWritten access
If bytesWritten is accessed before the object has been properly constructed then return undefined. Fixes: https://github.com/nodejs/node/issues/3298 PR-URL: https://github.com/nodejs/node/pull/3305 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
-rw-r--r--lib/net.js3
-rw-r--r--test/parallel/test-net-access-byteswritten.js16
2 files changed, 19 insertions, 0 deletions
diff --git a/lib/net.js b/lib/net.js
index 47422c30db..a7a8eb1fcc 100644
--- a/lib/net.js
+++ b/lib/net.js
@@ -727,6 +727,9 @@ Socket.prototype.__defineGetter__('bytesWritten', function() {
data = this._pendingData,
encoding = this._pendingEncoding;
+ if (!state)
+ return undefined;
+
state.getBuffer().forEach(function(el) {
if (el.chunk instanceof Buffer)
bytes += el.chunk.length;
diff --git a/test/parallel/test-net-access-byteswritten.js b/test/parallel/test-net-access-byteswritten.js
new file mode 100644
index 0000000000..362d533bbb
--- /dev/null
+++ b/test/parallel/test-net-access-byteswritten.js
@@ -0,0 +1,16 @@
+'use strict';
+
+require('../common');
+const assert = require('assert');
+const net = require('net');
+const tls = require('tls');
+const tty = require('tty');
+
+// Check that the bytesWritten getter doesn't crash if object isn't
+// constructed.
+assert.strictEqual(net.Socket.prototype.bytesWritten, undefined);
+assert.strictEqual(tls.TLSSocket.super_.prototype.bytesWritten, undefined);
+assert.strictEqual(tls.TLSSocket.prototype.bytesWritten, undefined);
+assert.strictEqual(tty.ReadStream.super_.prototype.bytesWritten, undefined);
+assert.strictEqual(tty.ReadStream.prototype.bytesWritten, undefined);
+assert.strictEqual(tty.WriteStream.prototype.bytesWritten, undefined);