From 12e7e93536dc5cb5d8c26a10ebd52d1e8090e139 Mon Sep 17 00:00:00 2001 From: John French Date: Fri, 28 Dec 2018 01:08:53 +0100 Subject: [PATCH] src: fix callbackData leaks on error napi status PR-URL: https://github.com/nodejs/node-addon-api/pull/417 Reviewed-By: Gabriel Schulhof Reviewed-By: Michael Dawson --- napi-inl.h | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/napi-inl.h b/napi-inl.h index aeb14ff..40959d8 100644 --- a/napi-inl.h +++ b/napi-inl.h @@ -1673,7 +1673,11 @@ inline Function Function::New(napi_env env, CbData::Wrapper, callbackData, &value); - NAPI_THROW_IF_FAILED(env, status, Function()); + if (status != napi_ok) { + delete callbackData; + NAPI_THROW_IF_FAILED(env, status, Function()); + } + return Function(env, value); } @@ -2636,7 +2640,10 @@ PropertyDescriptor::Accessor(Napi::Env env, auto callbackData = new CbData({ getter, data }); napi_status status = AttachData(env, object, callbackData); - NAPI_THROW_IF_FAILED(env, status, napi_property_descriptor()); + if (status != napi_ok) { + delete callbackData; + NAPI_THROW_IF_FAILED(env, status, napi_property_descriptor()); + } return PropertyDescriptor({ utf8name, @@ -2671,7 +2678,10 @@ inline PropertyDescriptor PropertyDescriptor::Accessor(Napi::Env env, auto callbackData = new CbData({ getter, data }); napi_status status = AttachData(env, object, callbackData); - NAPI_THROW_IF_FAILED(env, status, napi_property_descriptor()); + if (status != napi_ok) { + delete callbackData; + NAPI_THROW_IF_FAILED(env, status, napi_property_descriptor()); + } return PropertyDescriptor({ nullptr, @@ -2697,7 +2707,10 @@ inline PropertyDescriptor PropertyDescriptor::Accessor(Napi::Env env, auto callbackData = new CbData({ getter, setter, data }); napi_status status = AttachData(env, object, callbackData); - NAPI_THROW_IF_FAILED(env, status, napi_property_descriptor()); + if (status != napi_ok) { + delete callbackData; + NAPI_THROW_IF_FAILED(env, status, napi_property_descriptor()); + } return PropertyDescriptor({ utf8name, @@ -2734,7 +2747,10 @@ inline PropertyDescriptor PropertyDescriptor::Accessor(Napi::Env env, auto callbackData = new CbData({ getter, setter, data }); napi_status status = AttachData(env, object, callbackData); - NAPI_THROW_IF_FAILED(env, status, napi_property_descriptor()); + if (status != napi_ok) { + delete callbackData; + NAPI_THROW_IF_FAILED(env, status, napi_property_descriptor()); + } return PropertyDescriptor({ nullptr,