From patchwork Fri Dec 8 16:29:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "plaisthos (Code Review)" X-Patchwork-Id: 3510 Return-Path: Delivered-To: patchwork@openvpn.net Received: by 2002:a05:7300:8d12:b0:fc:24ac:f0cb with SMTP id i18csp3709838dys; Fri, 8 Dec 2023 08:30:40 -0800 (PST) X-Google-Smtp-Source: AGHT+IFkmeaUdynDxJVbVqJ8+VQK6eYgE+XmyO8EkMzMDnMR3S3WyOZ8VH/VSb7o+8lqaFWxKmSU X-Received: by 2002:a05:6a21:998e:b0:187:4a56:9a06 with SMTP id ve14-20020a056a21998e00b001874a569a06mr490337pzb.5.1702053040527; Fri, 08 Dec 2023 08:30:40 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702053040; cv=none; d=google.com; s=arc-20160816; b=PHtQSxzYnzrhCYUUZaSwEAlQdMtddpYMkvUkNJKmxnMrx5ohQl4fsXJNLBHj2A4ktV pUn+n+c2Wy/spnfWU14PuZDi4livOOk3U8y/a974E/zIBBa4ausBXE9nTTLnohFIn/0d txtT5KsCFRUlzsR6periLC+7FBVYxQFty0934HBEBOq5brtOgLlNfILQldRmP+AjTyj7 G0GfSPyAVkAo/C4DZOjrYLB1RnBznYzkGr+o9VSGVYKbvGlqpBUYzZM9Y4qMRCCJBVsu JHYExBadZFswlB+/199hAyqyQyxiE3pbVIECSTFCBmFkEQ4yNCgI4rGODUV7MIuOb/fQ 99aA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:cc:reply-to:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence:subject:user-agent :mime-version:message-id:references:auto-submitted:to:date:from :dkim-signature:dkim-signature:dkim-signature; bh=PucYKzZvB9V6hoGCdecDV/4h4poinIa+I0NbgozSiHs=; fh=GFP4qDxgyJ2WEPo/oeLZg3Mj4NqvY1j2nTvTt7psNwg=; b=fLfIsUoqCnjuZbmVsaTsv11IXPjDwiie3cep3Ea7ZeCzeQSg+FYiqRqUs3/xOAfJ58 2a4lk1TCZoJnnaFkKId5IG5EeoWRGDMuDNMnWjZhdiPQAFvy3upeoiTJMyWt+K/3hPn/ 15df45MXKbHkx2yQoxH2G8KAE7EcsVv7hNeftUkKK8CqI35uDwpX5oqS6PGUYjU1Q4z6 QINWb4zQBW2BBBZccNzcKnpU1ZBgVROgSqif02WGZwCJIZpWxzUQWBH0zLtnPEWVE3IO WK8SmsKn6TdmEdryTTIH1xMcT5sAthGkdPhYaGvRaIh8eGSpXT68nXAiqaSAtoSRvAzT 4myg== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@sourceforge.net header.s=x header.b=DENdxEgN; dkim=neutral (body hash did not verify) header.i=@sf.net header.s=x header.b=FRXJdVLt; dkim=neutral (body hash did not verify) header.i=@openvpn.net header.s=google header.b=OJLOG6gK; 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=openvpn.net Received: from lists.sourceforge.net (lists.sourceforge.net. [216.105.38.7]) by mx.google.com with ESMTPS id k125-20020a632483000000b005c66048f388si1713462pgk.278.2023.12.08.08.30.40 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 08 Dec 2023 08:30:40 -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=DENdxEgN; dkim=neutral (body hash did not verify) header.i=@sf.net header.s=x header.b=FRXJdVLt; dkim=neutral (body hash did not verify) header.i=@openvpn.net header.s=google header.b=OJLOG6gK; 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=openvpn.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 1rBdk0-00038K-Gr; Fri, 08 Dec 2023 16:30:17 +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 1rBdjr-00037v-Bb for openvpn-devel@lists.sourceforge.net; Fri, 08 Dec 2023 16:30:07 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sourceforge.net; s=x; h=Content-Type:Content-Transfer-Encoding:MIME-Version :Message-ID:Reply-To:References:Subject:List-Unsubscribe:List-Id:Cc:To:Date: From:Sender:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:List-Help: List-Subscribe:List-Post:List-Owner:List-Archive; bh=KyphPXwVj65Ry2+HcRx9vVaVZURNj1AEHYCLSLgFCwI=; b=DENdxEgNORPXhlWQi2XhDlec5S UeGMJGrilfJ6XPk4X+llohOHg0Hr1z49HAPrvKxqQNP5r+JSL+S81BcDoE7o16o+iPWDzIOW8vixJ uV8L4Wmt0B0iBPzbIjiZ4S3IANkwj3rEAGrCOxg/mpKiw/wmu4Ev7ZqgnPudboFAJQX0=; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sf.net; s=x ; h=Content-Type:Content-Transfer-Encoding:MIME-Version:Message-ID:Reply-To: References:Subject:List-Unsubscribe:List-Id:Cc:To:Date:From:Sender:Content-ID :Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To: Resent-Cc:Resent-Message-ID:In-Reply-To:List-Help:List-Subscribe:List-Post: List-Owner:List-Archive; bh=KyphPXwVj65Ry2+HcRx9vVaVZURNj1AEHYCLSLgFCwI=; b=F RXJdVLtVz0oiHCYtgIXqX5sFbJyPj4LnuJsWzTGTrjtWTkuBsWEqNU3xnsbRzJ8yTDHqbf2F2Dt64 o0meA+gU2GATqnqjABisdPOsviN7CNxejqKk0HoZo185e4ql6eaDVnFim3sSJjXfW/dNTV9zYyBTu kn+Vfw1Y/me50f9M=; Received: from mail-wr1-f47.google.com ([209.85.221.47]) by sfi-mx-2.v28.lw.sourceforge.com with esmtps (TLS1.2:ECDHE-RSA-AES128-GCM-SHA256:128) (Exim 4.95) id 1rBdjm-0007lZ-BW for openvpn-devel@lists.sourceforge.net; Fri, 08 Dec 2023 16:30:05 +0000 Received: by mail-wr1-f47.google.com with SMTP id ffacd0b85a97d-3332e351670so2127850f8f.0 for ; Fri, 08 Dec 2023 08:30:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=openvpn.net; s=google; t=1702052991; x=1702657791; darn=lists.sourceforge.net; h=user-agent:content-disposition:content-transfer-encoding :mime-version:message-id:reply-to:references:subject :list-unsubscribe:list-id:auto-submitted:cc:to:date:from:from:to:cc :subject:date:message-id:reply-to; bh=KyphPXwVj65Ry2+HcRx9vVaVZURNj1AEHYCLSLgFCwI=; b=OJLOG6gKYaexlznlpXDQ2ouW4lFC+6QT7J6g0dAxTYkQuDvXwRomkYVvY0Z3bTxLKU TLtBeFvNbyHusPaRdrQQ/oPYgnUfITcYKkMw8TcnlAvSI5LQifddcYIXBYaq0VQFrnv7 HeR4ISMGJeiJmgyXdFmyIjME+B0T1QF3sXyiY4YhLeNvfuQ4f2L8IFFvIFqjg6hU9GrB FzsybQN03cr9ClULbT91Q9bkPw+H8yXU91aoORDop7JAyPWttivHgo2LLTSXCbVz7rkm EoAG3AbIDRxor2urWTwUB3dWNlXe09KkJUBYmxEDBHjVLwxF3PDuvrPjHTAVGrstnuL8 BRYg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702052991; x=1702657791; h=user-agent:content-disposition:content-transfer-encoding :mime-version:message-id:reply-to:references:subject :list-unsubscribe:list-id:auto-submitted:cc:to:date:from :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=KyphPXwVj65Ry2+HcRx9vVaVZURNj1AEHYCLSLgFCwI=; b=LESWVR2ntk8/Ro6TmhoaY3PwxGyx6DIC/QVdnsXEUJsttGRwRYr1H0ZChTsU+wkij/ mp30zX9DmO2p0+NBGPPKrCIKN/qdMrOoFlvCl2TbHALxwztBaCnYcFJEoW66mOZdFG7B 0ywZZ4ZNa0//EFOaH5mkjlLTZGV0rCZgQKolkhqSk9VvUuz0ZnpJb3DjbRjjER6AFIF+ smCla1weuKKtYYq/annchN7B9clouvujLR/2+Z3vqy9D4uzybJlWCl/ps+jwNRD95gUY zMyxUCRxD7KOipfeGWjzdNW3yacUhF5LZXfr93QfI0MbODufidBN+uC2hKVxYmZnLjCE 42Vw== X-Gm-Message-State: AOJu0YzKFvtWTBw0qLfKdcR9v4iHLX5jrC/cypwu5mfd4LcRDEpopcdg nnmqIBeTU5Fuv1vjQ0yGubfExkG9gc8oMqOxYX8= X-Received: by 2002:a05:6000:1b05:b0:334:328:4bd2 with SMTP id f5-20020a0560001b0500b0033403284bd2mr79452wrz.256.1702052990761; Fri, 08 Dec 2023 08:29:50 -0800 (PST) Received: from gerrit.openvpn.in (ec2-18-159-0-78.eu-central-1.compute.amazonaws.com. [18.159.0.78]) by smtp.gmail.com with ESMTPSA id d16-20020a5d4f90000000b0033349bccac6sm2397568wru.1.2023.12.08.08.29.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Dec 2023 08:29:50 -0800 (PST) From: "flichtenheld (Code Review)" X-Google-Original-From: "flichtenheld (Code Review)" X-Gerrit-PatchSet: 1 Date: Fri, 8 Dec 2023 16:29:49 +0000 To: plaisthos Auto-Submitted: auto-generated X-Gerrit-MessageType: newchange X-Gerrit-Change-Id: I911fb5c5fa4b41f1fc1a30c6bf8b314245f64a6e X-Gerrit-Change-Number: 472 X-Gerrit-Project: openvpn X-Gerrit-ChangeURL: X-Gerrit-Commit: 87a2759fb38dffb5138e91183329a0757a55f19c References: Message-ID: <7153edcd45b85278164dbb86bf98b232c4090856-HTML@gerrit.openvpn.net> MIME-Version: 1.0 User-Agent: Gerrit/3.8.2 X-Spam-Score: -0.2 (/) X-Spam-Report: Spam detection software, running on the system "util-spamd-1.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: Attention is currently required from: plaisthos. Hello plaisthos, I'd like you to do a code review. Please visit Content analysis details: (-0.2 points, 6.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [209.85.221.47 listed in list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record -0.0 RCVD_IN_MSPIKE_H2 RBL: Average reputation (+2) [209.85.221.47 listed in wl.mailspike.net] 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record 0.0 WEIRD_PORT URI: Uses non-standard port number for HTTP 0.0 HTML_MESSAGE BODY: HTML included in message -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature -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_EF Message has a valid DKIM or DK signature from envelope-from domain -0.0 T_SCC_BODY_TEXT_LINE No description available. 0.0 T_KAM_HTML_FONT_INVALID Test for Invalidly Named or Formatted Colors in HTML X-Headers-End: 1rBdjm-0007lZ-BW Subject: [Openvpn-devel] [M] Change in openvpn[master]: 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: , Reply-To: frank@lichtenheld.com, arne-openvpn@rfc2549.org, openvpn-devel@lists.sourceforge.net Cc: openvpn-devel Errors-To: openvpn-devel-bounces@lists.sourceforge.net X-getmail-retrieved-from-mailbox: Inbox X-GMAIL-THRID: =?utf-8?q?1784731969011942501?= X-GMAIL-MSGID: =?utf-8?q?1784731969011942501?= X-getmail-filter-classifier: gerrit message type newchange Attention is currently required from: plaisthos. Hello plaisthos, I'd like you to do a code review. Please visit http://gerrit.openvpn.net/c/openvpn/+/472?usp=email to review the following change. Change subject: buffer: add documentation for string_mod and extend related UT ...................................................................... buffer: add documentation for string_mod and extend related UT 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 --- M src/openvpn/buffer.h M tests/unit_tests/openvpn/test_buffer.c 2 files changed, 74 insertions(+), 38 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/72/472/1 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!"); }