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 d2b80b8a ("src: clean up FSReqWrap").

PR-URL: https://github.com/iojs/io.js/pull/1092
Reviewed-By: Fedor Indutny <fedor@indutny.com>
pull/1092/head
Ben Noordhuis 2015-03-07 19:08:41 +01:00
parent 648fc63cd1
commit 528d8786ff
1 changed files with 7 additions and 2 deletions

View File

@ -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);
}