From patchwork Sat Jul 1 02:54:00 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steffan Karger X-Patchwork-Id: 102 Return-Path: Delivered-To: patchwork@openvpn.net Delivered-To: patchwork@openvpn.net Received: from director2.mail.ord1d.rsapps.net ([172.28.255.1]) by backend31.mail.ord1d.rsapps.net (Dovecot) with LMTP id 4wROCRzTH1r+PAAAgoeIoA for ; Thu, 30 Nov 2017 04:45:00 -0500 Received: from director8.mail.ord1c.rsapps.net ([172.28.255.1]) by director2.mail.ord1d.rsapps.net (Dovecot) with LMTP id O+tBCRzTH1oDXAAAgYhSiA ; Thu, 30 Nov 2017 04:45:00 -0500 Received: from smtp41.gate.ord1c ([172.28.255.1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) by director8.mail.ord1c.rsapps.net (Dovecot) with LMTP id 6ns5CBzTH1oXBwAAPBwpBw ; Thu, 30 Nov 2017 04:45:00 -0500 X-Spam-Threshold: 95 X-Spam-Score: 0 X-Spam-Flag: NO Authentication-Results: smtp41.gate.ord1c.rsapps.net x-tls.subject="/OU=GT58646928/OU=See www.geotrust.com/resources/cps (c)14/OU=Domain Control Validated - QuickSSL(R) Premium/CN=ns2.fox-it.com"; auth=pass (cipher=DHE-RSA-AES256-SHA) X-Virus-Scanned: OK X-Orig-To: patchwork@openvpn.net X-Originating-Ip: [178.250.144.131] Authentication-Results: smtp41.gate.ord1c.rsapps.net; iprev=pass policy.iprev="178.250.144.131"; spf=pass smtp.mailfrom="steffan.karger@fox-it.com" smtp.helo="ns2.fox-it.com"; dkim=none (message not signed) header.d=none; dmarc=none (p=nil; dis=none) header.from=fox-it.com X-Classification-ID: 20f4de30-d5b3-11e7-9622-b8ca3a5bd12c-1-1 Received: from [178.250.144.131] ([178.250.144.131:2598] helo=ns2.fox-it.com) by smtp41.gate.ord1c.rsapps.net (envelope-from ) (ecelerity 4.2.1.56364 r(Core:4.2.1.14)) with ESMTPS (cipher=DHE-RSA-AES256-SHA subject="/OU=GT58646928/OU=See www.geotrust.com/resources/cps (c)14/OU=Domain Control Validated - QuickSSL(R) Premium/CN=ns2.fox-it.com") id F5/DE-27263-B13DF1A5; Thu, 30 Nov 2017 04:44:59 -0500 Received: from FOXDFT52.FOX.local (unknown [10.0.0.129]) by ns2.fox-it.com (Postfix) with ESMTPS id 71DBC1C50A5 for ; Thu, 30 Nov 2017 10:44:58 +0100 (CET) Received: from steffan-fox (172.16.5.186) by FOXDFT52.FOX.local (10.0.0.129) with Microsoft SMTP Server (TLS) id 15.0.1293.2; Thu, 30 Nov 2017 10:44:58 +0100 Resent-From: Steffan Karger Resent-Date: Thu, 30 Nov 2017 10:44:56 +0100 Resent-Message-ID: <20171130094456.GE5754@steffan-fox> Resent-To: Received: from FOXDFT52.FOX.local (10.0.0.129) by FOXDFT52.FOX.local (10.0.0.129) with Microsoft SMTP Server (TLS) id 15.0.1293.2 via Mailbox Transport; Sat, 1 Jul 2017 14:54:13 +0200 Received: from steffan-fox.fox.local (172.16.5.170) by FOXDFT52.FOX.local (10.0.0.129) with Microsoft SMTP Server (TLS) id 15.0.1293.2; Sat, 1 Jul 2017 14:54:12 +0200 From: Steffan Karger To: CC: Steffan Karger Subject: [PATCH 3/5] buffer_list_aggregate_separator(): don't exceed max_len Date: Sat, 1 Jul 2017 14:54:00 +0200 Message-ID: <1498913642-32459-3-git-send-email-steffan.karger@fox-it.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1498913642-32459-1-git-send-email-steffan.karger@fox-it.com> References: <1498913642-32459-1-git-send-email-steffan.karger@fox-it.com> X-ClientProxiedBy: FOXDFT52.FOX.local (10.0.0.129) To FOXDFT52.FOX.local (10.0.0.129) MIME-Version: 1.0 X-ClientProxiedBy: FOXDFT52.FOX.local (10.0.0.129) To FOXDFT52.FOX.local (10.0.0.129) X-getmail-retrieved-from-mailbox: Inbox 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 --- src/openvpn/buffer.c | 17 +++++++++++++---- src/openvpn/buffer.h | 2 +- tests/unit_tests/openvpn/test_buffer.c | 12 +++++++----- 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/src/openvpn/buffer.c b/src/openvpn/buffer.c index 1cc2957..2700ad1 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,10 +1241,18 @@ 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; - more = more->next; + size_t extra_len = BLEN(&more->buf) + sep_len; + if (size + extra_len <= max_len) + { + size += extra_len; + more = more->next; + } + else + { + break; + } } if (count >= 2) diff --git a/src/openvpn/buffer.h b/src/openvpn/buffer.h index 8bc4428..019e606 100644 --- a/src/openvpn/buffer.h +++ b/src/openvpn/buffer.h @@ -1102,7 +1102,7 @@ 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 6842114..8b2575f 100644 --- a/tests/unit_tests/openvpn/test_buffer.c +++ b/tests/unit_tests/openvpn/test_buffer.c @@ -131,14 +131,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_string_equal((const char *)buf->data, - teststr1 testsep teststr2 testsep); + assert_string_equal((const char *)buf->data, expected); } static void