From 528d8786ff75ab2a88e6bd0d4980cef96bdacb26 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Sat, 7 Mar 2015 19:08:41 +0100 Subject: [PATCH] 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 --- src/node_file.cc | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/node_file.cc b/src/node_file.cc index 27f904a92981f0..37dbb77f93eed2 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -853,9 +853,14 @@ static void WriteString(const FunctionCallbackInfo& args) { uv_buf_t uvbuf = uv_buf_init(const_cast(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); }