Skip to content

Commit d117e41

Browse files
addaleaxBridgeAR
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: nodejs#26731 Fixes: nodejs#26514 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Richard Lau <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Michael Dawson <[email protected]> Reviewed-By: Luigi Pinca <[email protected]>
1 parent bca23f2 commit d117e41

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

src/env-inl.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -759,8 +759,10 @@ inline AllocatedBuffer::AllocatedBuffer(Environment* env, uv_buf_t buf)
759759
: env_(env), buffer_(buf) {}
760760

761761
inline void AllocatedBuffer::Resize(size_t len) {
762-
char* new_data = env_->Reallocate(buffer_.base, buffer_.len, len);
763-
CHECK_IMPLIES(len > 0, new_data != nullptr);
762+
// The `len` check is to make sure we don't end up with `nullptr` as our base.
763+
char* new_data = env_->Reallocate(buffer_.base, buffer_.len,
764+
len > 0 ? len : 1);
765+
CHECK_NOT_NULL(new_data);
764766
buffer_ = uv_buf_init(new_data, len);
765767
}
766768

0 commit comments

Comments
 (0)