public inbox for linux-btrfs@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/3] btrfs-progs: fix dropped 'const' qualifiers exposed by the latest GCC (15.2.1)
@ 2026-02-22 23:12 Qu Wenruo
  2026-02-22 23:12 ` [PATCH 1/3] btrfs-progs: enhance find_option() Qu Wenruo
                   ` (3 more replies)
  0 siblings, 4 replies; 6+ messages in thread
From: Qu Wenruo @ 2026-02-22 23:12 UTC (permalink / raw)
  To: linux-btrfs

It looks lile the latest gcc has taken the 'const' qualifier checks one
step further, a char pointer that points into a string that has 'const'
qualifier should also has 'const'.

This will expose unexpected modification like the following:

 void my_func(const char *options)
 {
         char *dot;

         dot = strchr(options, '.');
         if (!dot)
                 *dot = '\0';
 }

In above example, @dot is either NULL or points to a location inside
@options. For the later case, since @dot itself is not const, we can
modify the content, resulting modification of the content of @options.
The latest GCC is able to detect such proxy modification and gives us
warning on them.

And in fact, btrfs-progs has exactly such proxied modification in
bconf_save_param(), fixed in the last patch by dropping the 'const'
quailifer.

Other than that, most are just false alerts and we can fix them by
adding a const quailifer.

Qu Wenruo (3):
  btrfs-progs: enhance find_option()
  btrfs-progs: constify the @dots variable inside parse_range_u64()
  btrfs-progs: drop the 'const' qualifier from bconf_save_param()

 common/parse-utils.c |  4 ++--
 common/utils.c       | 24 ++++++++++++++++++------
 common/utils.h       |  2 +-
 3 files changed, 21 insertions(+), 9 deletions(-)

--
2.53.0


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH 1/3] btrfs-progs: enhance find_option()
  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
  2026-03-17 16:39   ` David Sterba
  2026-02-22 23:12 ` [PATCH 2/3] btrfs-progs: constify the @dots variable inside parse_range_u64() Qu Wenruo
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 6+ messages in thread
From: Qu Wenruo @ 2026-02-22 23:12 UTC (permalink / raw)
  To: linux-btrfs

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


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH 2/3] btrfs-progs: constify the @dots variable inside parse_range_u64()
  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 ` [PATCH 1/3] btrfs-progs: enhance find_option() Qu Wenruo
@ 2026-02-22 23:12 ` 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
  3 siblings, 0 replies; 6+ messages in thread
From: Qu Wenruo @ 2026-02-22 23:12 UTC (permalink / raw)
  To: linux-btrfs

[WARNING]
The latest compiler (GCC 15.2.1) along with the latest glibc (2.43) will
report the following warning:

 common/parse-utils.c: In function 'parse_range_u64':
 common/parse-utils.c:82:14: warning: assignment discards 'const' qualifier from pointer target type [-Wdiscarded-qualifiers]
    82 |         dots = strstr(range, "..");
       |              ^

[CAUSE]
Although the strstr() definition in man page doesn't have a 'const'
qualifier, 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.

[FIX]
Just add a 'const' qualifier to @dots.

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

diff --git a/common/parse-utils.c b/common/parse-utils.c
index 9e88aa844b44..4fc9ec865572 100644
--- a/common/parse-utils.c
+++ b/common/parse-utils.c
@@ -74,9 +74,9 @@ int parse_u64(const char *str, u64 *result)
  */
 int parse_range_u64(const char *range, u64 *start, u64 *end)
 {
-	char *dots;
-	char *endptr;
+	const char *dots;
 	const char *rest;
+	char *endptr;
 	int skipped = 0;
 
 	dots = strstr(range, "..");
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH 3/3] btrfs-progs: drop the 'const' qualifier from bconf_save_param()
  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 ` [PATCH 1/3] btrfs-progs: enhance find_option() Qu Wenruo
  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 ` 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
  3 siblings, 0 replies; 6+ messages in thread
From: Qu Wenruo @ 2026-02-22 23:13 UTC (permalink / raw)
  To: linux-btrfs

[BUG]
The latest GCC (15.2.1) will report the following warning:

 common/utils.c: In function 'bconf_save_param':
 common/utils.c:975:13: warning: assignment discards 'const' qualifier from pointer target type [-Wdiscarded-qualifiers]
   975 |         tmp = strchr(str, '=');
       |             ^

[CAUSE]
I believe the latest GCC has a more strict const qualifier checks, that
any returned pointer that points into a const pointer should also
have a 'const' qualifier.

But in this particular case, we're even modifying the content of the
original const string.

This means we shouldn't really have 'const' qualifier for
bconf_save_param() in the first place.

[FIX]
Drop the 'const' qualifier from @str parameter, as we're going to modify
the string in that function.

Signed-off-by: Qu Wenruo <wqu@suse.com>
---
 common/utils.c | 2 +-
 common/utils.h | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/common/utils.c b/common/utils.c
index f183705edb8f..59e318282918 100644
--- a/common/utils.c
+++ b/common/utils.c
@@ -968,7 +968,7 @@ const char *bconf_param_value(const char *key)
 	return NULL;
 }
 
-void bconf_save_param(const char *str)
+void bconf_save_param(char *str)
 {
 	char *tmp;
 
diff --git a/common/utils.h b/common/utils.h
index 1419850df387..d0bd062b75a5 100644
--- a/common/utils.h
+++ b/common/utils.h
@@ -118,7 +118,7 @@ void btrfs_config_init(void);
 void bconf_be_verbose(void);
 void bconf_be_quiet(void);
 void bconf_add_param(const char *key, const char *value);
-void bconf_save_param(const char *str);
+void bconf_save_param(char *str);
 void bconf_set_dry_run(void);
 bool bconf_is_dry_run(void);
 const char *bconf_param_value(const char *key);
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH 1/3] btrfs-progs: enhance find_option()
  2026-02-22 23:12 ` [PATCH 1/3] btrfs-progs: enhance find_option() Qu Wenruo
@ 2026-03-17 16:39   ` David Sterba
  0 siblings, 0 replies; 6+ messages in thread
From: David Sterba @ 2026-03-17 16:39 UTC (permalink / raw)
  To: Qu Wenruo; +Cc: linux-btrfs

On Mon, Feb 23, 2026 at 09:42:58AM +1030, Qu Wenruo wrote:
> 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) != ',')

Is there a reason for not using ret[i] ?

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH 0/3] btrfs-progs: fix dropped 'const' qualifiers exposed by the latest GCC (15.2.1)
  2026-02-22 23:12 [PATCH 0/3] btrfs-progs: fix dropped 'const' qualifiers exposed by the latest GCC (15.2.1) Qu Wenruo
                   ` (2 preceding siblings ...)
  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 ` David Sterba
  3 siblings, 0 replies; 6+ messages in thread
From: David Sterba @ 2026-03-17 16:43 UTC (permalink / raw)
  To: Qu Wenruo; +Cc: linux-btrfs

On Mon, Feb 23, 2026 at 09:42:57AM +1030, Qu Wenruo wrote:
> It looks lile the latest gcc has taken the 'const' qualifier checks one
> step further, a char pointer that points into a string that has 'const'
> qualifier should also has 'const'.
> 
> This will expose unexpected modification like the following:
> 
>  void my_func(const char *options)
>  {
>          char *dot;
> 
>          dot = strchr(options, '.');
>          if (!dot)
>                  *dot = '\0';
>  }
> 
> In above example, @dot is either NULL or points to a location inside
> @options. For the later case, since @dot itself is not const, we can
> modify the content, resulting modification of the content of @options.
> The latest GCC is able to detect such proxy modification and gives us
> warning on them.
> 
> And in fact, btrfs-progs has exactly such proxied modification in
> bconf_save_param(), fixed in the last patch by dropping the 'const'
> quailifer.
> 
> Other than that, most are just false alerts and we can fix them by
> adding a const quailifer.
> 
> Qu Wenruo (3):
>   btrfs-progs: enhance find_option()
>   btrfs-progs: constify the @dots variable inside parse_range_u64()
>   btrfs-progs: drop the 'const' qualifier from bconf_save_param()

Added to devel, thanks.

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2026-03-17 16:43 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 ` [PATCH 1/3] btrfs-progs: enhance find_option() Qu Wenruo
2026-03-17 16:39   ` 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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox