From patchwork Thu Oct 8 16:13:24 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gert Doering X-Patchwork-Id: 5448 Return-Path: Delivered-To: patchwork@openvpn.net Received: by 2002:a05:7000:32d1:b0:8d1:cccb:4552 with SMTP id y17csp1918309mad; Thu, 8 Oct 2026 09:14:13 -0700 (PDT) X-Forwarded-Encrypted: i=2; AKwUvBxkeyN+OHsjgH1tp5KoHUhRvT8jcLji8WbavsyIHf7CNs9spHNA3luBKY+JOG/iGuDvgCV1TFe5Y3w=@openvpn.net X-Received: by 2002:a05:6820:1628:b0:6da:389:5c61 with SMTP id 006d021491bc7-6eb23a6cf91mr1894671eaf.3.1791476053142; Thu, 08 Oct 2026 09:14:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1791476053; cv=none; d=google.com; s=arc-20260327; b=jzwwF10/U/7sFJD4ddOd5BMbyCCzz6Kl5SJqNyLJ5xQItZOHebsv4YQyzJFvAMt+XZ xD/DmKWJmU/k5ycSIvvckT47Y6xzkF2HRGdJtankvuHtZHggfGXyB9QNrCPSula3l4Jm NfbYfWww4NGVjmL5f4N1/SSa3bMnPbk3zERfdfDykZtUEgR6WmW0YxW9mjuOzk7lL0x9 BUfUcmMEA6wULHmmzGfyHDS0v08/yC6cFYBIxn2Jlk45pAHJg7/k6J/6GbO14RKliLiR E/kPPaXdbb2uhPSFsMTts82mPcKcicxlJxQQnhL6FRst7kZNjf0/nNA+TbOaA1498bKE ePEw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20260327; h=errors-to:content-transfer-encoding:list-subscribe:list-help :list-post:list-archive:list-unsubscribe:list-id:precedence:subject :mime-version:references:in-reply-to:message-id:date:to:from :dkim-signature:dkim-signature:dkim-signature; bh=aRAqbStrtvoLP9oHgMGNcm4pktWhkWKg2y3ivvomqw8=; fh=4NbAC/LsuMLI0S0hprUlLSLCiHwg6SCAifhH718Jh0Q=; b=oC5jfYgNXNSKN06C3Vvb+DboYwLF19krndTM+tZtEChzWj7GMqLtyZq/a8OCNZ2s8W I1BtkgZNiVFHztV1zIHarC+ZLtxdU8963C8wkTpu0gUCD7+BCpweMtJ8yXlBUvVEQ2XE Z6AtnOUtBxSvCMXRi4whfRmWY6xn6E/3s5A4HiuCGwuuEdl73++KV+I+cTgrQoOdfp+v abY8Of/ZbaN7hnzAU4SbsTrIeQ4AYGOYy9s7/D215Dmkl16VQV7TK6Q+LNv3/IDTd/Ho HAhW/ZPBRv9AhupwZYGlsrZ0TjZaESH6N9eAzP6LfM+Dx4XdvgO9KnZ7v6QRkxPpJQew vSXQ==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@lists.sourceforge.net header.s=beta header.b=lWdfkzoI; dkim=neutral (body hash did not verify) header.i=@sourceforge.net header.s=x header.b=GHZdCQ+x; dkim=neutral (body hash did not verify) header.i=@sf.net header.s=x header.b=NZtE2VTh; spf=pass (google.com: domain of openvpn-devel-bounces@lists.sourceforge.net designates 216.105.38.7 as permitted sender) smtp.mailfrom=openvpn-devel-bounces@lists.sourceforge.net; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=muc.de Received: from lists.sourceforge.net (lists.sourceforge.net. [216.105.38.7]) by mx.google.com with ESMTPS id 586e51a60fabf-4a274bb61c9si6357002fac.47.2026.10.08.09.14.12 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 08 Oct 2026 09:14:12 -0700 (PDT) Received-SPF: pass (google.com: domain of openvpn-devel-bounces@lists.sourceforge.net designates 216.105.38.7 as permitted sender) client-ip=216.105.38.7; Authentication-Results: mx.google.com; dkim=pass header.i=@lists.sourceforge.net header.s=beta header.b=lWdfkzoI; dkim=neutral (body hash did not verify) header.i=@sourceforge.net header.s=x header.b=GHZdCQ+x; dkim=neutral (body hash did not verify) header.i=@sf.net header.s=x header.b=NZtE2VTh; spf=pass (google.com: domain of openvpn-devel-bounces@lists.sourceforge.net designates 216.105.38.7 as permitted sender) smtp.mailfrom=openvpn-devel-bounces@lists.sourceforge.net; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=muc.de DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.sourceforge.net; s=beta; h=Content-Transfer-Encoding:Content-Type: List-Subscribe:List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id: Subject:MIME-Version:References:In-Reply-To:Message-ID:Date:To:From:Sender: Reply-To:Cc:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=aRAqbStrtvoLP9oHgMGNcm4pktWhkWKg2y3ivvomqw8=; b=lWdfkzoIvJB5K9etphfg5pQiic VSChuTwxIc1WDzsPxkvfxAw7jfaLOrLDL0UKeuIuI7o+VxCndYYBFrDc/SuqXIH1QlwoOEXxMr3qA 1pFGFiusVvw3ZQTG/K3QD31qTwM9O1H8qWCguopJz9H4IuNwChdpT3onpWQWQKMa0ThU=; Received: from [127.0.0.1] (helo=sfs-ml-2.v29.lw.sourceforge.com) by sfs-ml-2.v29.lw.sourceforge.com with esmtp (Exim 4.95) (envelope-from ) id 1xEqkw-0007DA-8M; Thu, 08 Oct 2026 16:14:07 +0000 Received: from [172.30.29.66] (helo=mx.sourceforge.net) by sfs-ml-2.v29.lw.sourceforge.com with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.95) (envelope-from ) id 1xEqkd-0007BG-Il for openvpn-devel@lists.sourceforge.net; Thu, 08 Oct 2026 16:13:48 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sourceforge.net; s=x; h=Content-Transfer-Encoding:MIME-Version:References: In-Reply-To:Message-ID:Date:Subject:To:From:Sender:Reply-To:Cc:Content-Type: 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=VpzcID78sXHC9qEMzclyMZJ6VfHIgVDkezNV4p1gcYE=; b=GHZdCQ+x3GyTVv7cVe0AeUC7JK U5g8rIbWZavUnGowEb+VWfygFtWb7QfFzpu4BTLuPSVXfoY6dAP+p+2nvFtOXPIwV1hakXdKiB+0z VZC41Jd8JQa02lVRuxdN1VbokDV8hTRW+GCT66AzwMPyuzSQmL5Q4XiELf8qg4v/G+BY=; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sf.net; s=x ; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To:Message-ID: Date:Subject:To:From:Sender:Reply-To:Cc:Content-Type: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=VpzcID78sXHC9qEMzclyMZJ6VfHIgVDkezNV4p1gcYE=; b=NZtE2VThCm6k+MJVUKsFBJI5Xt LaA6XOHE1uo10QAX3WLm+VbE+XIlcCDTmXj1LQcp9+Imv44m//ecDHKaJMIU/sMKJ+NpqfGZfb1WI iCvfxZK9dngV3rxF18hc74NEbvUgXPX8LCFiTeHM+9ENXAHK5p3kaM3I99PSyvoI0dIg=; Received: from [193.149.48.129] (helo=blue.greenie.muc.de) by sfi-mx-1.v28.lw.sourceforge.com with esmtps (TLS1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.95) id 1xEqkT-0006yS-I7 for openvpn-devel@lists.sourceforge.net; Thu, 08 Oct 2026 16:13:39 +0000 Received: from blue.greenie.muc.de (localhost [127.0.0.1]) by blue.greenie.muc.de (8.18.1/8.18.1) with ESMTP id 698GDUXM032325 for ; Thu, 8 Oct 2026 18:13:30 +0200 Received: (from gert@localhost) by blue.greenie.muc.de (8.18.2/8.18.1/Submit) id 698GDUqY032324 for openvpn-devel@lists.sourceforge.net; Thu, 8 Oct 2026 18:13:30 +0200 From: Gert Doering To: openvpn-devel@lists.sourceforge.net Date: Thu, 8 Oct 2026 18:13:24 +0200 Message-ID: <20261008161329.32304-1-gert@greenie.muc.de> X-Mailer: git-send-email 2.53.0 In-Reply-To: References: MIME-Version: 1.0 X-Spam-Score: 1.3 (+) X-Spam-Report: Spam detection software, running on the system "sfi-spamd-2.hosts.colo.sdot.me", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: From: Frank Lichtenheld This is very weird code. We re-run strlen for every char that we delete, even though we actually know which char to test next. Avoid this. Also add unit tests to make sure I did not break it. Content analysis details: (1.3 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- 1.3 RDNS_NONE Delivered to internal network by a host with no rDNS X-Headers-End: 1xEqkT-0006yS-I7 Subject: [Openvpn-devel] [PATCH v2] buffer: Clean up rm_trailing_chars 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 X-GMAIL-THRID: 1878498793797252644 X-GMAIL-MSGID: 1878498793797252644 From: Frank Lichtenheld This is very weird code. We re-run strlen for every char that we delete, even though we actually know which char to test next. Avoid this. Also add unit tests to make sure I did not break it. Change-Id: I47e0741c75832a8f3fe161769feb27ddbfba3cdd Signed-off-by: Frank Lichtenheld Acked-by: Razvan Cojocaru Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1987 --- This change was reviewed on Gerrit and approved by at least one developer. I request to merge it to master. Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1987 This mail reflects revision 2 of this Change. Acked-by according to Gerrit (reflected above): Razvan Cojocaru diff --git a/src/openvpn/buffer.c b/src/openvpn/buffer.c index 8f558b1..a90ddae 100644 --- a/src/openvpn/buffer.c +++ b/src/openvpn/buffer.c @@ -592,21 +592,11 @@ void rm_trailing_chars(char *str, const char *what_to_delete) { - bool modified; - do + size_t len = strlen(str); + while (len > 0 && strchr(what_to_delete, str[len - 1]) != NULL) { - const size_t len = strlen(str); - modified = false; - if (len > 0) - { - char *cp = str + (len - 1); - if (strchr(what_to_delete, *cp) != NULL) - { - *cp = '\0'; - modified = true; - } - } - } while (modified); + str[--len] = '\0'; + } } /* diff --git a/tests/unit_tests/openvpn/test_buffer.c b/tests/unit_tests/openvpn/test_buffer.c index 2945e8a..bf486e0 100644 --- a/tests/unit_tests/openvpn/test_buffer.c +++ b/tests/unit_tests/openvpn/test_buffer.c @@ -435,6 +435,44 @@ } void +test_string_chomp(void **state) +{ + char string[10]; + + strcpy(string, "Foo\n"); + chomp(string); + assert_string_equal(string, "Foo"); + + strcpy(string, "Foo\r\n\n\r"); + chomp(string); + assert_string_equal(string, "Foo"); + + strcpy(string, "Foo"); + chomp(string); + assert_string_equal(string, "Foo"); + + strcpy(string, "F\ro\no"); + chomp(string); + assert_string_equal(string, "F\ro\no"); + + string[2] = '\0'; + chomp(string); + assert_string_equal(string, "F"); + + strcpy(string, ""); + chomp(string); + assert_string_equal(string, ""); + + strcpy(string, "Foo"); + rm_trailing_chars(string, "o"); + assert_string_equal(string, "F"); + + strcpy(string, "Foo"); + rm_trailing_chars(string, "oF"); + assert_string_equal(string, ""); +} + +void test_buffer_chomp(void **state) { struct gc_arena gc = gc_new(); @@ -616,6 +654,7 @@ cmocka_unit_test(test_character_string_mod_buf), cmocka_unit_test(test_snprintf), cmocka_unit_test(test_checked_snprintf), + cmocka_unit_test(test_string_chomp), cmocka_unit_test(test_buffer_chomp), cmocka_unit_test(test_buffer_null_terminate), cmocka_unit_test(test_buffer_null_predicates),