From patchwork Sat Jul 1 02:54:02 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steffan Karger X-Patchwork-Id: 105 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 S7TGLSDTH1qWKQAAgoeIoA for ; Thu, 30 Nov 2017 04:45:04 -0500 Received: from director4.mail.ord1c.rsapps.net ([172.28.255.1]) by director2.mail.ord1d.rsapps.net (Dovecot) with LMTP id 69xUAiDTH1rAXwAAgYhSiA ; Thu, 30 Nov 2017 04:45:04 -0500 Received: from smtp47.gate.ord1a ([172.28.255.1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) by director4.mail.ord1c.rsapps.net (Dovecot) with LMTP id SC3tCyHTH1rUawAAsEL7Xg ; Thu, 30 Nov 2017 04:45:05 -0500 X-Spam-Threshold: 95 X-Spam-Score: 0 X-Spam-Flag: NO Authentication-Results: smtp47.gate.ord1a.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: smtp47.gate.ord1a.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: 22629294-d5b3-11e7-8682-0022192d87f6-1-1 Received: from [178.250.144.131] ([178.250.144.131:15937] helo=ns2.fox-it.com) by smtp47.gate.ord1a.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 03/DF-19269-E13DF1A5; Thu, 30 Nov 2017 04:45:02 -0500 Received: from FOXDFT52.FOX.local (unknown [10.0.0.129]) by ns2.fox-it.com (Postfix) with ESMTPS id D1A0B1C50A5 for ; Thu, 30 Nov 2017 10:45:00 +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:45:00 +0100 Resent-From: Steffan Karger Resent-Date: Thu, 30 Nov 2017 10:44:59 +0100 Resent-Message-ID: <20171130094459.GG5754@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:13 +0200 From: Steffan Karger To: CC: Steffan Karger Subject: [PATCH 5/5] buffer_list_aggregate_separator(): simplify code Date: Sat, 1 Jul 2017 14:54:02 +0200 Message-ID: <1498913642-32459-5-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 Clean up the function by slightly simplifying the logic. Mostly witespace changes, so best reviewed using 'git diff -w'. Signed-off-by: Steffan Karger --- src/openvpn/buffer.c | 75 ++++++++++++++++++++++++---------------------------- 1 file changed, 35 insertions(+), 40 deletions(-) diff --git a/src/openvpn/buffer.c b/src/openvpn/buffer.c index ecbdc1b..abb2342 100644 --- a/src/openvpn/buffer.c +++ b/src/openvpn/buffer.c @@ -1234,51 +1234,46 @@ void buffer_list_aggregate_separator(struct buffer_list *bl, const size_t max_len, const char *sep) { - int sep_len = strlen(sep); + const int sep_len = strlen(sep); + struct buffer_entry *more = bl->head; + size_t size = 0; + int count = 0; + for (count = 0; more; ++count) + { + size_t extra_len = BLEN(&more->buf) + sep_len; + if (size + extra_len <= max_len) + { + size += extra_len; + more = more->next; + } + else + { + break; + } + } - if (bl->head) + if (count >= 2) { - struct buffer_entry *more = bl->head; - size_t size = 0; - int count = 0; - for (count = 0; more; ++count) + struct buffer_entry *f; + ALLOC_OBJ_CLEAR(f, struct buffer_entry); + f->buf = alloc_buf(size+1); /* prevent 0-byte malloc */ + + struct buffer_entry *e = bl->head; + for (size_t i = 0; e && i < count; ++i) { - size_t extra_len = BLEN(&more->buf) + sep_len; - if (size + extra_len <= max_len) - { - size += extra_len; - more = more->next; - } - else - { - break; - } + struct buffer_entry *next = e->next; + buf_copy(&f->buf, &e->buf); + buf_write(&f->buf, sep, sep_len); + free_buf(&e->buf); + free(e); + e = next; } - - if (count >= 2) + bl->head = f; + bl->size -= count-1; + f->next = more; + if (!more) { - int i; - struct buffer_entry *e = bl->head, *f; - - ALLOC_OBJ_CLEAR(f, struct buffer_entry); - f->buf = alloc_buf(size+1); /* prevent 0-byte malloc */ - f->buf.capacity = size; - for (i = 0; e && i < count; ++i) - { - struct buffer_entry *next = e->next; - buf_copy(&f->buf, &e->buf); - buf_write(&f->buf, sep, sep_len); - free_buf(&e->buf); - free(e); - e = next; - } - bl->head = f; - bl->size -= count-1; - f->next = more; - if (!more) - { - bl->tail = f; - } + bl->tail = f; } } }