Implement a public BIO_ADDR_copy() function
authorMatt Caswell <matt@openssl.org>
Thu, 21 Sep 2023 10:59:58 +0000 (11:59 +0100)
committerPauli <pauli@openssl.org>
Sun, 24 Sep 2023 21:46:45 +0000 (07:46 +1000)
We already have BIO_ADDR_dup() but in some contexts that is not sufficent.
We implement BIO_ADDR_copy() and make BIO_ADDR_dup() use it.

Reviewed-by: Tomas Mraz <tomas@openssl.org>
Reviewed-by: Tim Hudson <tjh@openssl.org>
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/22164)

crypto/bio/bio_addr.c
include/openssl/bio.h.in
util/libcrypto.num

index aec94237fc8c8815b6049895019145341e84600f..2a6f6d522c7a51d848d298b2f8e5e98805d99992 100644 (file)
@@ -65,14 +65,29 @@ void BIO_ADDR_free(BIO_ADDR *ap)
     OPENSSL_free(ap);
 }
 
+int BIO_ADDR_copy(BIO_ADDR *dst, const BIO_ADDR *src)
+{
+    if (dst == NULL || src == NULL)
+        return 0;
+
+    if (src->sa.sa_family == AF_UNSPEC) {
+        BIO_ADDR_clear(dst);
+        return 1;
+    }
+
+    return BIO_ADDR_make(dst, &src->sa);
+}
+
 BIO_ADDR *BIO_ADDR_dup(const BIO_ADDR *ap)
 {
     BIO_ADDR *ret = NULL;
 
     if (ap != NULL) {
         ret = BIO_ADDR_new();
-        if (ret != NULL)
-            BIO_ADDR_make(ret, &ap->sa);
+        if (ret != NULL && !BIO_ADDR_copy(ret, ap)) {
+            BIO_ADDR_free(ret);
+            ret = NULL;
+        }
     }
     return ret;
 }
index 8aad1414460d9533ab1e43f5d67852011da5387a..c534dcd76cdb6d6ea0228979aa8c76a6b744c43a 100644 (file)
@@ -806,6 +806,7 @@ int BIO_hex_string(BIO *out, int indent, int width, const void *data,
 
 # ifndef OPENSSL_NO_SOCK
 BIO_ADDR *BIO_ADDR_new(void);
+int BIO_ADDR_copy(BIO_ADDR *dst, const BIO_ADDR *src);
 BIO_ADDR *BIO_ADDR_dup(const BIO_ADDR *ap);
 int BIO_ADDR_rawmake(BIO_ADDR *ap, int family,
                      const void *where, size_t wherelen, unsigned short port);
index e4265b4cef0b66c4f76ac98f279d7f53d46741cf..6af1fe1707b9a349745f8a1fc5789b80ef93347b 100644 (file)
@@ -5535,3 +5535,4 @@ OSSL_ERR_STATE_save_to_mark             ? 3_2_0   EXIST::FUNCTION:
 X509_STORE_CTX_set_get_crl              ?      3_2_0   EXIST::FUNCTION:
 X509_STORE_CTX_set_current_reasons      ?      3_2_0   EXIST::FUNCTION:
 OSSL_STORE_delete                       ?      3_2_0   EXIST::FUNCTION:
+BIO_ADDR_copy                           ?      3_2_0   EXIST::FUNCTION:SOCK