btrfs-progs: fix dropped 'const' qualifiers exposed by the latest GCC (15.2.1)
cover btrfs-progs: fix dropped 'const' qualifiers exposed by the latest GCC (15.2.1)
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
PATCH 3/3 btrfs-progs: drop the 'const' qualifier from bconf_save_param()
[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.
--- common/utils.c | 2 +- common/utils.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
common/utils.c +1 -1
@@ -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;
common/utils.h +1 -1
@@ -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);
Signature · 1 line
2.53.0
PATCH 2/3 btrfs-progs: constify the @dots variable inside parse_range_u64()
In reply to Qu Wenruo[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.
--- common/parse-utils.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
common/parse-utils.c +2 -2
@@ -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,".." );
Signature · 1 line
2.53.0
PATCH 1/3 btrfs-progs: enhance find_option()
In reply to Qu WenruoThere 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.
--- common/utils.c | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-)
common/utils.c +17 -5
@@ -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=" );
Signature · 1 line
2.53.0
Re: [PATCH 1/3] btrfs-progs: enhance find_option()
Quoted 55 lines · Qu Wenruo
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()failedEnhance 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 <[email protected]>--- common/utils.c | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-)common/utils.c +7
-1@@ -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)
common/utils.c struct mnt_entry { · line 396
Quoted 10 lines
common/utils.c +4
-4return 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] ?
Quoted 25 lines · Qu Wenruo
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.
Quoted 7 lines
Other than that, most are just false alerts and we can fix them by adding a const quailifer.
Qu Wenruo (3):
btrfs-progs: enhancefind_option()
btrfs-progs: constify the @dots variable insideparse_range_u64()
btrfs-progs: drop the 'const' qualifier frombconf_save_param()
Added to devel, thanks.