Skip to content

Commit

Permalink
src: fix crash on OnStreamRead on Windows
Browse files Browse the repository at this point in the history
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
  • Loading branch information
santigimeno committed Dec 20, 2022
1 parent 7738844 commit 469f8d2
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/stream_base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -583,9 +583,10 @@ void CustomBufferJSListener::OnStreamRead(ssize_t nread, const uv_buf_t& buf) {
HandleScope handle_scope(env->isolate());
Context::Scope context_scope(env->context());

// To deal with the case where POLLHUP is received and UV_EOF is returned, as
// libuv returns an empty buffer (on unices only).
if (nread == UV_EOF && buf.base == nullptr) {
// In the case that there's an error and buf is null, return immediately.
// This can happen on unices when POLLHUP is received and UV_EOF is returned
// or when getting an error while performing a UV_HANDLE_ZERO_READ on Windows.
if (buf.base == nullptr && nread < 0) {
stream->CallJSOnreadMethod(nread, Local<ArrayBuffer>());
return;
}
Expand Down
47 changes: 47 additions & 0 deletions test/parallel/test-net-child-process-connect-reset.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
'use strict';

const common = require('../common');
const assert = require('assert');
const { spawn } = require('child_process');
const net = require('net');

if (process.argv[2] === 'child') {
const server = net.createServer(common.mustCall());
server.listen(0, common.mustCall(() => {
process.send({ type: 'ready', data: { port: server.address().port } });
}));
} else {
const cp = spawn(process.execPath,
[__filename, 'child'],
{
stdio: ['ipc', 'inherit', 'inherit']
});

cp.on('exit', common.mustCall((code, signal) => {
assert.strictEqual(code, null);
assert.strictEqual(signal, 'SIGKILL');
}));

cp.on('message', common.mustCall((msg) => {
const { type, data } = msg;
assert.strictEqual(type, 'ready');
const port = data.port;

const conn = net.createConnection({
port,
onread: {
buffer: Buffer.alloc(65536),
callback: () => {},
}
});

conn.on('error', (err) => {
// Error emitted on Windows.
assert.strictEqual(err.code, 'ECONNRESET');
});

conn.on('connect', common.mustCall(() => {
cp.kill('SIGKILL');
}));
}));
}

0 comments on commit 469f8d2

Please sign in to comment.