We (and OpenSSL) already use calling free on null pointers in a number
of places and also C99 standards says free(NULL) does nothing.
The if (x) free(x) calls more often make code harder to read, instead
of easier, remove these NULL checks in favour of directly calling
free(x).
The OpenSSL *_free methods are also safe to call with NULL and
pkcs11h_certificate_freeCertificateIdList is also safe to be called with
NULL.
Signed-off-by: Arne Schwabe <arne@rfc2549.org>
Acked-by: Gert Doering <gert@greenie.muc.de>
Message-Id: <
20201023113431.26691-5-arne@rfc2549.org>
URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg21216.html
Signed-off-by: Gert Doering <gert@greenie.muc.de>
return OPENVPN_PLUGIN_FUNC_SUCCESS;
error:
- if (context)
- {
- free(context);
- }
+ free(context);
return OPENVPN_PLUGIN_FUNC_ERROR;
}
void
free_buf(struct buffer *buf)
{
- if (buf->data)
- {
- free(buf->data);
- }
+ free(buf->data);
CLEAR(*buf);
}
{
closelog();
use_syslog = false;
- if (pgmname_syslog)
- {
- free(pgmname_syslog);
- pgmname_syslog = NULL;
- }
+ free(pgmname_syslog);
+ pgmname_syslog = NULL;
}
#endif
}
}
/* free options compatibility strings */
- if (c->c2.options_string_local)
- {
- free(c->c2.options_string_local);
- }
- if (c->c2.options_string_remote)
- {
- free(c->c2.options_string_remote);
- }
+ free(c->c2.options_string_local);
+ free(c->c2.options_string_remote);
+
c->c2.options_string_local = c->c2.options_string_remote = NULL;
if (c->c2.pulled_options_state)
msg(M_CLIENT, ">PKCS11ID-ENTRY:'%d'", index);
}
- if (id != NULL)
- {
- free(id);
- }
- if (base64 != NULL)
- {
- free(base64);
- }
+ free(id);
+ free(base64);
}
#endif /* ifdef ENABLE_PKCS11 */
{
struct man_connection *mc = &man->connection;
- if (mc->es)
- {
- event_free(mc->es);
- }
+ event_free(mc->es);
#ifdef _WIN32
net_event_win32_close(&mc->ne32);
#endif
{
man_close_socket(man, mc->sd_cli);
}
- if (mc->in)
- {
- command_line_free(mc->in);
- }
- if (mc->out)
- {
- buffer_list_free(mc->out);
- }
+
+ command_line_free(mc->in);
+ buffer_list_free(mc->out);
+
in_extra_reset(&man->connection, IER_RESET);
buffer_list_free(mc->ext_key_input);
man_connection_clear(mc);
void
command_line_free(struct command_line *cl)
{
+ if (!cl)
+ {
+ return;
+ }
command_line_reset(cl);
free_buf(&cl->buf);
free_buf(&cl->residual);
static void
log_entry_free_contents(struct log_entry *e)
{
- if (e->string)
- {
- free((char *)e->string);
- }
+ /* Cast away constness of const char* */
+ free((char *)e->string);
CLEAR(*e);
}
if (mtcp)
{
event_free(mtcp->es);
- if (mtcp->esr)
- {
- free(mtcp->esr);
- }
+ free(mtcp->esr);
free(mtcp);
}
}
static void
set_cc_config(struct multi_instance *mi, struct buffer_list *cc_config)
{
- if (mi->cc_config)
- {
- buffer_list_free(mi->cc_config);
- }
+ buffer_list_free(mi->cc_config);
mi->cc_config = cc_config;
}
#endif
ret = pf_load_from_buffer_list(&mi->context, pf_config);
}
- if (pf_config)
- {
- buffer_list_free(pf_config);
- }
+ buffer_list_free(pf_config);
return ret;
}
#endif /* ifdef MANAGEMENT_PF */
if (p)
{
dmsg(D_PID_DEBUG, "PID packet_id_free");
- if (p->rec.seq_list)
- {
- free(p->rec.seq_list);
- }
+ free(p->rec.seq_list);
CLEAR(*p);
}
}
cleanup:
- if (id_list != NULL)
- {
- pkcs11h_certificate_freeCertificateIdList(id_list);
- id_list = NULL;
- }
+ pkcs11h_certificate_freeCertificateIdList(id_list);
+ id_list = NULL;
dmsg(
D_PKCS11_DEBUG,
cleanup:
- if (id_list != NULL)
- {
- pkcs11h_certificate_freeCertificateIdList(id_list);
- id_list = NULL;
- }
+ pkcs11h_certificate_freeCertificateIdList(id_list);
+ id_list = NULL;
- if (internal_id != NULL)
- {
- free(internal_id);
- internal_id = NULL;
- }
+ free(internal_id);
+ internal_id = NULL;
- if (internal_base64 != NULL)
- {
- free(internal_base64);
- internal_base64 = NULL;
- }
+ free(internal_base64);
+ internal_base64 = NULL;
- if (certificate_blob != NULL)
- {
- free(certificate_blob);
- certificate_blob = NULL;
- }
+ free(certificate_blob);
+ certificate_blob = NULL;
dmsg(
D_PKCS11_DEBUG,
certificate = NULL;
}
- if (ser != NULL)
- {
- free(ser);
- ser = NULL;
- }
+ free(ser);
+ ser = NULL;
}
cleanup:
- if (user_certificates != NULL)
- {
- pkcs11h_certificate_freeCertificateIdList(user_certificates);
- user_certificates = NULL;
- }
+ pkcs11h_certificate_freeCertificateIdList(user_certificates);
+ user_certificates = NULL;
pkcs11h_terminate();
gc_free(&gc);
* openssl objects have reference
* count, so release them
*/
- if (x509 != NULL)
- {
- X509_free(x509);
- x509 = NULL;
- }
+ X509_free(x509);
+ x509 = NULL;
- if (evp != NULL)
- {
- EVP_PKEY_free(evp);
- evp = NULL;
- }
+ EVP_PKEY_free(evp);
+ evp = NULL;
if (openssl_session != NULL)
{
dn = x509_get_subject(x509, gc);
cleanup:
- if (x509 != NULL)
- {
- X509_free(x509);
- x509 = NULL;
- }
+ X509_free(x509);
+ x509 = NULL;
return dn;
}
ret = 0;
cleanup:
+ X509_free(x509);
+ x509 = NULL;
- if (x509 != NULL)
- {
- X509_free(x509);
- x509 = NULL;
- }
return ret;
}
#endif /* defined(ENABLE_PKCS11) && defined(ENABLE_OPENSSL) */
static void
store_proxy_authenticate(struct http_proxy_info *p, char *data)
{
- if (p->proxy_authenticate)
- {
- free(p->proxy_authenticate);
- }
+ free(p->proxy_authenticate);
p->proxy_authenticate = data;
}
key_state_free(&session->key[i], false);
}
- if (session->common_name)
- {
- free(session->common_name);
- }
+ free(session->common_name);
cert_hash_free(session->cert_hash_set);
auth_set_client_reason(multi, NULL);
free(multi->peer_info);
-
- if (multi->locked_cn)
- {
- free(multi->locked_cn);
- }
-
- if (multi->locked_username)
- {
- free(multi->locked_username);
- }
+ free(multi->locked_cn);
+ free(multi->locked_username);
cert_hash_free(multi->locked_cert_hash_set);
if (ctx)
{
mbedtls_pk_free(ctx->priv_key);
- if (ctx->priv_key)
- {
- free(ctx->priv_key);
- }
+ free(ctx->priv_key);
mbedtls_x509_crt_free(ctx->ca_chain);
- if (ctx->ca_chain)
- {
- free(ctx->ca_chain);
- }
+ free(ctx->ca_chain);
mbedtls_x509_crt_free(ctx->crt_chain);
- if (ctx->crt_chain)
- {
- free(ctx->crt_chain);
- }
+ free(ctx->crt_chain);
mbedtls_dhm_free(ctx->dhm_ctx);
- if (ctx->dhm_ctx)
- {
- free(ctx->dhm_ctx);
- }
+ free(ctx->dhm_ctx);
mbedtls_x509_crl_free(ctx->crl);
- if (ctx->crl)
- {
- free(ctx->crl);
- }
+ free(ctx->crl);
#if defined(ENABLE_PKCS11)
pkcs11h_certificate_freeCertificate(ctx->pkcs11_cert);
#endif
- if (ctx->allowed_ciphers)
- {
- free(ctx->allowed_ciphers);
- }
+ free(ctx->allowed_ciphers);
- if (ctx->groups)
- {
- free(ctx->groups);
- }
+ free(ctx->groups);
CLEAR(*ctx);
ctx->initialised = false;
-
}
}
tls_ctx_free(struct tls_root_ctx *ctx)
{
ASSERT(NULL != ctx);
- if (NULL != ctx->ctx)
- {
- SSL_CTX_free(ctx->ctx);
- }
+ SSL_CTX_free(ctx->ctx);
ctx->ctx = NULL;
}
crypto_print_openssl_errors(M_DEBUG);
}
- if (in != NULL)
- {
- BIO_free(in);
- }
- if (x)
- {
- X509_free(x);
- }
+ BIO_free(in);
+ X509_free(x);
}
int
ret = 0;
end:
- if (pkey)
- {
- EVP_PKEY_free(pkey);
- }
- if (in)
- {
- BIO_free(in);
- }
+ EVP_PKEY_free(pkey);
+ BIO_free(in);
return ret;
}
{
RSA_free(rsa);
}
- else
+ else if (rsa_meth)
{
- if (rsa_meth)
- {
- RSA_meth_free(rsa_meth);
- }
+ RSA_meth_free(rsa_meth);
}
return 0;
}
err:
/* Reach here only when ec and privkey can be independenly freed */
- if (privkey)
- {
- EVP_PKEY_free(privkey);
- }
- if (ec)
- {
- EC_KEY_free(ec);
- }
+ EVP_PKEY_free(privkey);
+ EC_KEY_free(ec);
return 0;
}
#endif /* OPENSSL_VERSION_NUMBER > 1.1.0 dev && !defined(OPENSSL_NO_EC) */
}
}
- if (in)
- {
- BIO_free(in);
- }
+ BIO_free(in);
}
/* Set a store for certs (CA & CRL) with a lookup on the "capath" hash directory */
void
auth_set_client_reason(struct tls_multi *multi, const char *client_reason)
{
- if (multi->client_reason)
- {
- free(multi->client_reason);
- multi->client_reason = NULL;
- }
+ free(multi->client_reason);
+ multi->client_reason = NULL;
+
if (client_reason && strlen(client_reason))
{
multi->client_reason = string_alloc(client_reason, NULL);
subject[subject_mem->length] = '\0';
err:
- if (subject_bio)
- {
- BIO_free(subject_bio);
- }
-
+ BIO_free(subject_bio);
return subject;
}
ret = false;
}
}
- if (so->filename)
- {
- free(so->filename);
- }
+ free(so->filename);
+
if (buf_defined(&so->read_buf))
{
free_buf(&so->read_buf);
{
close(tt->fd);
}
- if (tt->actual_name)
- {
- free(tt->actual_name);
- }
+
+ free(tt->actual_name);
clear_tuntap(tt);
}
#endif /* !_WIN32 */
solaris_close_tun(tt);
- if (tt->actual_name)
- {
- free(tt->actual_name);
- }
+ free(tt->actual_name);
clear_tuntap(tt);
free(tt);
}
}
- if (tt->actual_name)
- {
- free(tt->actual_name);
- }
+ free(tt->actual_name);
if (tt->windows_driver == WINDOWS_DRIVER_WINTUN)
{
}
error:
- if (context)
- {
- free(context);
- }
+ free(context);
return OPENVPN_PLUGIN_FUNC_ERROR;
}
{
if (context)
{
- if (context->command)
- {
- free(context->command);
- }
+ free(context->command);
free(context);
}
}