From patchwork Sat Oct 17 17:18:53 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gert Doering X-Patchwork-Id: 5482 Return-Path: Delivered-To: patchwork@openvpn.net Received: by 2002:a05:7001:a68f:b0:8d7:ebbe:b250 with SMTP id wf15csp4619090mab; Sat, 17 Oct 2026 10:19:21 -0700 (PDT) X-Forwarded-Encrypted: i=2; AKwUvByFGCCeTIjPp81zvl0TuU89goenjMtWKzAbXs2W0Ll8+O/Y6slxS1kA7rpxxOQa6meFiyfztR5/xdc=@openvpn.net X-Received: by 2002:a05:6870:5b94:b0:494:9395:751a with SMTP id 586e51a60fabf-4a73095b4a5mr4191028fac.10.1792257561369; Sat, 17 Oct 2026 10:19:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1792257561; cv=none; d=google.com; s=arc-20260327; b=SQ/jOVusu/xf6u+wwgvT5AcXLtIUYR70WFizgy0WZ9fxmWHwf0IniTkIDdWP8L6fgz kATGQqFKXmZF2dJOQjvrgwbYSvALIqQCvljpDHLFacUWCPhgNmxL/nmyiTZUA8BKe4mO 9QYE9CaSlzUGU2PaX1qmDQSo7Z7xwnBmSQtBKPv95x1W3fWfxHMcse5K+3hG5lBQOxrD okcXlC1VNk7mNWbi65zk5n96eYfuMayRQpXaBz8zYju5mv0BaZ82REtcKnbJ2TJUmPuc r5+stek7GxzGYbRf3kedvgmxMX0d9tT7wHaIKVAUHvLDfkXbv6ZUn4sPOCDDcHLT2uqO iPhw== 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=lLuwCLMad3JKaeSaLBvCXT6w3peB33vdzuuiE5S2uEA=; fh=4NbAC/LsuMLI0S0hprUlLSLCiHwg6SCAifhH718Jh0Q=; b=cPY8dOdFf/22KkTRNn6L8JFAcJkCAql69TgfrIbou4v/Y0JzIkBc5obV7sQzfl5H3T oqysk6X3zQV5CFsek4ayFqXYz+J5/Hcf2V3P0EsA7lJ5cg+Nvma3YJ20MQ7dxax8IvS1 qZht8M51EYT+sUMXAk5Bv65PIywRHcUXnJwcD8RxmTXJi70AkVicnrtS68uVtFxW/KSb HbT9PTucvtXyzEsoeVUi1ci+Wk78D+RcygRsK1E44BBJ8nUVzjqWZQ2Kd35mdzITizj8 Vm76atYoqruoWYOnjoGJUOTYQ6th500I52YsfsycAaqSMEcan3/x//94wravlL2uKplZ z8uQ==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@lists.sourceforge.net header.s=beta header.b=mkU6t4hc; dkim=neutral (body hash did not verify) header.i=@sourceforge.net header.s=x header.b=c+plBKmR; dkim=neutral (body hash did not verify) header.i=@sf.net header.s=x header.b=czrNLklS; 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-4a73b7fc156si7742344fac.269.2026.10.17.10.19.21 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sat, 17 Oct 2026 10:19:21 -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=mkU6t4hc; dkim=neutral (body hash did not verify) header.i=@sourceforge.net header.s=x header.b=c+plBKmR; dkim=neutral (body hash did not verify) header.i=@sf.net header.s=x header.b=czrNLklS; 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=lLuwCLMad3JKaeSaLBvCXT6w3peB33vdzuuiE5S2uEA=; b=mkU6t4hcESg5yUS1cxsgoaAfl3 N/MmSysVqn5G6FbT23wNthWMGCtEaqEoEWVmMMMK7Bd7R6AH9/4/qozIEDwqfQb8zMDdYYdV/XHAY GlNWH4uE7kcdOit2I7NxJxRC4R40CZCBDhZLDekP5lPdN1T8Wi4gipfI9tLXMSKHnKRo=; Received: from [127.0.0.1] (helo=sfs-ml-4.v29.lw.sourceforge.com) by sfs-ml-4.v29.lw.sourceforge.com with esmtp (Exim 4.95) (envelope-from ) id 1xI83o-0000CE-PJ; Sat, 17 Oct 2026 17:19:08 +0000 Received: from [172.30.29.66] (helo=mx.sourceforge.net) by sfs-ml-4.v29.lw.sourceforge.com with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.95) (envelope-from ) id 1xI83n-0000C8-GN for openvpn-devel@lists.sourceforge.net; Sat, 17 Oct 2026 17:19:07 +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=CHSuPOmlApGIMU5zEqaeKsYkQNzwqx1hbB6lp/W2RtQ=; b=c+plBKmRlp5VbZeI65OT/irVb7 YhPWwYhpEiCHO9MiqF2hiuSTRHdPVdjrUKbnDjgc7nyCFUNTvVO5oznMUqtmv6YzXdEwKt22sbVuI FBhl0Qm5LcaSfbe4BsO5cm96fqNV5RjIhg+aNdSJKU8IqwgV7H+sqlXn3H4Gm5bPsT1U=; 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=CHSuPOmlApGIMU5zEqaeKsYkQNzwqx1hbB6lp/W2RtQ=; b=czrNLklS0Df/zbPMHMGvExlx8h WJcwpqeboZn3sI7vXHnzV2qCsIDFAR2E6yxCVlrXT4evhI/HKquwLYziI4TMR1OBdFp1Sx3sOje/I JjegtJyLEP+JQzqzFDZkWa3j00H3B8P45zvLpA4FvoTHTF3qykec986kTrDMriEItFTw=; 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 1xI83l-0006me-Ix for openvpn-devel@lists.sourceforge.net; Sat, 17 Oct 2026 17:19:07 +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 69HHIwuj016221 for ; Sat, 17 Oct 2026 19:18:58 +0200 Received: (from gert@localhost) by blue.greenie.muc.de (8.18.2/8.18.1/Submit) id 69HHIw9G016220 for openvpn-devel@lists.sourceforge.net; Sat, 17 Oct 2026 19:18:58 +0200 From: Gert Doering To: openvpn-devel@lists.sourceforge.net Date: Sat, 17 Oct 2026 19:18:53 +0200 Message-ID: <20261017171858.16208-1-gert@greenie.muc.de> X-Mailer: git-send-email 2.55.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-1.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 - Remove unused and wrongly document return value of buf_puts - Add UTs for buf_puts Both of these changes based on a PR by cuishuang . 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: 1xI83l-0006me-Ix Subject: [Openvpn-devel] [PATCH v3] buffer: Fix small issues in buf_printf and buf_puts 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: 1879318264741597376 X-GMAIL-MSGID: 1879318264741597376 From: Frank Lichtenheld - Remove unused and wrongly document return value of buf_puts - Add UTs for buf_puts Both of these changes based on a PR by cuishuang . In both functions: - Remove obsolete manual adding of \0 - Do not try to append if we can only overwrite the trailing \0 byte Change-Id: I1d6ed5d40910359b65b9f5c5d8f56ad3d3bc827a Signed-off-by: Frank Lichtenheld Acked-by: Razvan Cojocaru Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1997 --- 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/+/1997 This mail reflects revision 3 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 cc1b300..3599cc9 100644 --- a/src/openvpn/buffer.c +++ b/src/openvpn/buffer.c @@ -225,21 +225,20 @@ bool buf_printf(struct buffer *buf, const char *format, ...) { - int ret = false; + bool ret = false; if (buf_defined(buf)) { - uint8_t *ptr = BEND(buf); + char *ptr = (char *)BEND(buf); int cap = buf_forward_capacity(buf); - if (cap > 0) + if (cap > 1) /* can write more than \0 */ { int stat; va_list arglist; va_start(arglist, format); - stat = vsnprintf((char *)ptr, cap, format, arglist); + stat = vsnprintf(ptr, cap, format, arglist); va_end(arglist); - *(buf->data + buf->capacity - 1) = 0; /* windows vsnprintf needs this */ - buf->len += (int)strlen((char *)ptr); + buf->len += (int)strlen(ptr); if (stat >= 0 && stat < cap) { ret = true; @@ -249,19 +248,16 @@ return ret; } -bool +void buf_puts(struct buffer *buf, const char *str) { - int ret = false; - uint8_t *ptr = BEND(buf); + char *ptr = (char *)BEND(buf); int cap = buf_forward_capacity(buf); - if (cap > 0) + if (cap > 1) /* can write more than \0 */ { - int str_len = (int)strlcpy((char *)ptr, str, cap); + int str_len = (int)strlcpy(ptr, str, cap); buf->len += (str_len < cap) ? str_len : cap - 1; - ret = true; } - return ret; } /* diff --git a/src/openvpn/buffer.h b/src/openvpn/buffer.h index c66ed1a..eb64446 100644 --- a/src/openvpn/buffer.h +++ b/src/openvpn/buffer.h @@ -791,10 +791,8 @@ * * @param buf The buffer to append to. * @param str The null-terminated string to append. - * - * @return true if the string fit in the buffer, false if it was truncated. */ -bool buf_puts(struct buffer *buf, const char *str); +void buf_puts(struct buffer *buf, const char *str); /* diff --git a/tests/unit_tests/openvpn/test_buffer.c b/tests/unit_tests/openvpn/test_buffer.c index bf486e0..338265d 100644 --- a/tests/unit_tests/openvpn/test_buffer.c +++ b/tests/unit_tests/openvpn/test_buffer.c @@ -62,6 +62,10 @@ buf_printf(&buf, "%d", 123); buf_printf(&buf, "%s", "some text, too long to fit"); assert_buf_equals_str(&buf, "123some text, t"); + assert_int_equal(BCAP(&buf), 1); /* \0 byte not part of len */ + + buf_printf(&buf, "%s", "more"); + assert_buf_equals_str(&buf, "123some text, t"); buf_catrunc(&buf, "..."); assert_buf_equals_str(&buf, "123some text..."); @@ -79,6 +83,30 @@ } static void +test_buffer_puts(void **state) +{ + struct gc_arena gc = gc_new(); + struct buffer buf = alloc_buf_gc(5, &gc); + + buf_puts(&buf, "1234"); + assert_string_equal(BSTR(&buf), "1234"); + + buf_reset_len(&buf); + buf_puts(&buf, "12345"); + assert_string_equal(BSTR(&buf), "1234"); + + /* buf already full */ + buf_puts(&buf, "12345"); + assert_string_equal(BSTR(&buf), "1234"); + + struct buffer one_byte_buf = alloc_buf_gc(1, &gc); + buf_puts(&one_byte_buf, "x"); + assert_string_equal(BSTR(&one_byte_buf), ""); + + gc_free(&gc); +} + +static void test_buffer_format_hex_ex(void **state) { const int input_size = 10; @@ -632,6 +660,7 @@ const struct CMUnitTest tests[] = { cmocka_unit_test(test_buffer_strprefix), cmocka_unit_test(test_buffer_printf_catrunc), + cmocka_unit_test(test_buffer_puts), cmocka_unit_test(test_buffer_format_hex_ex), cmocka_unit_test_setup_teardown(test_buffer_list_aggregate_separator_empty, test_buffer_list_setup, test_buffer_list_teardown),