Skip to content

Commit

Permalink
src: do not make Resize(0)’d buffers base nullptr
Browse files Browse the repository at this point in the history
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 <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>
  • Loading branch information
addaleax authored and BridgeAR committed Mar 21, 2019
1 parent bca23f2 commit d117e41
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/env-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -759,8 +759,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);
}

Expand Down

0 comments on commit d117e41

Please sign in to comment.