[v1,1/6] target_fileio_read_stralloc: add an optional length parameter

Message ID 20260728151700.253720-2-matthieu.longo@arm.com
State New
Headers
Series gdb: introduce file_reader_t to read procfs files |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gdb_build--master-arm fail Patch failed to apply
linaro-tcwg-bot/tcwg_gdb_build--master-aarch64 fail Patch failed to apply

Commit Message

Matthieu Longo July 28, 2026, 3:16 p.m. UTC
  Extend target_fileio_read_stralloc with an optional output parameter
that returns the number of bytes read, excluding the terminating NUL
byte.

Most callers only need the returned NUL-terminated buffer and can
ignore the new parameter, but callers that need the number of bytes
read can now obtain it without recomputing it.

While updating the function, make a couple of minor cleanups by using
'\0' instead of 0 for character literals and clarifying the function
documentation.
---
 gdb/target.c | 11 ++++++++---
 gdb/target.h | 20 ++++++++++++--------
 2 files changed, 20 insertions(+), 11 deletions(-)
  

Comments

Thiago Jung Bauermann Aug. 3, 2026, 4:49 a.m. UTC | #1
Matthieu Longo <matthieu.longo@arm.com> writes:

> Extend target_fileio_read_stralloc with an optional output parameter
> that returns the number of bytes read, excluding the terminating NUL
> byte.
>
> Most callers only need the returned NUL-terminated buffer and can
> ignore the new parameter, but callers that need the number of bytes
> read can now obtain it without recomputing it.
>
> While updating the function, make a couple of minor cleanups by using
> '\0' instead of 0 for character literals and clarifying the function
> documentation.
> ---
>  gdb/target.c | 11 ++++++++---
>  gdb/target.h | 20 ++++++++++++--------
>  2 files changed, 20 insertions(+), 11 deletions(-)

Reviewed-by: Thiago Jung Bauermann <thiago.bauermann@linaro.org>
  

Patch

diff --git a/gdb/target.c b/gdb/target.c
index 5d937f3ae85..ebf0f30092c 100644
--- a/gdb/target.c
+++ b/gdb/target.c
@@ -3547,7 +3547,8 @@  target_fileio_read_alloc (struct inferior *inf, const char *filename,
 /* See target.h.  */
 
 gdb::unique_xmalloc_ptr<char>
-target_fileio_read_stralloc (struct inferior *inf, const char *filename)
+target_fileio_read_stralloc (struct inferior *inf, const char *filename,
+			     LONGEST *len)
 {
   gdb_byte *buffer;
   char *bufstr;
@@ -3556,17 +3557,21 @@  target_fileio_read_stralloc (struct inferior *inf, const char *filename)
   transferred = target_fileio_read_alloc_1 (inf, filename, &buffer, 1);
   bufstr = (char *) buffer;
 
+  /* Note: on failure, target_fileio_read_alloc_1 returns -1.  */
+  if (len != nullptr)
+    *len = transferred;
+
   if (transferred < 0)
     return gdb::unique_xmalloc_ptr<char> (nullptr);
 
   if (transferred == 0)
     return make_unique_xstrdup ("");
 
-  bufstr[transferred] = 0;
+  bufstr[transferred] = '\0';
 
   /* Check for embedded NUL bytes; but allow trailing NULs.  */
   for (i = strlen (bufstr); i < transferred; i++)
-    if (bufstr[i] != 0)
+    if (bufstr[i] != '\0')
       {
 	warning (_("target file %s "
 		   "contained unexpected null characters"),
diff --git a/gdb/target.h b/gdb/target.h
index 6d1c21f29f6..819279c08fc 100644
--- a/gdb/target.h
+++ b/gdb/target.h
@@ -2327,15 +2327,19 @@  extern LONGEST target_fileio_read_alloc (struct inferior *inf,
 					 const char *filename,
 					 gdb_byte **buf_p);
 
-/* Read target file FILENAME, in the filesystem as seen by INF.  If
-   INF is NULL, use the filesystem seen by the debugger (GDB or, for
-   remote targets, the remote stub).  The result is NUL-terminated and
-   returned as a string, allocated using xmalloc.  If an error occurs
-   or the transfer is unsupported, NULL is returned.  Empty objects
-   are returned as allocated but empty strings.  A warning is issued
-   if the result contains any embedded NUL bytes.  */
+/* Read the content of the target file FILENAME from the filesystem as
+   seen by INF.  If INF is NULL, use the filesystem seen by the debugger
+   (GDB or, for remote targets, the remote stub).
+
+   If LEN is not NULL, store the number of bytes read, excluding the
+   terminating NUL byte.
+
+   The returned buffer is NUL-terminated and allocated using xmalloc.
+   On error, or if the transfer is unsupported, return NULL and set
+   LEN to -1.  Empty files are returned as allocated but empty strings.
+   A warning is issued if the file content contains embedded NUL bytes.  */
 extern gdb::unique_xmalloc_ptr<char> target_fileio_read_stralloc
-    (struct inferior *inf, const char *filename);
+    (struct inferior *inf, const char *filename, LONGEST *len = nullptr);
 
 /* Invalidate the target associated with open handles that were open
    on target TARG, since we're about to close (and maybe destroy) the