Browse code

buffer_list_aggregate_separator(): simplify code

Clean up the function by slightly simplifying the logic.

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

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>

Steffan Karger authored on 2017/12/29 18:54:31
Showing 1 changed files
... ...
@@ -1271,49 +1271,44 @@ void
1271 1271
 buffer_list_aggregate_separator(struct buffer_list *bl, const size_t max_len,
1272 1272
                                 const char *sep)
1273 1273
 {
1274
-    int sep_len = strlen(sep);
1274
+    const int sep_len = strlen(sep);
1275
+    struct buffer_entry *more = bl->head;
1276
+    size_t size = 0;
1277
+    int count = 0;
1278
+    for (count = 0; more; ++count)
1279
+    {
1280
+        size_t extra_len = BLEN(&more->buf) + sep_len;
1281
+        if (size + extra_len > max_len)
1282
+        {
1283
+            break;
1284
+        }
1285
+
1286
+        size += extra_len;
1287
+        more = more->next;
1288
+    }
1275 1289
 
1276
-    if (bl->head)
1290
+    if (count >= 2)
1277 1291
     {
1278
-        struct buffer_entry *more = bl->head;
1279
-        size_t size = 0;
1280
-        int count = 0;
1281
-        for (count = 0; more; ++count)
1282
-        {
1283
-            size_t extra_len = BLEN(&more->buf) + sep_len;
1284
-            if (size + extra_len > max_len)
1285
-            {
1286
-                break;
1287
-            }
1292
+        struct buffer_entry *f;
1293
+        ALLOC_OBJ_CLEAR(f, struct buffer_entry);
1294
+        f->buf = alloc_buf(size + 1); /* prevent 0-byte malloc */
1288 1295
 
1289
-            size += extra_len;
1290
-            more = more->next;
1296
+        struct buffer_entry *e = bl->head;
1297
+        for (size_t i = 0; e && i < count; ++i)
1298
+        {
1299
+            struct buffer_entry *next = e->next;
1300
+            buf_copy(&f->buf, &e->buf);
1301
+            buf_write(&f->buf, sep, sep_len);
1302
+            free_buf(&e->buf);
1303
+            free(e);
1304
+            e = next;
1291 1305
         }
1292
-
1293
-        if (count >= 2)
1306
+        bl->head = f;
1307
+        bl->size -= count - 1;
1308
+        f->next = more;
1309
+        if (!more)
1294 1310
         {
1295
-            int i;
1296
-            struct buffer_entry *e = bl->head, *f;
1297
-
1298
-            ALLOC_OBJ_CLEAR(f, struct buffer_entry);
1299
-            f->buf = alloc_buf(size + 1); /* prevent 0-byte malloc */
1300
-            f->buf.capacity = size;
1301
-            for (i = 0; e && i < count; ++i)
1302
-            {
1303
-                struct buffer_entry *next = e->next;
1304
-                buf_copy(&f->buf, &e->buf);
1305
-                buf_write(&f->buf, sep, sep_len);
1306
-                free_buf(&e->buf);
1307
-                free(e);
1308
-                e = next;
1309
-            }
1310
-            bl->head = f;
1311
-            bl->size -= count - 1;
1312
-            f->next = more;
1313
-            if (!more)
1314
-            {
1315
-                bl->tail = f;
1316
-            }
1311
+            bl->tail = f;
1317 1312
         }
1318 1313
     }
1319 1314
 }