[PATCH] gdb: Fix "target file /proc/.../cmdline contained unexpected null characters"

Ilya Leoshkevich iii@linux.ibm.com
Wed Jun 21 23:14:25 GMT 2023


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().
---
 gdb/linux-tdep.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/gdb/linux-tdep.c b/gdb/linux-tdep.c
index b5eee5e108c..96cbe8e5520 100644
--- a/gdb/linux-tdep.c
+++ b/gdb/linux-tdep.c
@@ -1902,15 +1902,22 @@ 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);
+    }
 
   memset (p, 0, sizeof (*p));
 
-- 
2.40.1



More information about the Gdb-patches mailing list