Skip to content

Commit

Permalink
net: don't throw on bytesWritten access
Browse files Browse the repository at this point in the history
If bytesWritten is accessed before the object has been properly
constructed then return undefined.

Fixes: #3298
PR-URL: #3305
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
  • Loading branch information
trevnorris committed Oct 12, 2015
1 parent 3202456 commit a713024
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
16 changes: 16 additions & 0 deletions test/parallel/test-net-access-byteswritten.js
Original file line number Diff line number Diff line change
@@ -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);

0 comments on commit a713024

Please sign in to comment.