Skip to contentSkip to conversation

log: Fix result of strstr to 'const char *'

PATCH Superseded Cédric Le Goater 4 participants 6 messages Dec 9, 2025

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 log: Fix result of strstr to 'const char *'

Assigning the result of strstr() to a 'char *' is unsafe since strstr() returns a pointer into the original string which is a read-only 'const char *' string. Newer compilers now complain when the result of strstr() is not a 'const char *' :

../util/log.c:208:24: error: initialization discards ‘const’ qualifier from pointer target type [-Werror=discarded-qualifiers]
208 | char *pidstr = strstr(filename, "%");
| ^~~~~~

Fix that.

---
 util/log.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
util/log.c +1 -1
index 41f78ce86b2522b8b7072c8b76d8e18603142db6..c44d66b5ce78338cf1b2cd26b7503cb94d4570cb 100644
@@ -203,7 +203,7 @@ static ValidFilenameTemplateResult
 valid_filename_template(const char *filename, bool per_thread, Error **errp)
 {
     if (filename) {
-        char *pidstr = strstr(filename, "%");
+        const char *pidstr = strstr(filename, "%");
 
         if (pidstr) {
             /* We only accept one %d, no other format strings */
Signature · 1 line
2.52.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 4 lines · Cédric Le Goater

Assigning the result of strstr() to a 'char *' is unsafe since strstr() returns a pointer into the original string which is a read-only 'const char *' string. Newer compilers

Which ones? Or does this depend on how the libc headers have marked up the strstr() prototype?

Quoted 19 lines

now complain when the
result of strstr() is not a 'const char *' :

../util/log.c:208:24: error: initialization discards ‘const’ qualifier from pointer target type [-Werror=discarded-qualifiers]
208 | char *pidstr = strstr(filename, "%");
| ^~~~~~

Fix that.

---
 util/log.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
util/log.c +0 -0
index 41f78ce86b2522b8b7072c8b76d8e18603142db6..c44d66b5ce78338cf1b2cd26b7503cb94d4570cb 100644

util/log.c static ValidFilenameTemplateResult

Quoted 9 lines
util/log.c +1 -2
@@ -203,7 +203,7 @@ static ValidFilenameTemplateResult
 valid_filename_template(const char *filename, bool per_thread, Error **errp)
 {
     if (filename) {
-        char *pidstr = strstr(filename, "%");
+        const char *pidstr = strstr(filename, "%");
 
         if (pidstr) {
             /* We only accept one %d, no other format strings */
--

thanks
-- PMM

#2 lore raw
Quoted 8 lines · Peter Maydell
Quoted 4 lines · Cédric Le Goater

Assigning the result of strstr() to a 'char *' is unsafe since strstr() returns a pointer into the original string which is a read-only 'const char *' string. Newer compilers

Which ones? Or does this depend on how the libc headers have marked up the strstr() prototype?

Right. This is a new issue and I am not sure why.

It's showing on :

gcc version 15.2.1 20251111 (Red Hat 15.2.1-4) (GCC) glibc-devel-2.42.9000-14.fc44.x86_64

and <string.h> doesn't seem to have change. Both strstr() prototypes are available.

Let me check.

C.

Quoted 35 lines
Quoted 20 lines

now complain when the
result of strstr() is not a 'const char *' :

../util/log.c:208:24: error: initialization discards ‘const’ qualifier from pointer target type [-Werror=discarded-qualifiers]
208 | char *pidstr = strstr(filename, "%");
| ^~~~~~

Fix that.

---
  util/log.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
util/log.c +0 -0
index 41f78ce86b2522b8b7072c8b76d8e18603142db6..c44d66b5ce78338cf1b2cd26b7503cb94d4570cb 100644

util/log.c static ValidFilenameTemplateResult

Quoted 9 lines
util/log.c +1 -2
@@ -203,7 +203,7 @@ static ValidFilenameTemplateResult
  valid_filename_template(const char *filename, bool per_thread, Error **errp)
  {
      if (filename) {
-        char *pidstr = strstr(filename, "%");
+        const char *pidstr = strstr(filename, "%");
 
          if (pidstr) {
              /* We only accept one %d, no other format strings */
--

thanks
-- PMM

#3 lore raw
In reply to Peter Maydell
Quoted 8 lines · Peter Maydell
Quoted 4 lines · Cédric Le Goater

Assigning the result of strstr() to a 'char *' is unsafe since strstr() returns a pointer into the original string which is a read-only 'const char *' string. Newer compilers

Which ones? Or does this depend on how the libc headers have marked up the strstr() prototype?

I don't believe it is compiler related, rather this is an ehancement in glibc 2.42.9000 / git master

https://sourceware.org/git/?p=glibc.git;a=commit;h=cd748a63ab1a7ae846175c532a3daab341c62690

AFAICT it should work with any gcc we have

Although the commit talks about C23, we get it regardless as we have _GNU_SOURCE defined.

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#4 lore raw
Quoted 13 lines · Daniel P. Berrangé
Quoted 8 lines · Peter Maydell
Quoted 4 lines · Cédric Le Goater

Assigning the result of strstr() to a 'char *' is unsafe since strstr() returns a pointer into the original string which is a read-only 'const char *' string. Newer compilers

Which ones? Or does this depend on how the libc headers have marked up the strstr() prototype?

I don't believe it is compiler related, rather this is an ehancement in glibc 2.42.9000 / git master

Quoted 6 lines
   https://sourceware.org/git/?p=glibc.git;a=commit;h=cd748a63ab1a7ae846175c532a3daab341c62690

AFAICT it should work with any gcc we have

Although the commit talks about C23, we get it regardless as we have _GNU_SOURCE defined.

yes. glibc 2.42.9000 has :

+# if __GLIBC_USE (ISOC23) && defined __glibc_const_generic && !defined _LIBC
+# define strstr(HAYSTACK, NEEDLE) \
+ __glibc_const_generic (HAYSTACK, const char *, \
+ strstr (HAYSTACK, NEEDLE))
+# endif

C.

Reviewed-by on v1 1/1
#5 lore raw
In reply to Cédric Le Goater
Quoted 21 lines · Cédric Le Goater

Assigning the result of strstr() to a 'char *' is unsafe since strstr() returns a pointer into the original string which is a read-only 'const char *' string. Newer compilers now complain when the result of strstr() is not a 'const char *' :

../util/log.c:208:24: error: initialization discards 'const' qualifier from pointer target type [-Werror=discarded-qualifiers]
208 | char *pidstr = strstr(filename, "%");
| ^~~~~~

Fix that.

---
 util/log.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
util/log.c +0 -0
index 41f78ce86b2522b8b7072c8b76d8e18603142db6..c44d66b5ce78338cf1b2cd26b7503cb94d4570cb 100644

util/log.c static ValidFilenameTemplateResult · line 209

Quoted 8 lines
util/log.c +1 -1
@@ -203,7 +203,7 @@ static ValidFilenameTemplateResult
 valid_filename_template(const char *filename, bool per_thread, Error **errp)
 {
     if (filename) {
-        char *pidstr = strstr(filename, "%");
+        const char *pidstr = strstr(filename, "%");
 
         if (pidstr) {
             /* We only accept one %d, no other format strings */