From 730acb50e387335a9248ae023522b18cd1258065 Mon Sep 17 00:00:00 2001 From: Santiago Gimeno Date: Tue, 31 Mar 2020 20:56:01 +0200 Subject: [PATCH] net: fix crash if POLLHUP is received If the `onread` socket option is used and a `POLLHUP` event is received, libuv returns `UV_EOF` along with a `NULL` buffer in the read callback, causing the crash. Deal with this case. Fixes: https://github.com/nodejs/node/issues/31823 PR-URL: https://github.com/nodejs/node/pull/32590 Reviewed-By: Anna Henningsen Reviewed-By: Ben Noordhuis Reviewed-By: James M Snell --- src/stream_base.cc | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/stream_base.cc b/src/stream_base.cc index 63b06378f7c127..d8a191dc2e5f6d 100644 --- a/src/stream_base.cc +++ b/src/stream_base.cc @@ -517,13 +517,21 @@ uv_buf_t CustomBufferJSListener::OnStreamAlloc(size_t suggested_size) { void CustomBufferJSListener::OnStreamRead(ssize_t nread, const uv_buf_t& buf) { CHECK_NOT_NULL(stream_); - CHECK_EQ(buf.base, buffer_.base); StreamBase* stream = static_cast(stream_); Environment* env = stream->stream_env(); 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) { + stream->CallJSOnreadMethod(nread, Local()); + return; + } + + CHECK_EQ(buf.base, buffer_.base); + MaybeLocal ret = stream->CallJSOnreadMethod(nread, Local(), 0,