From a18c9f80916134bd7122cc1ba204bb5cdca752a3 Mon Sep 17 00:00:00 2001 From: Matt Caswell Date: Thu, 21 Sep 2023 11:59:58 +0100 Subject: [PATCH] Implement a public BIO_ADDR_copy() function 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 Reviewed-by: Tim Hudson Reviewed-by: Paul Dale (Merged from https://github.com/openssl/openssl/pull/22164) --- crypto/bio/bio_addr.c | 19 +++++++++++++++++-- include/openssl/bio.h.in | 1 + util/libcrypto.num | 1 + 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/crypto/bio/bio_addr.c b/crypto/bio/bio_addr.c index aec94237fc8..2a6f6d522c7 100644 --- a/crypto/bio/bio_addr.c +++ b/crypto/bio/bio_addr.c @@ -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; } diff --git a/include/openssl/bio.h.in b/include/openssl/bio.h.in index 8aad1414460..c534dcd76cd 100644 --- a/include/openssl/bio.h.in +++ b/include/openssl/bio.h.in @@ -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); diff --git a/util/libcrypto.num b/util/libcrypto.num index e4265b4cef0..6af1fe1707b 100644 --- a/util/libcrypto.num +++ b/util/libcrypto.num @@ -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 -- 2.47.2