Browse code

buffer_list_aggregate_separator(): don't exceed max_len

buffer_list_aggregate_separator() would merge buffer_list entries until it
had exceeded the provided max_len, instead of stopping *before* exceeding
the max value.

Signed-off-by: Steffan Karger <steffan.karger@fox-it.com>
Acked-by: Antonio Quartulli <antonio@openvpn.net>
Message-Id: <1514541191-19471-1-git-send-email-steffan.karger@fox-it.com>
URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg16104.html
Signed-off-by: Gert Doering <gert@greenie.muc.de>

Steffan Karger authored on 2017/12/29 18:53:11
Showing 3 changed files
... ...
@@ -1223,7 +1223,8 @@ buffer_list_peek(struct buffer_list *ol)
1223 1223
 }
1224 1224
 
1225 1225
 void
1226
-buffer_list_aggregate_separator(struct buffer_list *bl, const size_t max, const char *sep)
1226
+buffer_list_aggregate_separator(struct buffer_list *bl, const size_t max_len,
1227
+                                const char *sep)
1227 1228
 {
1228 1229
     int sep_len = strlen(sep);
1229 1230
 
... ...
@@ -1232,9 +1233,15 @@ buffer_list_aggregate_separator(struct buffer_list *bl, const size_t max, const
1232 1232
         struct buffer_entry *more = bl->head;
1233 1233
         size_t size = 0;
1234 1234
         int count = 0;
1235
-        for (count = 0; more && size <= max; ++count)
1235
+        for (count = 0; more; ++count)
1236 1236
         {
1237
-            size += BLEN(&more->buf) + sep_len;
1237
+            size_t extra_len = BLEN(&more->buf) + sep_len;
1238
+            if (size + extra_len > max_len)
1239
+            {
1240
+                break;
1241
+            }
1242
+
1243
+            size += extra_len;
1238 1244
             more = more->next;
1239 1245
         }
1240 1246
 
... ...
@@ -1107,7 +1107,8 @@ void buffer_list_pop(struct buffer_list *ol);
1107 1107
 
1108 1108
 void buffer_list_aggregate(struct buffer_list *bl, const size_t max);
1109 1109
 
1110
-void buffer_list_aggregate_separator(struct buffer_list *bl, const size_t max, const char *sep);
1110
+void buffer_list_aggregate_separator(struct buffer_list *bl,
1111
+                                     const size_t max_len, const char *sep);
1111 1112
 
1112 1113
 struct buffer_list *buffer_list_file(const char *fn, int max_line_len);
1113 1114
 
... ...
@@ -134,13 +134,16 @@ static void
134 134
 test_buffer_list_aggregate_separator_two(void **state)
135 135
 {
136 136
     struct test_buffer_list_aggregate_ctx *ctx = *state;
137
+    const char *expected = teststr1 testsep teststr2 testsep;
137 138
 
138
-    /* Aggregate the first two elements */
139
-    /* FIXME this exceeds the supplied max */
140
-    buffer_list_aggregate_separator(ctx->one_two_three, 4, testsep);
139
+    /* Aggregate the first two elements
140
+     * (add 1 to max_len to test if "three" is not sneaked in too)
141
+     */
142
+    buffer_list_aggregate_separator(ctx->one_two_three, strlen(expected) + 1,
143
+                                    testsep);
141 144
     assert_int_equal(ctx->one_two_three->size, 2);
142 145
     struct buffer *buf = buffer_list_peek(ctx->one_two_three);
143
-    assert_buf_equals_str(buf, teststr1 testsep teststr2 testsep);
146
+    assert_buf_equals_str(buf, expected);
144 147
 }
145 148
 
146 149
 static void