public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH v2] gdb: Fix "target file /proc/.../cmdline contained unexpected null characters"
@ 2023-07-19 11:27 Ilya Leoshkevich
  2023-07-19 16:07 ` Bruno Larsen
  0 siblings, 1 reply; 2+ messages in thread
From: Ilya Leoshkevich @ 2023-07-19 11:27 UTC (permalink / raw)
  To: Tom Tromey, Bruno Larsen; +Cc: gdb-patches, Ilya Leoshkevich

When using the gcore command, GDB prints the following warning:

    (gdb) gcore
    warning: target file /proc/.../cmdline contained unexpected null characters

The reason is that cmdline is read with target_fileio_read_stralloc(),
which warns on seeing null characters. However, it's perfectly valid
for cmdline to contain \0s, so switch to target_fileio_read_alloc().
---

v1: https://sourceware.org/pipermail/gdb-patches/2023-June/200436.html
    https://sourceware.org/pipermail/gdb-patches/2023-July/200993.html

v1 -> v2: Improve the commit message (Bruno).
          Handle the missing trailing '\0' (Bruno).

 gdb/linux-tdep.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

diff --git a/gdb/linux-tdep.c b/gdb/linux-tdep.c
index b5eee5e108c..90ad02310b3 100644
--- a/gdb/linux-tdep.c
+++ b/gdb/linux-tdep.c
@@ -1902,15 +1902,23 @@ linux_fill_prpsinfo (struct elf_internal_linux_prpsinfo *p)
   pid = inferior_ptid.pid ();
   xsnprintf (filename, sizeof (filename), "/proc/%d/cmdline", (int) pid);
   /* The full name of the program which generated the corefile.  */
-  gdb::unique_xmalloc_ptr<char> fname
-    = target_fileio_read_stralloc (NULL, filename);
+  gdb_byte *buf = NULL;
+  size_t buf_len = target_fileio_read_alloc (NULL, filename, &buf);
+  gdb::unique_xmalloc_ptr<char> fname ((char *)buf);
 
-  if (fname == NULL || fname.get ()[0] == '\0')
+  if (buf_len < 1 || fname.get ()[0] == '\0')
     {
       /* No program name was read, so we won't be able to retrieve more
 	 information about the process.  */
       return 0;
     }
+  if (fname.get ()[buf_len - 1] != '\0')
+    {
+      warning (_("target file %s "
+		 "does not contain a trailing null character"),
+	       filename);
+      return 0;
+    }
 
   memset (p, 0, sizeof (*p));
 
-- 
2.41.0


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

* Re: [PATCH v2] gdb: Fix "target file /proc/.../cmdline contained unexpected null characters"
  2023-07-19 11:27 [PATCH v2] gdb: Fix "target file /proc/.../cmdline contained unexpected null characters" Ilya Leoshkevich
@ 2023-07-19 16:07 ` Bruno Larsen
  0 siblings, 0 replies; 2+ messages in thread
From: Bruno Larsen @ 2023-07-19 16:07 UTC (permalink / raw)
  To: Ilya Leoshkevich, Tom Tromey; +Cc: gdb-patches

On 19/07/2023 13:27, Ilya Leoshkevich wrote:
> When using the gcore command, GDB prints the following warning:
>
>      (gdb) gcore
>      warning: target file /proc/.../cmdline contained unexpected null characters
>
> The reason is that cmdline is read with target_fileio_read_stralloc(),
> which warns on seeing null characters. However, it's perfectly valid
> for cmdline to contain \0s, so switch to target_fileio_read_alloc().
> ---

Looks good to me now :)

Reviewed-By: Bruno Larsen <blarsen@redhat.com>

I hope a maintainer can approve this soon!

-- 
Cheers,
Bruno

> v1: https://sourceware.org/pipermail/gdb-patches/2023-June/200436.html
>      https://sourceware.org/pipermail/gdb-patches/2023-July/200993.html
>
> v1 -> v2: Improve the commit message (Bruno).
>            Handle the missing trailing '\0' (Bruno).
>
>   gdb/linux-tdep.c | 14 +++++++++++---
>   1 file changed, 11 insertions(+), 3 deletions(-)
>
> diff --git a/gdb/linux-tdep.c b/gdb/linux-tdep.c
> index b5eee5e108c..90ad02310b3 100644
> --- a/gdb/linux-tdep.c
> +++ b/gdb/linux-tdep.c
> @@ -1902,15 +1902,23 @@ linux_fill_prpsinfo (struct elf_internal_linux_prpsinfo *p)
>     pid = inferior_ptid.pid ();
>     xsnprintf (filename, sizeof (filename), "/proc/%d/cmdline", (int) pid);
>     /* The full name of the program which generated the corefile.  */
> -  gdb::unique_xmalloc_ptr<char> fname
> -    = target_fileio_read_stralloc (NULL, filename);
> +  gdb_byte *buf = NULL;
> +  size_t buf_len = target_fileio_read_alloc (NULL, filename, &buf);
> +  gdb::unique_xmalloc_ptr<char> fname ((char *)buf);
>   
> -  if (fname == NULL || fname.get ()[0] == '\0')
> +  if (buf_len < 1 || fname.get ()[0] == '\0')
>       {
>         /* No program name was read, so we won't be able to retrieve more
>   	 information about the process.  */
>         return 0;
>       }
> +  if (fname.get ()[buf_len - 1] != '\0')
> +    {
> +      warning (_("target file %s "
> +		 "does not contain a trailing null character"),
> +	       filename);
> +      return 0;
> +    }
>   
>     memset (p, 0, sizeof (*p));
>   


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

end of thread, other threads:[~2023-07-19 16:07 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-07-19 11:27 [PATCH v2] gdb: Fix "target file /proc/.../cmdline contained unexpected null characters" Ilya Leoshkevich
2023-07-19 16:07 ` Bruno Larsen

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 read-only IMAP folder(s) and NNTP newsgroup(s).