From patchwork Thu Nov 9 03:02:13 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steffan Karger X-Patchwork-Id: 54 Return-Path: Delivered-To: patchwork@openvpn.net Delivered-To: patchwork@openvpn.net Received: from director2.mail.ord1d.rsapps.net ([172.30.191.6]) by backend31.mail.ord1d.rsapps.net (Dovecot) with LMTP id pZCuCC9gBFpRGAAAgoeIoA for ; Thu, 09 Nov 2017 09:03:27 -0500 Received: from proxy16.mail.ord1d.rsapps.net ([172.30.191.6]) by director2.mail.ord1d.rsapps.net (Dovecot) with LMTP id Y10YDy9gBFoTTQAAgYhSiA ; Thu, 09 Nov 2017 09:03:27 -0500 Received: from smtp46.gate.ord1c ([172.30.191.6]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) by proxy16.mail.ord1d.rsapps.net (Dovecot) with LMTP id DMd4CC9gBFoSFwAAetu3IA ; Thu, 09 Nov 2017 09:03:27 -0500 X-Spam-Threshold: 95 X-Spam-Score: 0 X-Spam-Flag: NO X-Virus-Scanned: OK X-Orig-To: openvpnslackdevel@openvpn.net X-Originating-Ip: [216.34.181.88] Authentication-Results: smtp46.gate.ord1c.rsapps.net; iprev=pass policy.iprev="216.34.181.88"; spf=pass smtp.mailfrom="openvpn-devel-bounces@lists.sourceforge.net" smtp.helo="lists.sourceforge.net"; dkim=fail (signature verification failed) header.d=sourceforge.net; dkim=fail (signature verification failed) header.d=sf.net; dmarc=none (p=nil; dis=none) header.from=fox-it.com X-Classification-ID: c1c311aa-c556-11e7-9e0a-bc305bf48bb4-1-1 Received: from [216.34.181.88] ([216.34.181.88:60204] helo=lists.sourceforge.net) by smtp46.gate.ord1c.rsapps.net (envelope-from ) (ecelerity 4.2.1.56364 r(Core:4.2.1.14)) with ESMTPS (cipher=DHE-RSA-AES256-GCM-SHA384) id 1E/BC-21687-E20640A5; Thu, 09 Nov 2017 09:03:26 -0500 Received: from localhost ([127.0.0.1] helo=sfs-ml-2.v29.ch3.sourceforge.com) by sfs-ml-2.v29.ch3.sourceforge.com with esmtp (Exim 4.89) (envelope-from ) id 1eCnPo-0004RL-Em; Thu, 09 Nov 2017 14:02:44 +0000 Received: from sfi-mx-2.v28.ch3.sourceforge.com ([172.29.28.192] helo=mx.sourceforge.net) by sfs-ml-2.v29.ch3.sourceforge.com with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.89) (envelope-from ) id 1eCnPn-0004RF-92 for openvpn-devel@lists.sourceforge.net; Thu, 09 Nov 2017 14:02:43 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sourceforge.net; s=x; h=Content-Type:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:CC:To:From:Sender:Reply-To:Content-Transfer-Encoding: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=FyiNkg1FUvlqIfyj9AH+GhULx2e/x1gpTsWzB+ZwfNI=; b=aehsmEbNwSuCCdzMJH3pPO/o1O N1rvRwCYYjidnpdxVfXQ0yMNQHyTp75DJY6Ou7FBNabBTg3vdpvrtkRMZFOajCnmalXd35ZvHBBAZ +3D9ZvXIwlCMBeivvOvVLB2p6A81Ypvckat5rzcKhNtbzn70WHeTMCG4CrRh15ZtgAf8=; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sf.net; s=x ; h=Content-Type:MIME-Version:References:In-Reply-To:Message-ID:Date:Subject: CC:To:From:Sender:Reply-To:Content-Transfer-Encoding:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=FyiNkg1FUvlqIfyj9AH+GhULx2e/x1gpTsWzB+ZwfNI=; b=TzYPUcliZoJ9OqVBcByy2ETUlT WBu4CF+uxTCW/C3YnIq3O7ilNRLEgSLcOubZPPoFZY/l6EkBfIAryFWvk9E4SY/E09Caac+UkYqaQ JtsuylZt8FhVTlNgLQaq2qWs907ZDL+A4d6DcWjy42lTupj5A+nS29WYDgsGj7Z4VX24=; Received: from ns2.fox-it.com ([178.250.144.131]) by sfi-mx-2.v28.ch3.sourceforge.com with esmtps (TLSv1:ECDHE-RSA-AES256-SHA:256) (Exim 4.89) id 1eCnPg-0001r3-Qm for openvpn-devel@lists.sourceforge.net; Thu, 09 Nov 2017 14:02:43 +0000 Received: from FOXDFT52.FOX.local (unknown [10.0.0.129]) by ns2.fox-it.com (Postfix) with ESMTPS id 9D7C71C4877 for ; Thu, 9 Nov 2017 15:02:30 +0100 (CET) Received: from steffan-fox.fox.local (10.0.3.167) by FOXDFT52.FOX.local (10.0.0.129) with Microsoft SMTP Server (TLS) id 15.0.1293.2; Thu, 9 Nov 2017 15:02:30 +0100 From: Steffan Karger To: Date: Thu, 9 Nov 2017 15:02:13 +0100 Message-ID: <1510236133-6926-1-git-send-email-steffan.karger@fox-it.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1498913642-32459-3-git-send-email-steffan.karger@fox-it.com> References: <1498913642-32459-3-git-send-email-steffan.karger@fox-it.com> MIME-Version: 1.0 X-ClientProxiedBy: FOXDFT52.FOX.local (10.0.0.129) To FOXDFT52.FOX.local (10.0.0.129) X-Spam-Report: Spam Filtering performed by mx.sourceforge.net. See http://spamassassin.org/tag/ for more details. -0.0 SPF_PASS SPF: sender matches SPF record -0.0 RP_MATCHES_RCVD Envelope sender domain matches handover relay domain 0.3 AWL AWL: Adjusted score from AWL reputation of From: address X-Headers-End: 1eCnPg-0001r3-Qm Subject: [Openvpn-devel] [PATCH 3/5 v2] buffer_list_aggregate_separator(): don't exceed max_len X-BeenThere: openvpn-devel@lists.sourceforge.net X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: openvpn-devel-bounces@lists.sourceforge.net 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 Acked-by: Antonio Quartulli --- v2: rebase on 1/5 v2 (other patches should still apply) src/openvpn/buffer.c | 17 +++++++++++++---- src/openvpn/buffer.h | 2 +- tests/unit_tests/openvpn/test_buffer.c | 11 +++++++---- 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/openvpn/buffer.c b/src/openvpn/buffer.c index 583c5c7..f72d525 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 1ed5631..499c75e 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 f5830a1..99e6530 100644 --- a/tests/unit_tests/openvpn/test_buffer.c +++ b/tests/unit_tests/openvpn/test_buffer.c @@ -135,13 +135,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