Copilot commented on code in PR #3657:
URL: https://github.com/apache/brpc/pull/3657#discussion_r4379113484


##########
src/brpc/details/ssl_helper.cpp:
##########
@@ -953,14 +953,15 @@ void Print(std::ostream& os, SSL* ssl, const char* sep) {
        << "protocol=" << SSL_get_version(ssl) << sep
        << "verify=" << (SSL_get_verify_mode(ssl) & SSL_VERIFY_PEER
                         ? "success" : "none");
-    X509* cert = SSL_get_peer_certificate(ssl);
+    std::unique_ptr<X509, decltype(&X509_free)> cert(
+        SSL_get_peer_certificate(ssl), X509_free);
     if (cert) {

Review Comment:
   `SSL_get_peer_certificate()` is deprecated in newer OpenSSL and its 
ownership semantics are less explicit than the newer APIs. Consider switching 
to `SSL_get1_peer_certificate()` (owning/up-ref) or 
`SSL_get0_peer_certificate()` (non-owning) depending on intended semantics; 
this reduces deprecation warnings and makes ownership clearer across SSL 
library versions.



##########
test/brpc_ssl_unittest.cpp:
##########
@@ -88,6 +90,75 @@ class SSLTest : public ::testing::Test{
     virtual void TearDown() {};
 };
 
+#ifndef USE_MESALINK
+static void MarkCertificateFreed(void*, void* ptr, CRYPTO_EX_DATA*,
+                                 int, long, void*) {
+    if (ptr != nullptr) {
+        *static_cast<bool*>(ptr) = true;
+    }
+}
+
+TEST_F(SSLTest, diagnostic_print_releases_peer_certificate) {
+    bool certificate_freed = false;
+    std::unique_ptr<SSL_CTX, decltype(&SSL_CTX_free)> server_ctx(
+        SSL_CTX_new(SSLv23_method()), SSL_CTX_free);
+    std::unique_ptr<SSL_CTX, decltype(&SSL_CTX_free)> client_ctx(
+        SSL_CTX_new(SSLv23_method()), SSL_CTX_free);

Review Comment:
   `SSLv23_method()` is deprecated in modern OpenSSL (it historically enabled 
SSLv2/v3 and is now an alias in some versions). Using `TLS_method()` avoids 
deprecation warnings and is more future-proof while still negotiating the best 
available TLS version.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to