From: Steffan Karger Date: Fri, 29 Dec 2017 09:53:11 +0000 (+0100) Subject: buffer_list_aggregate_separator(): don't exceed max_len X-Git-Tag: v2.4.5~36 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=76fbbd16eb66c8fd40eb35100ffb1b6f443b6a86;p=thirdparty%2Fopenvpn.git 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 Acked-by: Antonio Quartulli 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 (cherry picked from commit fb6138dd32cf01922d7ef670d502148596511268) --- diff --git a/src/openvpn/buffer.c b/src/openvpn/buffer.c index a79ab0318..7d43d07af 100644 --- a/src/openvpn/buffer.c +++ b/src/openvpn/buffer.c @@ -1231,7 +1231,8 @@ buffer_list_peek(struct buffer_list *ol) } void -buffer_list_aggregate_separator(struct buffer_list *bl, const size_t max, const char *sep) +buffer_list_aggregate_separator(struct buffer_list *bl, const size_t max_len, + const char *sep) { int sep_len = strlen(sep); @@ -1240,9 +1241,15 @@ buffer_list_aggregate_separator(struct buffer_list *bl, const size_t max, const struct buffer_entry *more = bl->head; size_t size = 0; int count = 0; - for (count = 0; more && size <= max; ++count) + for (count = 0; more; ++count) { - size += BLEN(&more->buf) + sep_len; + size_t extra_len = BLEN(&more->buf) + sep_len; + if (size + extra_len > max_len) + { + break; + } + + size += extra_len; more = more->next; } diff --git a/src/openvpn/buffer.h b/src/openvpn/buffer.h index 1ed56316c..a1176e75d 100644 --- a/src/openvpn/buffer.h +++ b/src/openvpn/buffer.h @@ -1102,7 +1102,8 @@ void buffer_list_pop(struct buffer_list *ol); void buffer_list_aggregate(struct buffer_list *bl, const size_t max); -void buffer_list_aggregate_separator(struct buffer_list *bl, const size_t max, const char *sep); +void buffer_list_aggregate_separator(struct buffer_list *bl, + const size_t max_len, const char *sep); struct buffer_list *buffer_list_file(const char *fn, int max_line_len); diff --git a/tests/unit_tests/openvpn/test_buffer.c b/tests/unit_tests/openvpn/test_buffer.c index 4841272cb..743e586d1 100644 --- a/tests/unit_tests/openvpn/test_buffer.c +++ b/tests/unit_tests/openvpn/test_buffer.c @@ -134,13 +134,16 @@ static void test_buffer_list_aggregate_separator_two(void **state) { struct test_buffer_list_aggregate_ctx *ctx = *state; + const char *expected = teststr1 testsep teststr2 testsep; - /* Aggregate the first two elements */ - /* FIXME this exceeds the supplied max */ - buffer_list_aggregate_separator(ctx->one_two_three, 4, testsep); + /* Aggregate the first two elements + * (add 1 to max_len to test if "three" is not sneaked in too) + */ + buffer_list_aggregate_separator(ctx->one_two_three, strlen(expected) + 1, + testsep); assert_int_equal(ctx->one_two_three->size, 2); struct buffer *buf = buffer_list_peek(ctx->one_two_three); - assert_buf_equals_str(buf, teststr1 testsep teststr2 testsep); + assert_buf_equals_str(buf, expected); } static void