Skip to content

Commit 5844691

Browse files
brendanashworthaddaleax
authored andcommitted
net: fix bytesWritten during writev
When a writev is caused on a socket (sometimes through corking and uncorking), previously net would call Buffer.byteLength on the array of buffers and chunks. This throws a TypeError, because Buffer.byteLength throws when passed a non-string. In dbfe8c4, behavior changed to throw when passed a non-string. This is correct behavior. Previously, it would cast the argument to a string, so before this commit, bytesWritten would give an erroneous value. This commit corrects the behavior equally both before and after dbfe8c4. This commit fixes this bug by iterating over each chunk in the pending stack and calculating the length individually. Also adds a regression test. This additionally changes an `instanceof Buffer` check to `typeof !== 'string'`, which should be equivalent. PR-URL: #14236 Reviewed-By: Brian White <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Tobias Nießen <[email protected]> Refs: #2960
1 parent 8fea174 commit 5844691

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

lib/net.js

+13-2
Original file line numberDiff line numberDiff line change
@@ -825,8 +825,19 @@ protoGetter('bytesWritten', function bytesWritten() {
825825
bytes += Buffer.byteLength(el.chunk, el.encoding);
826826
});
827827

828-
if (data) {
829-
if (data instanceof Buffer)
828+
if (Array.isArray(data)) {
829+
// was a writev, iterate over chunks to get total length
830+
for (var i = 0; i < data.length; i++) {
831+
const chunk = data[i];
832+
833+
if (data.allBuffers || chunk instanceof Buffer)
834+
bytes += chunk.length;
835+
else
836+
bytes += Buffer.byteLength(chunk.chunk, chunk.encoding);
837+
}
838+
} else if (data) {
839+
// Writes are either a string or a Buffer.
840+
if (typeof data !== 'string')
830841
bytes += data.length;
831842
else
832843
bytes += Buffer.byteLength(data, encoding);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const assert = require('assert');
5+
const net = require('net');
6+
7+
const server = net.createServer(function(socket) {
8+
socket.end();
9+
});
10+
11+
server.listen(0, common.mustCall(function() {
12+
const socket = net.connect(server.address().port);
13+
14+
// Cork the socket, then write twice; this should cause a writev, which
15+
// previously caused an err in the bytesWritten count.
16+
socket.cork();
17+
18+
socket.write('one');
19+
socket.write(new Buffer('twø', 'utf8'));
20+
21+
socket.uncork();
22+
23+
// one = 3 bytes, twø = 4 bytes
24+
assert.strictEqual(socket.bytesWritten, 3 + 4);
25+
26+
socket.on('connect', common.mustCall(function() {
27+
assert.strictEqual(socket.bytesWritten, 3 + 4);
28+
}));
29+
30+
socket.on('end', common.mustCall(function() {
31+
assert.strictEqual(socket.bytesWritten, 3 + 4);
32+
33+
server.close();
34+
}));
35+
}));

0 commit comments

Comments
 (0)