qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] log: Fix result of strstr to 'const char *'
@ 2025-12-09 15:03 Cédric Le Goater
  2025-12-09 15:07 ` Peter Maydell
  2025-12-09 15:10 ` Laurent Vivier
  0 siblings, 2 replies; 6+ messages in thread
From: Cédric Le Goater @ 2025-12-09 15:03 UTC (permalink / raw)
  To: qemu-devel
  Cc: Richard Henderson, Philippe Mathieu-Daudé, Markus Armbruster,
	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.

Signed-off-by: Cédric Le Goater <clg@redhat.com>
---
 util/log.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/util/log.c b/util/log.c
index 41f78ce86b2522b8b7072c8b76d8e18603142db6..c44d66b5ce78338cf1b2cd26b7503cb94d4570cb 100644
--- a/util/log.c
+++ b/util/log.c
@@ -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 */
-- 
2.52.0



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

* Re: [PATCH] log: Fix result of strstr to 'const char *'
  2025-12-09 15:03 [PATCH] log: Fix result of strstr to 'const char *' Cédric Le Goater
@ 2025-12-09 15:07 ` Peter Maydell
  2025-12-09 15:14   ` Cédric Le Goater
  2025-12-09 15:21   ` Daniel P. Berrangé
  2025-12-09 15:10 ` Laurent Vivier
  1 sibling, 2 replies; 6+ messages in thread
From: Peter Maydell @ 2025-12-09 15:07 UTC (permalink / raw)
  To: Cédric Le Goater
  Cc: qemu-devel, Richard Henderson, Philippe Mathieu-Daudé,
	Markus Armbruster

On Tue, 9 Dec 2025 at 15:04, Cédric Le Goater <clg@redhat.com> wrote:
>
> 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?

> 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.
>
> Signed-off-by: Cédric Le Goater <clg@redhat.com>
> ---
>  util/log.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/util/log.c b/util/log.c
> index 41f78ce86b2522b8b7072c8b76d8e18603142db6..c44d66b5ce78338cf1b2cd26b7503cb94d4570cb 100644
> --- a/util/log.c
> +++ b/util/log.c
> @@ -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 */
> --

Reviewed-by: Peter Maydell <peter.maydell@linaro.org>

thanks
-- PMM


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

* Re: [PATCH] log: Fix result of strstr to 'const char *'
  2025-12-09 15:03 [PATCH] log: Fix result of strstr to 'const char *' Cédric Le Goater
  2025-12-09 15:07 ` Peter Maydell
@ 2025-12-09 15:10 ` Laurent Vivier
  1 sibling, 0 replies; 6+ messages in thread
From: Laurent Vivier @ 2025-12-09 15:10 UTC (permalink / raw)
  To: Cédric Le Goater
  Cc: qemu-devel, Richard Henderson, Philippe Mathieu-Daudé,
	Markus Armbruster

On 12/9/25 16:03, Cédric Le Goater wrote:
> 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.
> 
> Signed-off-by: Cédric Le Goater <clg@redhat.com>
> ---
>  util/log.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/util/log.c b/util/log.c
> index 41f78ce86b2522b8b7072c8b76d8e18603142db6..c44d66b5ce78338cf1b2cd26b7503cb94d4570cb 100644
> --- a/util/log.c
> +++ b/util/log.c
> @@ -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 */

Reviewed-by: Laurent Vivier <laurent@vivier.eu>


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

* Re: [PATCH] log: Fix result of strstr to 'const char *'
  2025-12-09 15:07 ` Peter Maydell
@ 2025-12-09 15:14   ` Cédric Le Goater
  2025-12-09 15:21   ` Daniel P. Berrangé
  1 sibling, 0 replies; 6+ messages in thread
From: Cédric Le Goater @ 2025-12-09 15:14 UTC (permalink / raw)
  To: Peter Maydell
  Cc: qemu-devel, Richard Henderson, Philippe Mathieu-Daudé,
	Markus Armbruster

On 12/9/25 16:07, Peter Maydell wrote:
> On Tue, 9 Dec 2025 at 15:04, Cédric Le Goater <clg@redhat.com> wrote:
>>
>> 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.


> 
>> 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.
>>
>> Signed-off-by: Cédric Le Goater <clg@redhat.com>
>> ---
>>   util/log.c | 2 +-
>>   1 file changed, 1 insertion(+), 1 deletion(-)
>>
>> diff --git a/util/log.c b/util/log.c
>> index 41f78ce86b2522b8b7072c8b76d8e18603142db6..c44d66b5ce78338cf1b2cd26b7503cb94d4570cb 100644
>> --- a/util/log.c
>> +++ b/util/log.c
>> @@ -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 */
>> --
> 
> Reviewed-by: Peter Maydell <peter.maydell@linaro.org>
> 
> thanks
> -- PMM
> 



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

* Re: [PATCH] log: Fix result of strstr to 'const char *'
  2025-12-09 15:07 ` Peter Maydell
  2025-12-09 15:14   ` Cédric Le Goater
@ 2025-12-09 15:21   ` Daniel P. Berrangé
  2025-12-09 15:22     ` Cédric Le Goater
  1 sibling, 1 reply; 6+ messages in thread
From: Daniel P. Berrangé @ 2025-12-09 15:21 UTC (permalink / raw)
  To: Peter Maydell
  Cc: Cédric Le Goater, qemu-devel, Richard Henderson,
	Philippe Mathieu-Daudé, Markus Armbruster

On Tue, Dec 09, 2025 at 03:07:04PM +0000, Peter Maydell wrote:
> On Tue, 9 Dec 2025 at 15:04, Cédric Le Goater <clg@redhat.com> wrote:
> >
> > 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
-- 
|: 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 :|



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

* Re: [PATCH] log: Fix result of strstr to 'const char *'
  2025-12-09 15:21   ` Daniel P. Berrangé
@ 2025-12-09 15:22     ` Cédric Le Goater
  0 siblings, 0 replies; 6+ messages in thread
From: Cédric Le Goater @ 2025-12-09 15:22 UTC (permalink / raw)
  To: Daniel P. Berrangé, Peter Maydell
  Cc: qemu-devel, Richard Henderson, Philippe Mathieu-Daudé,
	Markus Armbruster

On 12/9/25 16:21, Daniel P. Berrangé wrote:
> On Tue, Dec 09, 2025 at 03:07:04PM +0000, Peter Maydell wrote:
>> On Tue, 9 Dec 2025 at 15:04, Cédric Le Goater <clg@redhat.com> wrote:
>>>
>>> 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.

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.



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

end of thread, other threads:[~2025-12-09 15:22 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-12-09 15:03 [PATCH] log: Fix result of strstr to 'const char *' Cédric Le Goater
2025-12-09 15:07 ` Peter Maydell
2025-12-09 15:14   ` Cédric Le Goater
2025-12-09 15:21   ` Daniel P. Berrangé
2025-12-09 15:22     ` Cédric Le Goater
2025-12-09 15:10 ` Laurent Vivier

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).