policycoreutils/secon: fix discarded-qualifiers warning with glibc 2.43
PATCH 1/1 policycoreutils/secon: fix discarded-qualifiers warning with glibc 2.43
https://github.com/SELinuxProject/selinux/pull/507
In my_getXcon_raw(), ptr is declared as const char * but is assigned from fgets(), which returns char *. With glibc 2.43, strchr(const char *, int) now correctly returns const char * (matching the constness of its input), so the subsequent assignment to char *tmp triggers a build failure:
secon.c: In function 'my_getXcon_raw':
secon.c:365:18: error: initialization discards 'const' qualifier from pointer target type [-Werror=discarded-qualifiers]
365 | char *tmp = strchr(ptr, '\n');
| ^~~~~~
This breaks builds on any system with glibc 2.43+ and gcc with -Werror (which the SELinux Makefile uses).
Fix
The const on ptr was always incorrect:
fgets() returns char * (a mutable pointer into buf, which is char[4096])
The result of strchr() through ptr is immediately used mutably:
*tmp = 0
Remove the erroneous const qualifier from ptr.
Relation to issue #506
This is the same class of bug reported in #506 (libselinux/src/selinux_config.c:284). That issue covers strrchr() in selinux_set_policy_root(); this patch covers strchr() in my_getXcon_raw() in policycoreutils/secon/secon.c.
Both instances have the same root cause: a local variable was unnecessarily const-qualified, which glibc 2.43's strengthened strchr/strrchr const-propagation now correctly rejects.
Testing
Verified by building policycoreutils version 3.10 against glibc 2.43 on a Wolfi-based system (Chainguard) where this was a build failure.
From fe074290a6eff8f12345350ea2938bb0e5e5bc5a Mon Sep 17 00:00:00 2001
From: Dustin Kirkland <[email protected]>
Date: Thu, 12 Mar 2026 09:55:17 -0500
Subject: [PATCH] policycoreutils/secon: fix discarded-qualifiers warning with
glibc 2.43
In my_getXcon_raw(), ptr is declared as const char * but is assigned from fgets(), which returns char *. With glibc 2.43, strchr(const char *, int) now returns const char *, so the subsequent assignment to char *tmp triggers:
secon.c:365:18: error: initialization discards 'const' qualifier from pointer target type [-Werror=discarded-qualifiers]
The const on ptr was always incorrect: fgets() returns a mutable pointer into buf (a char[4096]), and the result of strchr() through ptr is written to via *tmp = 0. Remove the erroneous const.
This is the same class of bug reported in issue #506 (libselinux/src/selinux_config.c:284). The fix here is analogous:
the variable should not have been const-qualified in the first place.
--- policycoreutils/secon/secon.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
policycoreutils/secon/secon.c +0 -0
@@ -348,7 +348,7 @@ static int my_getXcon_raw(pid_t pid, char **con,
const char *val)
{
char buf[4096];
FILE *fp = NULL;
- const char *ptr = NULL;
+ char *ptr = NULL;
snprintf(buf, sizeof(buf), "%s/%ld/attr/%s", "/proc", (long int)pid, val);
PATCH 1/1 policycoreutils/secon: fix discarded-qualifiers warning with glibc 2.43
From: Dustin Kirkland <[email protected]>
In my_getXcon_raw(), ptr is declared as const char * but is assigned from fgets(), which returns char *. With glibc 2.43, strchr(const char *, int) now returns const char *, so the subsequent assignment to char *tmp triggers:
secon.c:365:18: error: initialization discards 'const' qualifier from pointer target type [-Werror=discarded-qualifiers]
The const on ptr was always incorrect: fgets() returns a mutable pointer into buf (a char[4096]), and the result of strchr() through ptr is written to via *tmp = 0. Remove the erroneous const.
This is the same class of bug reported in issue #506 (libselinux/src/selinux_config.c:284). The fix here is analogous:
the variable should not have been const-qualified in the first place.
---
Resent of the original patch downloaded from githu as the original patch in the email was slightly broken.
policycoreutils/secon/secon.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
policycoreutils/secon/secon.c +1 -1
@@ -348,7 +348,7 @@ static int my_getXcon_raw(pid_t pid, char **con, const char *val) {char buf[4096 ];FILE *fp =NULL ; -const char *ptr =NULL ; +char *ptr =NULL ;snprintf (buf,sizeof (buf),"%s/%ld/attr/%s" ,"/proc" , (long int )pid, val);
Signature · 1 line
2.54.0
Quoted 14 lines · Petr Lautrbach
From: Dustin Kirkland <[email protected]>
In
my_getXcon_raw(), ptr is declared as const char * but is assigned fromfgets(), which returns char *. With glibc 2.43,strchr(const char *, int)now returns const char *, so the subsequent assignment to char *tmp triggers:secon.c:365:18: error: initialization discards 'const' qualifier from pointer target type [-Werror=discarded-qualifiers]
The const on ptr was always incorrect:
fgets()returns a mutable pointer into buf (a char[4096]), and the result ofstrchr()through ptr is written to via *tmp = 0. Remove the erroneous const.
Quoted 5 lines
This is the same class of bug reported in issue #506 (
libselinux/src/selinux_config.c:284). The fix here is analogous:
the variable should not have been const-qualified in the first place.Signed-off-by Dustin Kirkland <[email protected]>
Quoted 23 lines
---
Resent of the original patch downloaded from githu as the original patch in the email was slightly broken.
policycoreutils/secon/secon.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)policycoreutils/secon/secon.c +1
-1@@ -348,7 +348,7 @@ static int my_getXcon_raw(pid_t pid, char **con, const char *val) {char buf[4096 ];FILE *fp =NULL ; -const char *ptr =NULL ; +char *ptr =NULL ;snprintf (buf,sizeof (buf),"%s/%ld/attr/%s" ,"/proc" , (long int )pid, val);Signature · 1 line
2.54.0
Quoted 17 lines · Petr Lautrbach
Quoted 14 lines · Petr Lautrbach
From: Dustin Kirkland <[email protected]>
In
my_getXcon_raw(), ptr is declared as const char * but is assigned fromfgets(), which returns char *. With glibc 2.43,strchr(const char *, int)now returns const char *, so the subsequent assignment to char *tmp triggers:secon.c:365:18: error: initialization discards 'const' qualifier from pointer target type [-Werror=discarded-qualifiers]
The const on ptr was always incorrect:
fgets()returns a mutable pointer into buf (a char[4096]), and the result ofstrchr()through ptr is written to via *tmp = 0. Remove the erroneous const.
Quoted 8 lines
Quoted 5 lines
This is the same class of bug reported in issue #506 (
libselinux/src/selinux_config.c:284). The fix here is analogous:
the variable should not have been const-qualified in the first place.Signed-off-by Dustin Kirkland <[email protected]>Acked-by Petr Lautrbach <[email protected]>
Merged.
Thanks,
Jim
Quoted 26 lines
Quoted 24 lines
---
Resent of the original patch downloaded from githu as the original patch in the email was slightly broken.
policycoreutils/secon/secon.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)policycoreutils/secon/secon.c +1
-2@@ -348,7 +348,7 @@ static int my_getXcon_raw(pid_t pid, char **con, const char *val) {char buf[4096 ];FILE *fp =NULL ; -const char *ptr =NULL ; +char *ptr =NULL ;snprintf (buf,sizeof (buf),"%s/%ld/attr/%s" ,"/proc" , (long int )pid, val); --2.54.0