From patchwork Sat Nov 11 21:29:15 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Heiko Hund X-Patchwork-Id: 73 X-Patchwork-Delegate: davids@openvpn.net Return-Path: Delivered-To: patchwork@openvpn.net Delivered-To: patchwork@openvpn.net Received: from director3.mail.ord1d.rsapps.net ([172.30.191.6]) by backend31.mail.ord1d.rsapps.net (Dovecot) with LMTP id KyVMFp4GCFqrRgAAgoeIoA for ; Sun, 12 Nov 2017 03:30:22 -0500 Received: from proxy1.mail.ord1d.rsapps.net ([172.30.191.6]) by director3.mail.ord1d.rsapps.net (Dovecot) with LMTP id 0dPoAp4GCFo1OQAAkXNnRw ; Sun, 12 Nov 2017 03:30:22 -0500 Received: from smtp26.gate.ord1c ([172.30.191.6]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) by proxy1.mail.ord1d.rsapps.net (Dovecot) with LMTP id MvcZFJ4GCFqODQAAasrz9Q ; Sun, 12 Nov 2017 03:30:22 -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: smtp26.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; dkim=fail (signature verification failed) header.d=sophos.com; dmarc=fail (p=none; dis=none) header.from=sophos.com X-Classification-ID: b93327f8-c783-11e7-a60f-a4badb0c5489-1-1 Received: from [216.34.181.88] ([216.34.181.88:19571] helo=lists.sourceforge.net) by smtp26.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 A9/6E-06059-E96080A5; Sun, 12 Nov 2017 03:30:22 -0500 Received: from localhost ([127.0.0.1] helo=sfs-ml-4.v29.ch3.sourceforge.com) by sfs-ml-4.v29.ch3.sourceforge.com with esmtp (Exim 4.89) (envelope-from ) id 1eDneF-0003Mf-7Q; Sun, 12 Nov 2017 08:29:47 +0000 Received: from sfi-mx-3.v28.ch3.sourceforge.com ([172.29.28.193] helo=mx.sourceforge.net) by sfs-ml-4.v29.ch3.sourceforge.com with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.89) (envelope-from ) id 1eDneD-0003MY-Tu for openvpn-devel@lists.sourceforge.net; Sun, 12 Nov 2017 08:29:45 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sourceforge.net; s=x; h=Message-ID:Content-Type:MIME-Version:References: In-Reply-To:Date:Subject:To:From:Sender:Reply-To:Cc: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=AniQmNFOG1ojYDkltd+ooYvuVZO0dkYKdpwzI/s3/E8=; b=Hz2eF+7Gc+eNfdnGdOBPPkx768 NZlbRu8wuJC6ZFC+BmF6B5ncc7KBKd3af58Jxgiq2fye8v+A1wysXMlhUvjJqbYTy2Ww6TSjkGmta 0aDqCPYJZY4UaJ6JjYMws1a+rwJOd/P+Y8prDO8rrVRW4icyUEyy4GqCWjNpC07XuDM4=; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sf.net; s=x ; h=Message-ID:Content-Type:MIME-Version:References:In-Reply-To:Date:Subject: To:From:Sender:Reply-To:Cc: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=AniQmNFOG1ojYDkltd+ooYvuVZO0dkYKdpwzI/s3/E8=; b=AHgfNQifyTfvGanlNEpN7S6azn 9Zf8ww1UWrmklEPiAEeT5YilJsd/+KUkBH+VqOxyFM8jYR7VktzxTBy8HPZ49kFYB0PxMn0uHu+GW r6INfvwa6I94Z78r0IMckg2H38k11Y5nLmHh3Kl7DQuNXYNPVOZ7Gl3IvTnALT8pCe04=; Received: from mx1.sophos.com ([145.253.124.137]) by sfi-mx-3.v28.ch3.sourceforge.com with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.89) id 1eDneB-0005wP-Ii for openvpn-devel@lists.sourceforge.net; Sun, 12 Nov 2017 08:29:45 +0000 Received: from mx1.sophos.com (localhost.localdomain [127.0.0.1]) by localhost (Postfix) with SMTP id 228C1888853 for ; Sun, 12 Nov 2017 08:29:37 +0000 (GMT) Received: from de-wie-exch3a.green.sophos (unknown [10.60.70.61]) by mx1.sophos.com (Postfix) with ESMTPS id 148F7888823 for ; Sun, 12 Nov 2017 08:29:37 +0000 (GMT) Received: from localhost.localdomain (10.128.128.78) by de-wie-exch3a.green.sophos (10.60.70.61) with Microsoft SMTP Server (TLS) id 14.3.169.1; Sun, 12 Nov 2017 09:29:21 +0100 From: Heiko Hund To: <1477672963-5724-7-git-send-email-heiko.hund@sophos.com>, Date: Sun, 12 Nov 2017 09:29:15 +0100 X-Mailer: git-send-email 2.11.0 In-Reply-To: <1477672963-5724-8-git-send-email-heiko.hund@sophos.com> References: <1477672963-5724-8-git-send-email-heiko.hund@sophos.com> MIME-Version: 1.0 Message-ID: <7c0332b7-f19c-4290-bbbb-ae8cf136f116@DE-WIE-EXCH3A.green.sophos> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sophos.com; h=from:to:subject:date:in-reply-to:references:mime-version:content-type:message-id; s=global; bh=AniQmNFOG1ojYDkltd+ooYvuVZO0dkYKdpwzI/s3/E8=; b=vgrVl4m2M6IwP9LJ7RRB886Xkx2cTVDe8iH+d6MVPumL2DXxYM+JYYdQpNwY2TJ30u0xejXxuFTczgCcHfKVvWFHyzdHbBTlIqpufOIoP3/P+Jp+yOmQ2DbiMJ7Ug8yCnxeP+c2t+B8kWQGV+Nh1NwXtLq6ig1WkMMRjsktXDAk= 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.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature -0.0 AWL AWL: Adjusted score from AWL reputation of From: address X-Headers-End: 1eDneB-0005wP-Ii Subject: [Openvpn-devel] [PATCHv2 7/7] Add gc_arena to struct argv to save allocations 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 With the private gc_arena we do not have to allocate the strings found during parsing again, since we know the arena they are allocated in is valid as long as the argv vector is. Signed-off-by: Heiko Hund --- src/openvpn/argv.c | 44 ++++++++++++++++-------------------- src/openvpn/argv.h | 1 + tests/unit_tests/openvpn/test_argv.c | 23 +++++++++++++++++++ 3 files changed, 43 insertions(+), 25 deletions(-) diff --git a/src/openvpn/argv.c b/src/openvpn/argv.c index 419b1dc6..51dd1b22 100644 --- a/src/openvpn/argv.c +++ b/src/openvpn/argv.c @@ -46,12 +46,11 @@ argv_extend(struct argv *a, const size_t newcap) { char **newargv; size_t i; - ALLOC_ARRAY_CLEAR(newargv, char *, newcap); + ALLOC_ARRAY_CLEAR_GC(newargv, char *, newcap, &a->gc); for (i = 0; i < a->argc; ++i) { newargv[i] = a->argv[i]; } - free(a->argv); a->argv = newargv; a->capacity = newcap; } @@ -63,6 +62,7 @@ argv_init(struct argv *a) a->capacity = 0; a->argc = 0; a->argv = NULL; + a->gc = gc_new(); argv_extend(a, 8); } @@ -77,24 +77,21 @@ argv_new(void) void argv_free(struct argv *a) { - size_t i; - for (i = 0; i < a->argc; ++i) - { - free(a->argv[i]); - } - free(a->argv); + gc_free(&a->gc); } static void argv_reset(struct argv *a) { - size_t i; - for (i = 0; i < a->argc; ++i) + if (a->argc) { - free(a->argv[i]); - a->argv[i] = NULL; + size_t i; + for (i = 0; i < a->argc; ++i) + { + a->argv[i] = NULL; + } + a->argc = 0; } - a->argc = 0; } static void @@ -106,7 +103,7 @@ argv_grow(struct argv *a, const size_t add) } static void -argv_append(struct argv *a, char *str) /* str must have been malloced or be NULL */ +argv_append(struct argv *a, char *str) /* str must have been gc_malloced or be NULL */ { argv_grow(a, 1); a->argv[a->argc++] = str; @@ -127,7 +124,7 @@ argv_clone(const struct argv *a, const size_t headroom) { for (i = 0; i < a->argc; ++i) { - argv_append(&r, string_alloc(a->argv[i], NULL)); + argv_append(&r, string_alloc(a->argv[i], &r.gc)); } } return r; @@ -138,7 +135,7 @@ argv_insert_head(const struct argv *a, const char *head) { struct argv r; r = argv_clone(a, 1); - r.argv[0] = string_alloc(head, NULL); + r.argv[0] = string_alloc(head, &r.gc); return r; } @@ -243,7 +240,7 @@ argv_printf_arglist(struct argv *a, const char *format, va_list arglist) } size = adjust_power_of_2(len + 1); - buf = gc_malloc(size, false, &gc); + buf = gc_malloc(size, false, &a->gc); len = vsnprintf(buf, size, f, arglist); if (len < 0 || len >= size) { @@ -255,11 +252,11 @@ argv_printf_arglist(struct argv *a, const char *format, va_list arglist) while (end) { *end = '\0'; - argv_append(a, string_alloc(buf, NULL)); + argv_append(a, buf); buf = end + 1; end = strchr(buf, delim); } - argv_append(a, string_alloc(buf, NULL)); + argv_append(a, buf); if (a->argc != argc) { @@ -303,23 +300,20 @@ argv_parse_cmd(struct argv *a, const char *s) { int nparms; char *parms[MAX_PARMS + 1]; - struct gc_arena gc = gc_new(); argv_reset(a); - nparms = parse_line(s, parms, MAX_PARMS, "SCRIPT-ARGV", 0, D_ARGV_PARSE_CMD, &gc); + nparms = parse_line(s, parms, MAX_PARMS, "SCRIPT-ARGV", 0, D_ARGV_PARSE_CMD, &a->gc); if (nparms) { int i; for (i = 0; i < nparms; ++i) { - argv_append(a, string_alloc(parms[i], NULL)); + argv_append(a, parms[i]); } } else { - argv_append(a, string_alloc(s, NULL)); + argv_append(a, string_alloc(s, &a->gc)); } - - gc_free(&gc); } diff --git a/src/openvpn/argv.h b/src/openvpn/argv.h index 2a1945e3..a1aa7ebb 100644 --- a/src/openvpn/argv.h +++ b/src/openvpn/argv.h @@ -33,6 +33,7 @@ #include "buffer.h" struct argv { + struct gc_arena gc; size_t capacity; size_t argc; char **argv; diff --git a/tests/unit_tests/openvpn/test_argv.c b/tests/unit_tests/openvpn/test_argv.c index e15e2fe5..5bfef8af 100644 --- a/tests/unit_tests/openvpn/test_argv.c +++ b/tests/unit_tests/openvpn/test_argv.c @@ -118,6 +118,28 @@ argv_printf__empty_parameter__argc_correct(void **state) } static void +argv_printf__long_args__data_correct(void **state) +{ + int i; + struct argv a = argv_new(); + const char *args[] = { + "good_tools_have_good_names_even_though_it_might_impair_typing", + "--long-opt=looooooooooooooooooooooooooooooooooooooooooooooooong", + "--long-cat=loooooooooooooooooooooooooooooooooooooooooooooooooooonger", + "file_with_very_descriptive_filename_that_leaves_no_questions_open.jpg.exe" + }; + + argv_printf(&a, "%s %s %s %s", args[0], args[1], args[2], args[3]); + assert_int_equal(a.argc, 4); + for (i = 0; i < a.argc; i++) + { + assert_string_equal(a.argv[i], args[i]); + } + + argv_free(&a); +} + +static void argv_parse_cmd__command_string__argc_correct(void **state) { struct argv a = argv_new(); @@ -233,6 +255,7 @@ main(void) cmocka_unit_test(argv_printf__group_sep_in_arg__fail_no_ouput), cmocka_unit_test(argv_printf__combined_path_with_spaces__argc_correct), cmocka_unit_test(argv_printf__empty_parameter__argc_correct), + cmocka_unit_test(argv_printf__long_args__data_correct), cmocka_unit_test(argv_parse_cmd__command_string__argc_correct), cmocka_unit_test(argv_parse_cmd__command_and_extra_options__argc_correct), cmocka_unit_test(argv_printf_cat__used_twice__argc_correct),