From f597b37efbfe25a22227e2852ec51f04c6df1792 Mon Sep 17 00:00:00 2001
From: Anna Henningsen <anna@addaleax.net>
Date: Mon, 18 Mar 2019 12:13:29 +0100
Subject: [PATCH] =?UTF-8?q?src:=20do=20not=20make=20`Resize(0)`=E2=80=99d?=
 =?UTF-8?q?=20buffers=20base=20`nullptr`?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

This fixes issues in which APIs that accept pointers created this way
treat `nullptr` and a zero-length buffer differently.
We already do something similar for our `Malloc()` implementation.

PR-URL: https://github.com/nodejs/node/pull/26731
Fixes: https://github.com/nodejs/node/issues/26514
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Richard Lau <riclau@uk.ibm.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
---
 src/env-inl.h | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/src/env-inl.h b/src/env-inl.h
index edf5d7a52e1d86..c794226d2c44a7 100644
--- a/src/env-inl.h
+++ b/src/env-inl.h
@@ -751,8 +751,10 @@ inline AllocatedBuffer::AllocatedBuffer(Environment* env, uv_buf_t buf)
     : env_(env), buffer_(buf) {}
 
 inline void AllocatedBuffer::Resize(size_t len) {
-  char* new_data = env_->Reallocate(buffer_.base, buffer_.len, len);
-  CHECK_IMPLIES(len > 0, new_data != nullptr);
+  // The `len` check is to make sure we don't end up with `nullptr` as our base.
+  char* new_data = env_->Reallocate(buffer_.base, buffer_.len,
+                                    len > 0 ? len : 1);
+  CHECK_NOT_NULL(new_data);
   buffer_ = uv_buf_init(new_data, len);
 }