[Openvpn-devel,3/5] Align reliable_free with other free methods to accept NULL

Message ID 20201023113431.26691-3-arne@rfc2549.org
State Accepted
Headers show
Series
  • [Openvpn-devel,1/5] Inline function tls_get_peer_info
Related show

Commit Message

Arne Schwabe Oct. 23, 2020, 11:34 a.m.
The semantic of most free methods is to free a pointer and all its
contents and also free the pointer itself. Align reliable_free to this
semantic.

Also clean up the other free uses in key_state_free.

Signed-off-by: Arne Schwabe <arne@rfc2549.org>
---
 src/openvpn/reliable.c |  5 +++++
 src/openvpn/reliable.h |  4 +++-
 src/openvpn/ssl.c      | 24 ++++--------------------
 3 files changed, 12 insertions(+), 21 deletions(-)

Comments

Gert Doering Oct. 24, 2020, 7:47 p.m. | #1
Acked-by: Gert Doering <gert@greenie.muc.de>

Change brings this more in line with other code parts, so easier
to understand.

Your patch has been applied to the master branch.

commit 2c8a9877617727438cdd874ecd38c04adebf53ad
Author: Arne Schwabe
Date:   Fri Oct 23 13:34:29 2020 +0200

     Align reliable_free with other free methods to accept NULL

     Signed-off-by: Arne Schwabe <arne@rfc2549.org>
     Acked-by: Gert Doering <gert@greenie.muc.de>
     Message-Id: <20201023113431.26691-3-arne@rfc2549.org>
     URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg21215.html
     Signed-off-by: Gert Doering <gert@greenie.muc.de>


--
kind regards,

Gert Doering

Patch

diff --git a/src/openvpn/reliable.c b/src/openvpn/reliable.c
index eae1e0cb..6c1f2da1 100644
--- a/src/openvpn/reliable.c
+++ b/src/openvpn/reliable.c
@@ -326,12 +326,17 @@  reliable_init(struct reliable *rel, int buf_size, int offset, int array_size, bo
 void
 reliable_free(struct reliable *rel)
 {
+    if (!rel)
+    {
+        return;
+    }
     int i;
     for (i = 0; i < rel->size; ++i)
     {
         struct reliable_entry *e = &rel->array[i];
         free_buf(&e->buf);
     }
+    free(rel);
 }
 
 /* no active buffers? */
diff --git a/src/openvpn/reliable.h b/src/openvpn/reliable.h
index 688c65c8..a84d4290 100644
--- a/src/openvpn/reliable.h
+++ b/src/openvpn/reliable.h
@@ -192,7 +192,9 @@  bool reliable_ack_write(struct reliable_ack *ack,
 void reliable_init(struct reliable *rel, int buf_size, int offset, int array_size, bool hold);
 
 /**
- * Free allocated memory associated with a reliable structure.
+ * Free allocated memory associated with a reliable structure and the pointer
+ * itself.
+ * Does nothing if rel is NULL.
  *
  * @param rel The reliable structured to clean up.
  */
diff --git a/src/openvpn/ssl.c b/src/openvpn/ssl.c
index 87b51d96..7a3eb146 100644
--- a/src/openvpn/ssl.c
+++ b/src/openvpn/ssl.c
@@ -969,27 +969,11 @@  key_state_free(struct key_state *ks, bool clear)
     free_buf(&ks->ack_write_buf);
     buffer_list_free(ks->paybuf);
 
-    if (ks->send_reliable)
-    {
-        reliable_free(ks->send_reliable);
-        free(ks->send_reliable);
-    }
-
-    if (ks->rec_reliable)
-    {
-        reliable_free(ks->rec_reliable);
-        free(ks->rec_reliable);
-    }
+    reliable_free(ks->send_reliable);
+    reliable_free(ks->rec_reliable);
 
-    if (ks->rec_ack)
-    {
-        free(ks->rec_ack);
-    }
-
-    if (ks->key_src)
-    {
-        free(ks->key_src);
-    }
+    free(ks->rec_ack);
+    free(ks->key_src);
 
     packet_id_free(&ks->crypto_options.packet_id);