From nobody Fri Dec 12 14:06:20 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) client-ip=8.43.85.245; envelope-from=devel-bounces@lists.libvirt.org; helo=lists.libvirt.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) smtp.mailfrom=devel-bounces@lists.libvirt.org; dmarc=pass(p=reject dis=none) header.from=lists.libvirt.org ARC-Seal: i=1; a=rsa-sha256; t=1764251879; cv=none; d=zohomail.com; s=zohoarc; b=ITsGLZkKRZjyCW9W++HZqhxuE1+kr7KteUYZHA8DIdWuVK0JR+RXz8t7Td8vYRsx6tMVttTvW8ebcGE2ZV+bC3o2dE8YsxxK4xvBLr5u0GlE0O18S1Er3UTUi4RashSjhIL1b1mNDFoIllIUjn74Egg0x56WGRSFUJjS8SG6LRA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1764251879; h=Content-Type:Content-Transfer-Encoding:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Owner:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Subject:Subject:To:To:Message-Id:Cc; bh=8xV1P9eiGzYk+E3aX+KjdoSss6Qa6P80k2Vq2mM34YI=; b=NoaZLf2LJ+OwibqqIOBp3tq4EmQCMCmrluCN5gIvwrGQUa02rdJ0ZoYFz3LMkGuJjMnzhQ8I9/zmb69UJylcAn7Fps6YnySvruC4O+0KiFlo8Q3TBrXyogZJsM0XmyofeACowaWp03gWkOMNIJTnn9i7/Vh/ovKLIVPbnYjJ4P4= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) smtp.mailfrom=devel-bounces@lists.libvirt.org; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists.libvirt.org (lists.libvirt.org [8.43.85.245]) by mx.zohomail.com with SMTPS id 1764251879017307.1743718668922; Thu, 27 Nov 2025 05:57:59 -0800 (PST) Received: by lists.libvirt.org (Postfix, from userid 993) id 78CAD41AA5; Thu, 27 Nov 2025 08:57:58 -0500 (EST) Received: from [172.19.199.74] (lists.libvirt.org [8.43.85.245]) by lists.libvirt.org (Postfix) with ESMTP id 1834044BE1; Thu, 27 Nov 2025 08:53:36 -0500 (EST) Received: by lists.libvirt.org (Postfix, from userid 993) id 570CF4192A; Thu, 27 Nov 2025 08:52:37 -0500 (EST) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (3072 bits) server-digest SHA256) (No client certificate requested) by lists.libvirt.org (Postfix) with ESMTPS id DD78644C1F for ; Thu, 27 Nov 2025 08:51:13 -0500 (EST) Received: from mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-211-1QOhcDzPOtmNJNkwdQQydQ-1; Thu, 27 Nov 2025 08:51:12 -0500 Received: from mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.93]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 57E9F18001D1 for ; Thu, 27 Nov 2025 13:51:11 +0000 (UTC) Received: from moe (unknown [10.43.3.236]) by mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id A08911800451 for ; Thu, 27 Nov 2025 13:51:10 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on lists.libvirt.org X-Spam-Level: X-Spam-Status: No, score=-5.0 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED,RCVD_IN_VALIDITY_RPBL_BLOCKED, RCVD_IN_VALIDITY_SAFE_BLOCKED,SPF_PASS autolearn=unavailable autolearn_force=no version=4.0.1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1764251473; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=8xV1P9eiGzYk+E3aX+KjdoSss6Qa6P80k2Vq2mM34YI=; b=ACSOKhMDpOV9/+OobaFKgFjSwRmrg/WekqdcIiccfq4NZoy/5RArcCThu6u9s5kfVI9Lvk rbeYt6DgNxJBSAck1MxRVTIn0c9DxVWpk4vQbxxwfLardtezEPPInVSGPhscmuEAefg3qr G2XnDIsL+FDzW80N3ZeIcI5AfxieACo= X-MC-Unique: 1QOhcDzPOtmNJNkwdQQydQ-1 X-Mimecast-MFC-AGG-ID: 1QOhcDzPOtmNJNkwdQQydQ_1764251471 To: devel@lists.libvirt.org Subject: [PATCH 4/8] virSkipSpacesBackwards: Turn @endp into const Date: Thu, 27 Nov 2025 14:50:59 +0100 Message-ID: In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.93 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: bgRWGT2zvOyUku_TcBWMVzHl3xPOEYmaI8nRA2A6qco_1764251471 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Message-ID-Hash: GMYPEZ6SLVBMRRTAC4LPALABJABXDQ4P X-Message-ID-Hash: GMYPEZ6SLVBMRRTAC4LPALABJABXDQ4P X-MailFrom: mprivozn@redhat.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; header-match-devel.lists.libvirt.org-0; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.10 Precedence: list List-Id: Development discussions about the libvirt library & tools Archived-At: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: From: Michal Privoznik via Devel Reply-To: Michal Privoznik X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1764251879557019200 Content-Type: text/plain; charset="utf-8"; x-default="true" From: Michal Privoznik The aim of virSkipSpacesBackwards() is find the first space trailing character in given string, For instance, if the input is "Something whitespacey ", then the output should be pointing to the very first space after "y". Problem here is that the input string is constant, but the returned pointer is non-constant. This is confusing, a caller shouldn't be able to modify the string, since the input was a constant string. Therefore, make the function return a const pointer too. Under the hood the function used virTrimSpaces() which under some circumstances could modify the input string. A trick was used to hide this fact away, but to be double sure rewrite the function's body. Signed-off-by: Michal Privoznik --- src/util/virstring.c | 20 +++++++++++++------- src/util/virstring.h | 2 +- src/util/virsysinfo.c | 14 +++++++------- tests/virstringtest.c | 2 +- 4 files changed, 22 insertions(+), 16 deletions(-) diff --git a/src/util/virstring.c b/src/util/virstring.c index 7249ab4e7c..e001d76bf1 100644 --- a/src/util/virstring.c +++ b/src/util/virstring.c @@ -484,17 +484,23 @@ virTrimSpaces(char *str, char **endp) * but spaces. */ void -virSkipSpacesBackwards(const char *str, char **endp) +virSkipSpacesBackwards(const char *str, + const char **endp) { - /* Casting away const is safe, since virTrimSpaces does not - * modify string with this particular usage. */ - char *s =3D (char*) str; + const char *end; =20 if (!*endp) - *endp =3D s + strlen(s); - virTrimSpaces(s, endp); - if (s =3D=3D *endp) + end =3D str + strlen(str); + else + end =3D *endp; + + while (end > str && g_ascii_isspace(end[-1])) + end--; + + if (str =3D=3D end) *endp =3D NULL; + else + *endp =3D end; } =20 /** diff --git a/src/util/virstring.h b/src/util/virstring.h index 3d880faf0c..8c2208ece8 100644 --- a/src/util/virstring.h +++ b/src/util/virstring.h @@ -75,7 +75,7 @@ void virSkipSpaces(const char **str) ATTRIBUTE_NONNULL(1); void virSkipSpacesAndBackslash(const char **str) ATTRIBUTE_NONNULL(1); void virSkipToDigit(const char **str) ATTRIBUTE_NONNULL(1); void virTrimSpaces(char *str, char **endp) ATTRIBUTE_NONNULL(1); -void virSkipSpacesBackwards(const char *str, char **endp) +void virSkipSpacesBackwards(const char *str, const char **endp) ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2); =20 bool virStringIsEmpty(const char *str); diff --git a/src/util/virsysinfo.c b/src/util/virsysinfo.c index 0f12a8964f..c017ad34b7 100644 --- a/src/util/virsysinfo.c +++ b/src/util/virsysinfo.c @@ -625,7 +625,7 @@ virSysinfoParseBIOS(const char *base, virSysinfoBIOSDef= **bios) { int ret =3D -1; const char *cur; - char *eol =3D NULL; + const char *eol =3D NULL; virSysinfoBIOSDef *def; =20 if ((cur =3D strstr(base, "BIOS Information")) =3D=3D NULL) @@ -679,7 +679,7 @@ virSysinfoParseX86System(const char *base, virSysinfoSy= stemDef **sysdef) { int ret =3D -1; const char *cur; - char *eol =3D NULL; + const char *eol =3D NULL; virSysinfoSystemDef *def; =20 if ((cur =3D strstr(base, "System Information")) =3D=3D NULL) @@ -755,7 +755,7 @@ virSysinfoParseX86BaseBoard(const char *base, size_t *nbaseBoard) { const char *cur; - char *eol =3D NULL; + const char *eol =3D NULL; virSysinfoBaseBoardDef *boards =3D NULL; size_t nboards =3D 0; =20 @@ -832,7 +832,7 @@ virSysinfoParseX86Chassis(const char *base, { int ret =3D -1; const char *cur; - char *eol =3D NULL; + const char *eol =3D NULL; virSysinfoChassisDef *def; =20 if ((cur =3D strstr(base, "Chassis Information")) =3D=3D NULL) @@ -942,7 +942,7 @@ virSysinfoParseOEMStrings(const char *base, while ((cur =3D strstr(cur, "String "))) { char *collon =3D NULL; unsigned int idx =3D 0; - char *eol; + const char *eol; =20 cur +=3D 7; =20 @@ -1005,7 +1005,7 @@ static void virSysinfoParseX86Processor(const char *base, virSysinfoDef *ret) { const char *cur, *tmp_base; - char *eol; + const char *eol; virSysinfoProcessorDef *processor; =20 while ((tmp_base =3D strstr(base, "Processor Information")) !=3D NULL)= { @@ -1103,7 +1103,7 @@ static void virSysinfoParseX86Memory(const char *base, virSysinfoDef *ret) { const char *cur, *tmp_base; - char *eol; + const char *eol; virSysinfoMemoryDef *memory; =20 while ((tmp_base =3D strstr(base, "Memory Device")) !=3D NULL) { diff --git a/tests/virstringtest.c b/tests/virstringtest.c index a9c8e621ce..0792155cc3 100644 --- a/tests/virstringtest.c +++ b/tests/virstringtest.c @@ -118,7 +118,7 @@ static int testSkipSpacesBackwards(const void *opaque G_GNUC_UNUSED) { const char *str =3D TEST_STR TEST_SPACES; - char *eol =3D NULL; + const char *eol =3D NULL; =20 virSkipSpacesBackwards(str, &eol); =20 --=20 2.51.2