Skip to content

Commit

Permalink
src: fix memory leak in fs.writeSync error path
Browse files Browse the repository at this point in the history
The SYNC_CALL macro returns on error, bypassing the delete[] call.

Mea culpa, it looks like I introduced this memory leak back in 2013,
in commit d2b80b8 ("src: clean up FSReqWrap").

PR-URL: #1092
Reviewed-By: Fedor Indutny <fedor@indutny.com>
  • Loading branch information
bnoordhuis committed Mar 7, 2015
1 parent 648fc63 commit 528d878
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -853,9 +853,14 @@ static void WriteString(const FunctionCallbackInfo<Value>& args) {
uv_buf_t uvbuf = uv_buf_init(const_cast<char*>(buf), len);

if (!req->IsObject()) {
// SYNC_CALL returns on error. Make sure to always free the memory.
struct Delete {
inline explicit Delete(char* pointer) : pointer_(pointer) {}
inline ~Delete() { delete[] pointer_; }
char* const pointer_;
};
Delete delete_on_return(ownership == FSReqWrap::MOVE ? buf : nullptr);
SYNC_CALL(write, nullptr, fd, &uvbuf, 1, pos)
if (ownership == FSReqWrap::MOVE)
delete[] buf;
return args.GetReturnValue().Set(SYNC_RESULT);
}

Expand Down

0 comments on commit 528d878

Please sign in to comment.