[Openvpn-devel,5/5,v2] buffer_list_aggregate_separator(): simplify code

Message ID 1514541271-19597-1-git-send-email-steffan.karger@fox-it.com
State Accepted
Headers show
Series None | expand

Commit Message

Steffan Karger Dec. 28, 2017, 10:54 p.m. UTC
Clean up the function by slightly simplifying the logic.

Mostly witespace changes, so best reviewed using 'git diff -w'.

Signed-off-by: Steffan Karger <steffan.karger@fox-it.com>
---
v2: rebase on new version of preceding patches

 src/openvpn/buffer.c | 71 ++++++++++++++++++++++++----------------------------
 1 file changed, 33 insertions(+), 38 deletions(-)

Comments

Antonio Quartulli Oct. 9, 2018, 9:27 p.m. UTC | #1
Hi,

On 29/12/17 17:54, Steffan Karger wrote:
> Clean up the function by slightly simplifying the logic.
> 
> Mostly witespace changes, so best reviewed using 'git diff -w'.
          ^ missing 'h' here

> 
> Signed-off-by: Steffan Karger <steffan.karger@fox-it.com>
> ---
> v2: rebase on new version of preceding patches
> 

Other than the typ0 above, the patch does what it says and does not
introduce any functional change.

Even though it was "easier" to review with "git show -w", I still had to
dig into the original function to make sure I understood the original
logic and I was later able to guarantee it was not changed by the patch.

For this reason I wrote some doc for the buffer_list_* functions which I
will send as a new patch soon.

Acked-by: Antonio Quartulli <a@unstable.cc>
David Sommerseth Oct. 16, 2018, 9:35 a.m. UTC | #2
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512

Commit message type fixed during commit.

Your patch has been applied to the following branches

commit e883c66b9da390f56ef4a596c9eb6b237a185a50  (master)
commit be47e4292459aabdf89e53d9f5747b113fc2cd5d  (release/2.4)
Author: Steffan Karger
Date:   Fri Dec 29 10:54:31 2017 +0100

     buffer_list_aggregate_separator(): simplify code

     Signed-off-by: Steffan Karger <steffan.karger@fox-it.com>
     Acked-by: Antonio Quartulli <a@unstable.cc>
     Message-Id: <1514541271-19597-1-git-send-email-steffan.karger@fox-it.com>
     URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg16105.html
     Signed-off-by: David Sommerseth <davids@openvpn.net>


- --
kind regards,

David Sommerseth

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.22 (GNU/Linux)

iQIcBAEBCgAGBQJbxkuoAAoJEIbPlEyWcf3ypgoQAIpeS0Ick75HV61SgxyM5PCb
v3EbcoHCV+ihWKVRg+Q4RhkPz7VTzoMVfPJEj0seZ4RFOJni8XLZLSb62MbWhTqv
qFVvYiGHXxBeAYYlcN9qR3QX1efs4kpWMip9AFDmvVzBIiInr1i6nDA5C5obwmnH
2Z8M3lPZLZweAtcL9dHxsSYY+6k/+k5ITmTyrGbPqfK8wniI9ESLdTq5Ga/LD7yx
dOFBL0pw83d6umCVFX2LWtq9Hmdhj/sc1IYtpRFIiDdzYZX2KVqrCDsnOSlaJsnP
6a6oQqy47MUJmmX9XrG7R/q/IzGuj5TA6c6I2IpZU/4ojEiYXhKs8YD7D4sLAA1w
OElKQHPwCt/YnRXl+cnqs2QJjMaVTXS/X4wg5BPbehJrZvovwK6VsbvExPpJQ/fu
qRbCzJqGZ4S3rJjBL3GjVYs50mmgtSJa5tLHzUuDdSYI7FMdkZ9FwH3Spmive39g
2lQ/LQrPqTtg/yOl1wGigKEYKyiIPzY3TorIpokgmZWXI+4t1rRH/0fw4ZfSrM21
hFCKNdFY8blrbUOyryWnWhR30KV/PsFwmc6W7Ht1EfvNWonSzBp9PxPg3ijBz6YQ
c6UfkVBteLiVTB5k0jRxgNY7yoJ6B4VodIc9CC7+VBxR4oy/pp14PaX27Oy3/jZM
u2XA4B8wbVcuyj8Ytre3
=S2oM
-----END PGP SIGNATURE-----

Patch

diff --git a/src/openvpn/buffer.c b/src/openvpn/buffer.c
index cfe6f2c..858bb08 100644
--- a/src/openvpn/buffer.c
+++ b/src/openvpn/buffer.c
@@ -1226,49 +1226,44 @@  void
 buffer_list_aggregate_separator(struct buffer_list *bl, const size_t max_len,
                                 const char *sep)
 {
-    int sep_len = strlen(sep);
+    const int sep_len = strlen(sep);
+    struct buffer_entry *more = bl->head;
+    size_t size = 0;
+    int count = 0;
+    for (count = 0; more; ++count)
+    {
+        size_t extra_len = BLEN(&more->buf) + sep_len;
+        if (size + extra_len > max_len)
+        {
+            break;
+        }
+
+        size += extra_len;
+        more = more->next;
+    }
 
-    if (bl->head)
+    if (count >= 2)
     {
-        struct buffer_entry *more = bl->head;
-        size_t size = 0;
-        int count = 0;
-        for (count = 0; more; ++count)
-        {
-            size_t extra_len = BLEN(&more->buf) + sep_len;
-            if (size + extra_len > max_len)
-            {
-                break;
-            }
+        struct buffer_entry *f;
+        ALLOC_OBJ_CLEAR(f, struct buffer_entry);
+        f->buf = alloc_buf(size + 1); /* prevent 0-byte malloc */
 
-            size += extra_len;
-            more = more->next;
+        struct buffer_entry *e = bl->head;
+        for (size_t i = 0; e && i < count; ++i)
+        {
+            struct buffer_entry *next = e->next;
+            buf_copy(&f->buf, &e->buf);
+            buf_write(&f->buf, sep, sep_len);
+            free_buf(&e->buf);
+            free(e);
+            e = next;
         }
-
-        if (count >= 2)
+        bl->head = f;
+        bl->size -= count - 1;
+        f->next = more;
+        if (!more)
         {
-            int i;
-            struct buffer_entry *e = bl->head, *f;
-
-            ALLOC_OBJ_CLEAR(f, struct buffer_entry);
-            f->buf = alloc_buf(size + 1); /* prevent 0-byte malloc */
-            f->buf.capacity = size;
-            for (i = 0; e && i < count; ++i)
-            {
-                struct buffer_entry *next = e->next;
-                buf_copy(&f->buf, &e->buf);
-                buf_write(&f->buf, sep, sep_len);
-                free_buf(&e->buf);
-                free(e);
-                e = next;
-            }
-            bl->head = f;
-            bl->size -= count - 1;
-            f->next = more;
-            if (!more)
-            {
-                bl->tail = f;
-            }
+            bl->tail = f;
         }
     }
 }