Skip to content

Commit f597b37

Browse files
addaleaxtargos
authored andcommitted
src: do not make Resize(0)’d buffers base nullptr
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: #26731 Fixes: #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>
1 parent 14c3af7 commit f597b37

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

src/env-inl.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -751,8 +751,10 @@ inline AllocatedBuffer::AllocatedBuffer(Environment* env, uv_buf_t buf)
751751
: env_(env), buffer_(buf) {}
752752

753753
inline void AllocatedBuffer::Resize(size_t len) {
754-
char* new_data = env_->Reallocate(buffer_.base, buffer_.len, len);
755-
CHECK_IMPLIES(len > 0, new_data != nullptr);
754+
// The `len` check is to make sure we don't end up with `nullptr` as our base.
755+
char* new_data = env_->Reallocate(buffer_.base, buffer_.len,
756+
len > 0 ? len : 1);
757+
CHECK_NOT_NULL(new_data);
756758
buffer_ = uv_buf_init(new_data, len);
757759
}
758760

0 commit comments

Comments
 (0)