Skip to content

Commit e40011b

Browse files
santigimenoRafaelGSS
authored andcommitted
src: fix crash on OnStreamRead on Windows
On Windows it's perfectly possible that the `uv_tcp_t` `read_cb` is called with an error and a null `uv_buf_t` if it corresponds to a `UV_HANDLE_ZERO_READ` read. Handle this case without crashing. Fixes: #40764 PR-URL: #45878 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent f1e300c commit e40011b

File tree

2 files changed

+51
-3
lines changed

2 files changed

+51
-3
lines changed

src/stream_base.cc

+4-3
Original file line numberDiff line numberDiff line change
@@ -583,9 +583,10 @@ void CustomBufferJSListener::OnStreamRead(ssize_t nread, const uv_buf_t& buf) {
583583
HandleScope handle_scope(env->isolate());
584584
Context::Scope context_scope(env->context());
585585

586-
// To deal with the case where POLLHUP is received and UV_EOF is returned, as
587-
// libuv returns an empty buffer (on unices only).
588-
if (nread == UV_EOF && buf.base == nullptr) {
586+
// In the case that there's an error and buf is null, return immediately.
587+
// This can happen on unices when POLLHUP is received and UV_EOF is returned
588+
// or when getting an error while performing a UV_HANDLE_ZERO_READ on Windows.
589+
if (buf.base == nullptr && nread < 0) {
589590
stream->CallJSOnreadMethod(nread, Local<ArrayBuffer>());
590591
return;
591592
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const assert = require('assert');
5+
const { spawn } = require('child_process');
6+
const net = require('net');
7+
8+
if (process.argv[2] === 'child') {
9+
const server = net.createServer(common.mustCall());
10+
server.listen(0, common.mustCall(() => {
11+
process.send({ type: 'ready', data: { port: server.address().port } });
12+
}));
13+
} else {
14+
const cp = spawn(process.execPath,
15+
[__filename, 'child'],
16+
{
17+
stdio: ['ipc', 'inherit', 'inherit']
18+
});
19+
20+
cp.on('exit', common.mustCall((code, signal) => {
21+
assert.strictEqual(code, null);
22+
assert.strictEqual(signal, 'SIGKILL');
23+
}));
24+
25+
cp.on('message', common.mustCall((msg) => {
26+
const { type, data } = msg;
27+
assert.strictEqual(type, 'ready');
28+
const port = data.port;
29+
30+
const conn = net.createConnection({
31+
port,
32+
onread: {
33+
buffer: Buffer.alloc(65536),
34+
callback: () => {},
35+
}
36+
});
37+
38+
conn.on('error', (err) => {
39+
// Error emitted on Windows.
40+
assert.strictEqual(err.code, 'ECONNRESET');
41+
});
42+
43+
conn.on('connect', common.mustCall(() => {
44+
cp.kill('SIGKILL');
45+
}));
46+
}));
47+
}

0 commit comments

Comments
 (0)