From 93f3b640d06df5e123e7ede90e40df06258d9d47 Mon Sep 17 00:00:00 2001 From: Alexis Campailla Date: Tue, 5 Aug 2014 15:33:16 +0200 Subject: [PATCH] windows: fix memory leak in WinapiErrnoException Fix https://github.com/joyent/node/issues/2341 Reviewed-By: Fedor Indutny --- src/node.cc | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/node.cc b/src/node.cc index f3409dc..d4247d0 100644 --- a/src/node.cc +++ b/src/node.cc @@ -833,7 +833,7 @@ Local UVException(Isolate* isolate, #ifdef _WIN32 // Does about the same as strerror(), // but supports all windows error messages -static const char *winapi_strerror(const int errorno) { +static const char *winapi_strerror(const int errorno, bool* must_free) { char *errmsg = NULL; FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | @@ -841,6 +841,8 @@ static const char *winapi_strerror(const int errorno) { MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&errmsg, 0, NULL); if (errmsg) { + *must_free = true; + // Remove trailing newlines for (int i = strlen(errmsg) - 1; i >= 0 && (errmsg[i] == '\n' || errmsg[i] == '\r'); i--) { @@ -850,6 +852,7 @@ static const char *winapi_strerror(const int errorno) { return errmsg; } else { // FormatMessage failed + *must_free = false; return "Unknown error"; } } @@ -862,8 +865,9 @@ Local WinapiErrnoException(Isolate* isolate, const char* path) { Environment* env = Environment::GetCurrent(isolate); Local e; + bool must_free = false; if (!msg || !msg[0]) { - msg = winapi_strerror(errorno); + msg = winapi_strerror(errorno, &must_free); } Local message = OneByteString(env->isolate(), msg); @@ -890,6 +894,9 @@ Local WinapiErrnoException(Isolate* isolate, obj->Set(env->syscall_string(), OneByteString(isolate, syscall)); } + if (must_free) + LocalFree((HLOCAL)msg); + return e; } #endif -- 2.7.4