Skip to content

Commit 9e87091

Browse files
committed
src: fix double free reported by coverity
Fix double free reported by coverity. ToBufferEndian() in node_i18n.cc was the only caller of Buffer::New() passing in a MaybeStackBuffer. Coverity reported a double free because there were paths in which the src buffer would be deleted by both the destruction of the MaybeStackBuffer and by the Buffer which was done even in failure cases for Buffer::New(). Signed-off-by: Michael Dawson <[email protected]> PR-URL: #51046 Reviewed-By: James M Snell <[email protected]>
1 parent 6e90fed commit 9e87091

File tree

1 file changed

+5
-8
lines changed

1 file changed

+5
-8
lines changed

src/node_internals.h

+5-8
Original file line numberDiff line numberDiff line change
@@ -192,16 +192,13 @@ static v8::MaybeLocal<v8::Object> New(Environment* env,
192192
char* src = reinterpret_cast<char*>(buf->out());
193193
const size_t len_in_bytes = buf->length() * sizeof(buf->out()[0]);
194194

195-
if (buf->IsAllocated())
195+
if (buf->IsAllocated()) {
196196
ret = New(env, src, len_in_bytes);
197-
else if (!buf->IsInvalidated())
198-
ret = Copy(env, src, len_in_bytes);
199-
200-
if (ret.IsEmpty())
201-
return ret;
202-
203-
if (buf->IsAllocated())
197+
// new always takes ownership of src
204198
buf->Release();
199+
} else if (!buf->IsInvalidated()) {
200+
ret = Copy(env, src, len_in_bytes);
201+
}
205202

206203
return ret;
207204
}

0 commit comments

Comments
 (0)