[PATCH v5 13/25] Convert char array to std::string in linux_find_memory_regions_full

Luis Machado via Gdb-patches
2021-01-27T20:21:48Z

This is a quick cleanup that removes the use of fixed-length char arrays and
uses std::string instead.

gdb/ChangeLog:

YYYY-MM-DD Luis Machado <luis.machado@linaro.org>

* linux-tdep.c (linux_find_memory_regions_full): Use std::string
instead of char arrays.
---
gdb/linux-tdep.c | 25 ++++++++++++++-----------
1 file changed, 14 insertions(+), 11 deletions(-)

diff --git a/gdb/linux-tdep.c b/gdb/linux-tdep.c
index e9f8e1b613..35e05e12a6 100644
--- a/gdb/linux-tdep.c
+++ b/gdb/linux-tdep.c
@@ -1277,8 +1277,6 @@ linux_find_memory_regions_full (struct gdbarch *gdbarch,
linux_find_memory_region_ftype *func,
void *obfd)
{
- char mapsfilename[100];
- char coredumpfilter_name[100];
pid_t pid;
/* Default dump behavior of coredump_filter (0x33), according to
Documentation/filesystems/proc.txt from the Linux kernel
@@ -1296,10 +1294,12 @@ linux_find_memory_regions_full (struct gdbarch *gdbarch,

if (use_coredump_filter)
{
- xsnprintf (coredumpfilter_name, sizeof (coredumpfilter_name),
- "/proc/%d/coredump_filter", pid);
+ std::string core_dump_filter_name
+ = string_printf ("/proc/%d/coredump_filter", pid);
+
gdb::unique_xmalloc_ptr<char> coredumpfilterdata
- = target_fileio_read_stralloc (NULL, coredumpfilter_name);
+ = target_fileio_read_stralloc (NULL, core_dump_filter_name.c_str());
+
if (coredumpfilterdata != NULL)
{
unsigned int flags;
@@ -1309,14 +1309,16 @@ linux_find_memory_regions_full (struct gdbarch *gdbarch,
}
}

- xsnprintf (mapsfilename, sizeof mapsfilename, "/proc/%d/smaps", pid);
+ std::string maps_filename = string_printf ("/proc/%d/smaps", pid);
+
gdb::unique_xmalloc_ptr<char> data
- = target_fileio_read_stralloc (NULL, mapsfilename);
+ = target_fileio_read_stralloc (NULL, maps_filename.c_str ());
+
if (data == NULL)
{
/* Older Linux kernels did not support /proc/PID/smaps. */
- xsnprintf (mapsfilename, sizeof mapsfilename, "/proc/%d/maps", pid);
- data = target_fileio_read_stralloc (NULL, mapsfilename);
+ maps_filename = string_printf ("/proc/%d/maps", pid);
+ data = target_fileio_read_stralloc (NULL, maps_filename.c_str ());
}

if (data != NULL)
@@ -1376,7 +1378,8 @@ linux_find_memory_regions_full (struct gdbarch *gdbarch,

if (sscanf (line, "%64s", keyword) != 1)
{
- warning (_("Error parsing {s,}maps file '%s'"), mapsfilename);
+ warning (_("Error parsing {s,}maps file '%s'"),
+ maps_filename.c_str ());
break;
}

@@ -1397,7 +1400,7 @@ linux_find_memory_regions_full (struct gdbarch *gdbarch,
if (sscanf (line, "%*s%lu", &number) != 1)
{
warning (_("Error parsing {s,}maps file '%s' number"),
- mapsfilename);
+ maps_filename.c_str ());
break;
}
if (number > 0)
--
2.25.1