Skip to content

Commit 528d878

Browse files
committed
src: fix memory leak in fs.writeSync error path
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 <[email protected]>
1 parent 648fc63 commit 528d878

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

src/node_file.cc

+7-2
Original file line numberDiff line numberDiff line change
@@ -853,9 +853,14 @@ static void WriteString(const FunctionCallbackInfo<Value>& args) {
853853
uv_buf_t uvbuf = uv_buf_init(const_cast<char*>(buf), len);
854854

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

0 commit comments

Comments
 (0)