From patchwork Mon Dec 11 17:02:14 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Frank Lichtenheld X-Patchwork-Id: 3519 Return-Path: Delivered-To: patchwork@openvpn.net Received: by 2002:a05:7300:8d12:b0:fc:24ac:f0cb with SMTP id i18csp5305991dys; Mon, 11 Dec 2023 09:03:25 -0800 (PST) X-Google-Smtp-Source: AGHT+IGWi4loJVAd+Kc5+oq+qUw17FJ4aNY0m6ZfU6Cpx1SH6boNMt5DHQcxwA7LUqHcsu5GeGT4 X-Received: by 2002:a05:6a21:78a3:b0:18b:a1d7:a736 with SMTP id bf35-20020a056a2178a300b0018ba1d7a736mr10966083pzc.6.1702314205013; Mon, 11 Dec 2023 09:03:25 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702314204; cv=none; d=google.com; s=arc-20160816; b=rVNrAdX2QEwmIP+K6clZ+3jCgxRkB1fV5HwMn3SwbBbKuuiNg3HSc0mKEgC9IwX2oG dppA/XapGm9fNqbumMIcm2eMWwlKBXoUgYxWXURSz4eqC+LZWfXLCmSuNmyv6e5BjcLX 2wlWgpclUtoidfjF5SrjFUYUcm8bXXm6sq5z/otEZNe5i2exVXbdIAK+rng7xfYLDUUn aGMHadkmxHvCEq6AAXOtGVkq4mkczzY3px5o70mPBiddPBZSW2PxjOTDG+HGr3LuexbL k+oemYlRLccLIzuoDTL+QuhrCc9E4XCwTl3SFcadoRd0UMP7xXjpjpKoY5+VjNlOUAqH h5fw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:content-transfer-encoding:cc: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=O7e0fKcW2fSWPYAQICk5o5YFRd2PfXeFTwRsiUC796k=; fh=CfxSpwd5kfiMJoL3kcov7PTxVKT7pGe/79idnx9XlBs=; b=cxOKygVsRy/jRPWinZyVLjydIuC36yq7UpwIEnUxh+aqLPB9R0dbja3h40D1xuDp8V f+MppI3SALFB0VOAF3BhsECUX4Vu/gA/mzAQYghhVfvprO2ZSNqd9oIau5LE0MG8KGXf +kGNoTa6OqE7tXFEfuGY7Yaw6RdXy5p5nxsxgtDwAl63eY9/zFk7+NfjSA2MOyX/1hCc dx/vdzCV44NvpSd5aI2keUeB8kbTV5ckGlmC8qS6+VGRz/SY1NaWHp39OjuNRBgCblWN WlQ9osuJUaJR4ZiZMWUsUCIMtWGNmQ4i3jHdRVqspVJ2Bbrn8p93qpTyVEp5obv0NVVC 70jg== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@sourceforge.net header.s=x header.b=L+oMq3Vm; dkim=neutral (body hash did not verify) header.i=@sf.net header.s=x header.b=JF3TjgYL; dkim=neutral (body hash did not verify) header.i=@lichtenheld.com header.s=MBO0001 header.b=lofuhIqS; 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 Received: from lists.sourceforge.net (lists.sourceforge.net. [216.105.38.7]) by mx.google.com with ESMTPS id 38-20020a630c66000000b005c203ad0e2csi5069280pgm.827.2023.12.11.09.03.24 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 11 Dec 2023 09:03:24 -0800 (PST) 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=neutral (body hash did not verify) header.i=@sourceforge.net header.s=x header.b=L+oMq3Vm; dkim=neutral (body hash did not verify) header.i=@sf.net header.s=x header.b=JF3TjgYL; dkim=neutral (body hash did not verify) header.i=@lichtenheld.com header.s=MBO0001 header.b=lofuhIqS; 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 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 1rCjfv-00068V-2Y; Mon, 11 Dec 2023 17:02:35 +0000 Received: from [172.30.20.202] (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 1rCjfs-00068G-83 for openvpn-devel@lists.sourceforge.net; Mon, 11 Dec 2023 17:02:34 +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:Cc:To:From:Sender:Reply-To: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=Z0eZy/uXfLW20yNsFN62R+rpmYVDEcD64brG5u10vgk=; b=L+oMq3VmLSpBJi0h1OFJ89+Jhw H+Ynjn4Whj+0IixevynG0FmXsOVBG0QHxyO+AKN+d4+xZZGKot35hnoo3mz0rJjXV7xbSfsbk3p+/ 3katT/NSjh+RMG66p5mCaptsR2VOdK9mDWntHwKa4HHGQQunhAdrqK5stkRBBRCncfKM=; 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:Cc:To:From:Sender:Reply-To: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=Z0eZy/uXfLW20yNsFN62R+rpmYVDEcD64brG5u10vgk=; b=JF3TjgYLTafjQMVnQgK64cNHkD rGHNPfmnURIn5Q5TovKAe/6UB7aTmvz30I+9IMZ/SNDS0yVF/0VTXM0flNksLqGrA+EPSaAfK1N3G Uh5hG4WRfdlg2r9LE7QALDRQjCQwEEWpbn+AEC7d27QA3J6VgKw3xHQZhDHgTSIaUTz0=; Received: from mout-p-101.mailbox.org ([80.241.56.151]) by sfi-mx-2.v28.lw.sourceforge.com with esmtps (TLS1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.95) id 1rCjfn-000490-G9 for openvpn-devel@lists.sourceforge.net; Mon, 11 Dec 2023 17:02:32 +0000 Received: from smtp202.mailbox.org (smtp202.mailbox.org [10.196.197.202]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-p-101.mailbox.org (Postfix) with ESMTPS id 4Spp2H51ZCz9t1Q; Mon, 11 Dec 2023 18:02:15 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=lichtenheld.com; s=MBO0001; t=1702314135; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Z0eZy/uXfLW20yNsFN62R+rpmYVDEcD64brG5u10vgk=; b=lofuhIqSc4yG8dq6igbRqdBS5oYzpnh3jemB9NUbIPNrlU5BZnJV8awfMuTudB8CyD+80j YMQBHgi5l0GubrOyyyrxRu6+IFAF0NnHLzcgGTGALXWRb9fVePQOoq2YSo3IBhfMQnYsXX GWucEkeoEEIGjBHfNq6/kf1O3WI080mbTcRyHzAjFMu/KjQA+JHB5u9LLag4Y0I0cRGGoy 6TjtitEJvqyPAS7QX0tljbjPmJFGzWkWazc6cXyPaUsY+yAHQw0CF0r5t4cCnXs/udKp/b zVDswPbKFgw4uWp8V9O0ZlmbcrKz3NTya4iB/0g0wuuX76K9f1qCRGDUkXeFDA== From: Frank Lichtenheld To: openvpn-devel@lists.sourceforge.net Date: Mon, 11 Dec 2023 18:02:14 +0100 Message-Id: <20231211170214.85417-1-frank@lichtenheld.com> In-Reply-To: References: MIME-Version: 1.0 X-Spam-Score: -0.9 (/) X-Spam-Report: Spam detection software, running on the system "util-spamd-2.v13.lw.sourceforge.com", 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: Since I was confused what exactly string_mod does, I added documentation and additional UTs to make it clearer. Change-Id: I911fb5c5fa4b41f1fc1a30c6bf8b314245f64a6e Signed-off-by: Frank Lichtenheld Acked-by: Arne Schwabe --- Content analysis details: (-0.9 points, 6.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -0.7 RCVD_IN_DNSWL_LOW RBL: Sender listed at https://www.dnswl.org/, low trust [80.241.56.151 listed in list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record -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.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain -0.0 T_SCC_BODY_TEXT_LINE No description available. X-Headers-End: 1rCjfn-000490-G9 Subject: [Openvpn-devel] [PATCH v1] buffer: add documentation for string_mod and extend related UT 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: , Cc: Arne Schwabe Errors-To: openvpn-devel-bounces@lists.sourceforge.net X-getmail-retrieved-from-mailbox: Inbox X-GMAIL-THRID: =?utf-8?q?1785005819827485227?= X-GMAIL-MSGID: =?utf-8?q?1785005819827485227?= Since I was confused what exactly string_mod does, I added documentation and additional UTs to make it clearer. Change-Id: I911fb5c5fa4b41f1fc1a30c6bf8b314245f64a6e Signed-off-by: Frank Lichtenheld Acked-by: Arne Schwabe --- This change was reviewed on Gerrit and approved by at least one developer. I request to merge it to master and release/2.6. Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/472 This mail reflects revision 1 of this Change. Acked-by according to Gerrit (reflected above): Arne Schwabe diff --git a/src/openvpn/buffer.h b/src/openvpn/buffer.h index 0456b27..61fc3a5 100644 --- a/src/openvpn/buffer.h +++ b/src/openvpn/buffer.h @@ -898,51 +898,78 @@ /* character classes */ -#define CC_ANY (1<<0) -#define CC_NULL (1<<1) +#define CC_ANY (1<<0) /**< any character */ +#define CC_NULL (1<<1) /**< null character \0 */ -#define CC_ALNUM (1<<2) -#define CC_ALPHA (1<<3) -#define CC_ASCII (1<<4) -#define CC_CNTRL (1<<5) -#define CC_DIGIT (1<<6) -#define CC_PRINT (1<<7) -#define CC_PUNCT (1<<8) -#define CC_SPACE (1<<9) -#define CC_XDIGIT (1<<10) +#define CC_ALNUM (1<<2) /**< alphanumeric isalnum() */ +#define CC_ALPHA (1<<3) /**< alphabetic isalpha() */ +#define CC_ASCII (1<<4) /**< ASCII character */ +#define CC_CNTRL (1<<5) /**< control character iscntrl() */ +#define CC_DIGIT (1<<6) /**< digit isdigit() */ +#define CC_PRINT (1<<7) /**< printable (>= 32, != 127) */ +#define CC_PUNCT (1<<8) /**< punctuation ispunct() */ +#define CC_SPACE (1<<9) /**< whitespace isspace() */ +#define CC_XDIGIT (1<<10) /**< hex digit isxdigit() */ -#define CC_BLANK (1<<11) -#define CC_NEWLINE (1<<12) -#define CC_CR (1<<13) +#define CC_BLANK (1<<11) /**< space or tab */ +#define CC_NEWLINE (1<<12) /**< newline */ +#define CC_CR (1<<13) /**< carriage return */ -#define CC_BACKSLASH (1<<14) -#define CC_UNDERBAR (1<<15) -#define CC_DASH (1<<16) -#define CC_DOT (1<<17) -#define CC_COMMA (1<<18) -#define CC_COLON (1<<19) -#define CC_SLASH (1<<20) -#define CC_SINGLE_QUOTE (1<<21) -#define CC_DOUBLE_QUOTE (1<<22) -#define CC_REVERSE_QUOTE (1<<23) -#define CC_AT (1<<24) -#define CC_EQUAL (1<<25) -#define CC_LESS_THAN (1<<26) -#define CC_GREATER_THAN (1<<27) -#define CC_PIPE (1<<28) -#define CC_QUESTION_MARK (1<<29) -#define CC_ASTERISK (1<<30) +#define CC_BACKSLASH (1<<14) /**< backslash */ +#define CC_UNDERBAR (1<<15) /**< underscore */ +#define CC_DASH (1<<16) /**< dash */ +#define CC_DOT (1<<17) /**< dot */ +#define CC_COMMA (1<<18) /**< comma */ +#define CC_COLON (1<<19) /**< colon */ +#define CC_SLASH (1<<20) /**< slash */ +#define CC_SINGLE_QUOTE (1<<21) /**< single quote */ +#define CC_DOUBLE_QUOTE (1<<22) /**< double quote */ +#define CC_REVERSE_QUOTE (1<<23) /**< reverse quote */ +#define CC_AT (1<<24) /**< at sign */ +#define CC_EQUAL (1<<25) /**< equal sign */ +#define CC_LESS_THAN (1<<26) /**< less than sign */ +#define CC_GREATER_THAN (1<<27) /**< greater than sign */ +#define CC_PIPE (1<<28) /**< pipe */ +#define CC_QUESTION_MARK (1<<29) /**< question mark */ +#define CC_ASTERISK (1<<30) /**< asterisk */ /* macro classes */ -#define CC_NAME (CC_ALNUM|CC_UNDERBAR) -#define CC_CRLF (CC_CR|CC_NEWLINE) +#define CC_NAME (CC_ALNUM|CC_UNDERBAR) /**< alphanumeric plus underscore */ +#define CC_CRLF (CC_CR|CC_NEWLINE) /**< carriage return or newline */ bool char_class(const unsigned char c, const unsigned int flags); bool string_class(const char *str, const unsigned int inclusive, const unsigned int exclusive); +/** + * Modifies a string in place by replacing certain classes of characters of it with a specified + * character. + * + * Guaranteed to not increase the length of the string. + * If replace is 0, characters are skipped instead of replaced. + * + * @param str The string to be modified. + * @param inclusive The character classes not to be replaced. + * @param exclusive Character classes to be replaced even if they are also in inclusive. + * @param replace The character to replace the specified character classes with. + * @return True if the string was not modified, false otherwise. + */ bool string_mod(char *str, const unsigned int inclusive, const unsigned int exclusive, const char replace); +/** + * Returns a copy of a string with certain classes of characters of it replaced with a specified + * character. + * + * If replace is 0, characters are skipped instead of replaced. + * + * @param str The input string to be modified. + * @param inclusive Character classes not to be replaced. + * @param exclusive Character classes to be replaced even if they are also in inclusive. + * @param replace The character to replace the specified character classes with. + * @param gc The garbage collector arena to allocate memory from. + * + * @return The modified string with characters replaced within the specified range. + */ const char *string_mod_const(const char *str, const unsigned int inclusive, const unsigned int exclusive, diff --git a/tests/unit_tests/openvpn/test_buffer.c b/tests/unit_tests/openvpn/test_buffer.c index f994812..ee84c1f 100644 --- a/tests/unit_tests/openvpn/test_buffer.c +++ b/tests/unit_tests/openvpn/test_buffer.c @@ -324,23 +324,32 @@ { char buf[256]; strcpy(buf, "There is \x01 a nice 1234 year old tr\x7f ee!"); - string_mod(buf, CC_PRINT, 0, '@'); + assert_false(string_mod(buf, CC_PRINT, 0, '@')); assert_string_equal(buf, "There is @ a nice 1234 year old tr@ ee!"); strcpy(buf, "There is \x01 a nice 1234 year old tr\x7f ee!"); - string_mod(buf, CC_PRINT, CC_DIGIT, '@'); + assert_true(string_mod(buf, CC_ANY, 0, '@')); + assert_string_equal(buf, "There is \x01 a nice 1234 year old tr\x7f ee!"); + + /* 0 as replace removes characters */ + strcpy(buf, "There is \x01 a nice 1234 year old tr\x7f ee!"); + assert_false(string_mod(buf, CC_PRINT, 0, '\0')); + assert_string_equal(buf, "There is a nice 1234 year old tr ee!"); + + strcpy(buf, "There is \x01 a nice 1234 year old tr\x7f ee!"); + assert_false(string_mod(buf, CC_PRINT, CC_DIGIT, '@')); assert_string_equal(buf, "There is @ a nice @@@@ year old tr@ ee!"); strcpy(buf, "There is \x01 a nice 1234 year old tr\x7f ee!"); - string_mod(buf, CC_ALPHA, CC_DIGIT, '.'); + assert_false(string_mod(buf, CC_ALPHA, CC_DIGIT, '.')); assert_string_equal(buf, "There.is...a.nice......year.old.tr..ee."); strcpy(buf, "There is \x01 a 'nice' \"1234\"\n year old \ntr\x7f ee!"); - string_mod(buf, CC_ALPHA|CC_DIGIT|CC_NEWLINE|CC_SINGLE_QUOTE, CC_DOUBLE_QUOTE|CC_BLANK, '.'); + assert_false(string_mod(buf, CC_ALPHA|CC_DIGIT|CC_NEWLINE|CC_SINGLE_QUOTE, CC_DOUBLE_QUOTE|CC_BLANK, '.')); assert_string_equal(buf, "There.is...a.'nice'..1234.\n.year.old.\ntr..ee."); strcpy(buf, "There is a \\'nice\\' \"1234\" [*] year old \ntree!"); - string_mod(buf, CC_PRINT, CC_BACKSLASH|CC_ASTERISK, '.'); + assert_false(string_mod(buf, CC_PRINT, CC_BACKSLASH|CC_ASTERISK, '.')); assert_string_equal(buf, "There is a .'nice.' \"1234\" [.] year old .tree!"); }