From: Timo Sirainen Date: Tue, 31 Oct 2017 16:19:33 +0000 (+0200) Subject: lib-ssl-iostream: ssl_iostream_settings_dup() - rewrite using string offsets array X-Git-Tag: 2.3.0.rc1~541 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=25aa88dd96482cb1a135d3e962b7936500dcaab5;p=thirdparty%2Fdovecot%2Fcore.git lib-ssl-iostream: ssl_iostream_settings_dup() - rewrite using string offsets array This array will be useful for other purposes as well. --- diff --git a/src/lib-ssl-iostream/iostream-ssl.c b/src/lib-ssl-iostream/iostream-ssl.c index 66027565a9..863e196e69 100644 --- a/src/lib-ssl-iostream/iostream-ssl.c +++ b/src/lib-ssl-iostream/iostream-ssl.c @@ -4,6 +4,24 @@ #include "module-dir.h" #include "iostream-ssl-private.h" +#define OFFSET(name) offsetof(struct ssl_iostream_settings, name) +static const size_t ssl_iostream_settings_string_offsets[] = { + OFFSET(protocols), + OFFSET(cipher_list), + OFFSET(curve_list), + OFFSET(ca), + OFFSET(ca_file), + OFFSET(ca_dir), + OFFSET(cert.cert), + OFFSET(cert.key), + OFFSET(cert.key_password), + OFFSET(alt_cert.cert), + OFFSET(alt_cert.key), + OFFSET(alt_cert.key_password), + OFFSET(dh), + OFFSET(cert_username_field), + OFFSET(crypto_device), +}; static bool ssl_module_loaded = FALSE; #ifdef HAVE_SSL @@ -226,23 +244,16 @@ struct ssl_iostream_settings *ssl_iostream_settings_dup(pool_t pool, } void ssl_iostream_settings_init_from(pool_t pool, - struct ssl_iostream_settings *new_set, - const struct ssl_iostream_settings *old_set) -{ - memcpy(new_set, old_set, sizeof(*new_set)); - new_set->protocols = p_strdup(pool, old_set->protocols); - new_set->cipher_list = p_strdup(pool, old_set->cipher_list); - new_set->curve_list = p_strdup(pool, old_set->curve_list); - new_set->ca = p_strdup(pool, old_set->ca); - new_set->ca_file = p_strdup(pool, old_set->ca_file); - new_set->ca_dir = p_strdup(pool, old_set->ca_dir); - new_set->cert.cert = p_strdup(pool, old_set->cert.cert); - new_set->cert.key = p_strdup(pool, old_set->cert.key); - new_set->cert.key_password = p_strdup(pool, old_set->cert.key_password); - new_set->alt_cert.cert = p_strdup(pool, old_set->alt_cert.cert); - new_set->alt_cert.key = p_strdup(pool, old_set->alt_cert.key); - new_set->alt_cert.key_password = p_strdup(pool, old_set->alt_cert.key_password); - new_set->dh = p_strdup(pool, old_set->dh); - new_set->cert_username_field = p_strdup(pool, old_set->cert_username_field); - new_set->crypto_device = p_strdup(pool, old_set->crypto_device); + struct ssl_iostream_settings *dest, + const struct ssl_iostream_settings *src) +{ + unsigned int i; + + *dest = *src; + for (i = 0; i < N_ELEMENTS(ssl_iostream_settings_string_offsets); i++) { + const size_t offset = ssl_iostream_settings_string_offsets[i]; + const char *const *src_str = CONST_PTR_OFFSET(src, offset); + const char **dest_str = PTR_OFFSET(dest, offset); + *dest_str = p_strdup(pool, *src_str); + } } diff --git a/src/lib-ssl-iostream/iostream-ssl.h b/src/lib-ssl-iostream/iostream-ssl.h index 4de49ba197..77a00fe938 100644 --- a/src/lib-ssl-iostream/iostream-ssl.h +++ b/src/lib-ssl-iostream/iostream-ssl.h @@ -11,6 +11,8 @@ struct ssl_iostream_cert { }; struct ssl_iostream_settings { + /* NOTE: when updating, remember to update: + ssl_iostream_settings_string_offsets[] */ const char *protocols; /* both */ const char *cipher_list; /* both */ const char *curve_list; /* both */ @@ -95,7 +97,7 @@ void ssl_iostream_context_deinit(struct ssl_iostream_context **ctx); struct ssl_iostream_settings *ssl_iostream_settings_dup(pool_t pool, const struct ssl_iostream_settings *old_set); void ssl_iostream_settings_init_from(pool_t pool, - struct ssl_iostream_settings *new_set, - const struct ssl_iostream_settings *old_set); + struct ssl_iostream_settings *dest, + const struct ssl_iostream_settings *src); #endif