Skip to content

Commit a713024

Browse files
committed
net: don't throw on bytesWritten access
If bytesWritten is accessed before the object has been properly constructed then return undefined. Fixes: #3298 PR-URL: #3305 Reviewed-By: Ben Noordhuis <[email protected]>
1 parent 3202456 commit a713024

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

lib/net.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -727,6 +727,9 @@ Socket.prototype.__defineGetter__('bytesWritten', function() {
727727
data = this._pendingData,
728728
encoding = this._pendingEncoding;
729729

730+
if (!state)
731+
return undefined;
732+
730733
state.getBuffer().forEach(function(el) {
731734
if (el.chunk instanceof Buffer)
732735
bytes += el.chunk.length;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict';
2+
3+
require('../common');
4+
const assert = require('assert');
5+
const net = require('net');
6+
const tls = require('tls');
7+
const tty = require('tty');
8+
9+
// Check that the bytesWritten getter doesn't crash if object isn't
10+
// constructed.
11+
assert.strictEqual(net.Socket.prototype.bytesWritten, undefined);
12+
assert.strictEqual(tls.TLSSocket.super_.prototype.bytesWritten, undefined);
13+
assert.strictEqual(tls.TLSSocket.prototype.bytesWritten, undefined);
14+
assert.strictEqual(tty.ReadStream.super_.prototype.bytesWritten, undefined);
15+
assert.strictEqual(tty.ReadStream.prototype.bytesWritten, undefined);
16+
assert.strictEqual(tty.WriteStream.prototype.bytesWritten, undefined);

0 commit comments

Comments
 (0)