public inbox for linux-btrfs@vger.kernel.org
 help / color / mirror / Atom feed
From: Qu Wenruo <wqu@suse.com>
To: linux-btrfs@vger.kernel.org
Subject: [PATCH 1/3] btrfs-progs: enhance find_option()
Date: Mon, 23 Feb 2026 09:42:58 +1030	[thread overview]
Message-ID: <4f0ee51a24d281ae387f01db510e825e9259f73f.1771801832.git.wqu@suse.com> (raw)
In-Reply-To: <cover.1771801832.git.wqu@suse.com>

There are two minor problems in that function:

- Latest GCC reports dropped 'const' qualifier

  The latest GCC (15.2.1) will report the 'const' qualifier is dropped
  for the strstr() call.

  It looks like the latest GCC has taken the 'const' qualifier checks
  one step further, that any pointer that points into a 'const' pointer
  should also have a 'const' qualifier, to make sure there is no
  modification into the original pointed memory.

  In our case, @options parameter has a 'const' qualifier, thus the
  returned @tmp pointer should also have a 'const' qualifier, or we can
  modify the @options through @tmp.

- No error handling if strdup() failed

Enhance them by:

- Add the 'const' qualifier for @tmp
  This also means we can not use @tmp pointer to modify the newly
  duplicated string.
  Instead use a new @i index to access the new string.

- Return NULL if strdup() failed
  And make the caller check errno for memory allocation failure.

Signed-off-by: Qu Wenruo <wqu@suse.com>
---
 common/utils.c | 22 +++++++++++++++++-----
 1 file changed, 17 insertions(+), 5 deletions(-)

diff --git a/common/utils.c b/common/utils.c
index 23dbd9d16781..f183705edb8f 100644
--- a/common/utils.c
+++ b/common/utils.c
@@ -375,19 +375,27 @@ struct mnt_entry {
 /*
  * Find first occurrence of up an option string (as "option=") in @options,
  * separated by comma. Return allocated string as "option=value"
+ *
+ * If NULL is returned, the caller needs to check errno to make sure it's not
+ * caused by memory allocation failure.
  */
 static char *find_option(const char *options, const char *option)
 {
-	char *tmp, *ret;
+	const char *tmp;
+	char *ret;
+	int i;
 
+	errno = 0;
 	tmp = strstr(options, option);
 	if (!tmp)
 		return NULL;
 	ret = strdup(tmp);
-	tmp = ret;
-	while (*tmp && *tmp != ',')
-		tmp++;
-	*tmp = 0;
+	if (!ret)
+		return NULL;
+	i = 0;
+	while (*(ret + i) && *(ret + i) != ',')
+		i++;
+	*(ret + i) = '\0';
 	return ret;
 }
 
@@ -600,6 +608,10 @@ int find_mount_fsroot(const char *subvol, const char *subvolid, char **mount)
 			 * requested by the caller
 			 */
 			opt = find_option(ent.options2, "subvolid=");
+			if (!opt && errno) {
+				ret = -errno;
+				goto out;
+			}
 			if (!opt)
 				goto nextline;
 			value = opt + strlen("subvolid=");
-- 
2.53.0


  reply	other threads:[~2026-02-22 23:19 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-02-22 23:12 [PATCH 0/3] btrfs-progs: fix dropped 'const' qualifiers exposed by the latest GCC (15.2.1) Qu Wenruo
2026-02-22 23:12 ` Qu Wenruo [this message]
2026-03-17 16:39   ` [PATCH 1/3] btrfs-progs: enhance find_option() David Sterba
2026-02-22 23:12 ` [PATCH 2/3] btrfs-progs: constify the @dots variable inside parse_range_u64() Qu Wenruo
2026-02-22 23:13 ` [PATCH 3/3] btrfs-progs: drop the 'const' qualifier from bconf_save_param() Qu Wenruo
2026-03-17 16:43 ` [PATCH 0/3] btrfs-progs: fix dropped 'const' qualifiers exposed by the latest GCC (15.2.1) David Sterba

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4f0ee51a24d281ae387f01db510e825e9259f73f.1771801832.git.wqu@suse.com \
    --to=wqu@suse.com \
    --cc=linux-btrfs@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox