Skip to content

Commit

Permalink
src: use OnScopeLeave instead of multiple free()
Browse files Browse the repository at this point in the history
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: #44852
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Daeyeon Jeong <daeyeon.dev@gmail.com>
Reviewed-By: Darshan Sen <raisinten@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
tniessen authored Oct 3, 2022
1 parent ad8442f commit b5e1387
Showing 1 changed file with 3 additions and 4 deletions.
7 changes: 3 additions & 4 deletions src/crypto/crypto_common.cc
Original file line number Diff line number Diff line change
Expand Up @@ -992,18 +992,17 @@ static MaybeLocal<Value> GetX509NameObject(Environment* env, X509* cert) {
if (value_str_size < 0) {
return Undefined(env->isolate());
}
auto free_value_str = OnScopeLeave([&]() { OPENSSL_free(value_str); });

Local<String> v8_value;
if (!String::NewFromUtf8(env->isolate(),
reinterpret_cast<const char*>(value_str),
NewStringType::kNormal,
value_str_size).ToLocal(&v8_value)) {
OPENSSL_free(value_str);
value_str_size)
.ToLocal(&v8_value)) {
return MaybeLocal<Value>();
}

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
Expand Down

0 comments on commit b5e1387

Please sign in to comment.