From 781ad96227414970456426ec1ecec511a0862ced Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Nie=C3=9Fen?= Date: Mon, 3 Oct 2022 22:37:02 +0200 Subject: [PATCH] src: use OnScopeLeave instead of multiple free() This is not great either but it avoids having to call OPENSSL_free() in more than one branch, thus reducing the risk of memory leaks. PR-URL: https://github.com/nodejs/node/pull/44852 Reviewed-By: Colin Ihrig Reviewed-By: Daeyeon Jeong Reviewed-By: Darshan Sen Reviewed-By: James M Snell --- src/crypto/crypto_common.cc | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/crypto/crypto_common.cc b/src/crypto/crypto_common.cc index 3bf480f8f0c77d..97777371265510 100644 --- a/src/crypto/crypto_common.cc +++ b/src/crypto/crypto_common.cc @@ -992,18 +992,17 @@ static MaybeLocal GetX509NameObject(Environment* env, X509* cert) { if (value_str_size < 0) { return Undefined(env->isolate()); } + auto free_value_str = OnScopeLeave([&]() { OPENSSL_free(value_str); }); Local v8_value; if (!String::NewFromUtf8(env->isolate(), reinterpret_cast(value_str), NewStringType::kNormal, - value_str_size).ToLocal(&v8_value)) { - OPENSSL_free(value_str); + value_str_size) + .ToLocal(&v8_value)) { return MaybeLocal(); } - OPENSSL_free(value_str); - // For backward compatibility, we only create arrays if multiple values // exist for the same key. That is not great but there is not much we can // change here without breaking things. Note that this creates nested data