diff mbox

Change in binutils-gdb[master]: Change gcc_target_options to return std::string

Message ID gerrit.1571159149000.I51f61703426a323089e646da8f22320a2cafbc1f@gnutoolchain-gerrit.osci.io
State New
Headers show

Commit Message

Simon Marchi (Code Review) Oct. 15, 2019, 5:05 p.m. UTC
Tom Tromey has uploaded a new change for review.

Change URL: https://gnutoolchain-gerrit.osci.io/r/c/binutils-gdb/+/83
......................................................................

Change gcc_target_options to return std::string

This patch was inspired by a recent review that recommended using
std::string in a new implementation of the gcc_target_options gdbarch
function.  It changes this function to return std::string rather than
an ordinary xmalloc'd string.

I believe this caught a latent memory leak in compile.c:get_args.

Tested on x86-64 Fedora 29.

gdb/ChangeLog
2019-10-15  Tom Tromey  <tromey@adacore.com>

	* gdbarch.h, gdbarch.c: Rebuild.
	* gdbarch.sh (gcc_target_options): Change return type to
	std::string.
	* compile/compile.c (get_args): Update.
	* nios2-tdep.c (nios2_gcc_target_options): Return std::string.
	* arm-linux-tdep.c (arm_linux_gcc_target_options): Return
	std::string.
	* aarch64-linux-tdep.c (aarch64_linux_gcc_target_options): Return
	std::string.
	* arch-utils.c (default_gcc_target_options): Return std::string.
	* arch-utils.h (default_gcc_target_options): Return std::string.
	* s390-tdep.c (s390_gcc_target_options): Return std::string.

Change-Id: I51f61703426a323089e646da8f22320a2cafbc1f
---
M gdb/ChangeLog
M gdb/aarch64-linux-tdep.c
M gdb/arch-utils.c
M gdb/arch-utils.h
M gdb/arm-linux-tdep.c
M gdb/compile/compile.c
M gdb/gdbarch.c
M gdb/gdbarch.h
M gdb/gdbarch.sh
M gdb/nios2-tdep.c
M gdb/s390-tdep.c
11 files changed, 35 insertions(+), 19 deletions(-)

Comments

Simon Marchi (Code Review) Oct. 15, 2019, 5:23 p.m. UTC | #1
Andrew Burgess has posted comments on this change.

Change URL: https://gnutoolchain-gerrit.osci.io/r/c/binutils-gdb/+/83
......................................................................


Patch Set 1: Code-Review+2

Looks good.
diff mbox

Patch

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index 9d3ec1f..e52ec3f 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,3 +1,18 @@ 
+2019-10-15  Tom Tromey  <tromey@adacore.com>
+
+	* gdbarch.h, gdbarch.c: Rebuild.
+	* gdbarch.sh (gcc_target_options): Change return type to
+	std::string.
+	* compile/compile.c (get_args): Update.
+	* nios2-tdep.c (nios2_gcc_target_options): Return std::string.
+	* arm-linux-tdep.c (arm_linux_gcc_target_options): Return
+	std::string.
+	* aarch64-linux-tdep.c (aarch64_linux_gcc_target_options): Return
+	std::string.
+	* arch-utils.c (default_gcc_target_options): Return std::string.
+	* arch-utils.h (default_gcc_target_options): Return std::string.
+	* s390-tdep.c (s390_gcc_target_options): Return std::string.
+
 2019-10-15  Christian Biesinger  <cbiesinger@google.com>
 
 	* breakpoint.c (breakpoint_chain): Make static.
diff --git a/gdb/aarch64-linux-tdep.c b/gdb/aarch64-linux-tdep.c
index a375c3b..3ec08d1 100644
--- a/gdb/aarch64-linux-tdep.c
+++ b/gdb/aarch64-linux-tdep.c
@@ -1429,11 +1429,11 @@ 
 
 /* Implement the "gcc_target_options" gdbarch method.  */
 
-static char *
+static std::string
 aarch64_linux_gcc_target_options (struct gdbarch *gdbarch)
 {
   /* GCC doesn't know "-m64".  */
-  return NULL;
+  return {};
 }
 
 static void
diff --git a/gdb/arch-utils.c b/gdb/arch-utils.c
index c61fa6f..571646e 100644
--- a/gdb/arch-utils.c
+++ b/gdb/arch-utils.c
@@ -903,11 +903,12 @@ 
 /* -mcmodel=large is used so that no GOT (Global Offset Table) is needed to be
    created in inferior memory by GDB (normally it is set by ld.so).  */
 
-char *
+std::string
 default_gcc_target_options (struct gdbarch *gdbarch)
 {
-  return xstrprintf ("-m%d%s", gdbarch_ptr_bit (gdbarch),
-		     gdbarch_ptr_bit (gdbarch) == 64 ? " -mcmodel=large" : "");
+  return string_printf ("-m%d%s", gdbarch_ptr_bit (gdbarch),
+			(gdbarch_ptr_bit (gdbarch) == 64
+			 ? " -mcmodel=large" : ""));
 }
 
 /* gdbarch gnu_triplet_regexp method.  */
diff --git a/gdb/arch-utils.h b/gdb/arch-utils.h
index e5bbcd1..48ff3bb 100644
--- a/gdb/arch-utils.h
+++ b/gdb/arch-utils.h
@@ -247,7 +247,7 @@ 
 
 extern CORE_ADDR default_infcall_mmap (CORE_ADDR size, unsigned prot);
 extern void default_infcall_munmap (CORE_ADDR addr, CORE_ADDR size);
-extern char *default_gcc_target_options (struct gdbarch *gdbarch);
+extern std::string default_gcc_target_options (struct gdbarch *gdbarch);
 extern const char *default_gnu_triplet_regexp (struct gdbarch *gdbarch);
 extern int default_addressable_memory_unit_size (struct gdbarch *gdbarch);
 
diff --git a/gdb/arm-linux-tdep.c b/gdb/arm-linux-tdep.c
index 54ea851..32c1ef3 100644
--- a/gdb/arm-linux-tdep.c
+++ b/gdb/arm-linux-tdep.c
@@ -1710,11 +1710,11 @@ 
 
 /* Implement the gcc_target_options gdbarch method.  */
 
-static char *
+static std::string
 arm_linux_gcc_target_options (struct gdbarch *gdbarch)
 {
   /* GCC doesn't know "-m32".  */
-  return NULL;
+  return {};
 }
 
 static void
diff --git a/gdb/compile/compile.c b/gdb/compile/compile.c
index 5c3400f..94942db 100644
--- a/gdb/compile/compile.c
+++ b/gdb/compile/compile.c
@@ -635,7 +635,7 @@ 
   int argc_compiler;
   char **argv_compiler;
 
-  build_argc_argv (gdbarch_gcc_target_options (gdbarch),
+  build_argc_argv (gdbarch_gcc_target_options (gdbarch).c_str (),
 		   argcp, argvp);
 
   cs_producer_options = get_selected_pc_producer_options ();
diff --git a/gdb/gdbarch.c b/gdb/gdbarch.c
index 7b93d00..fa6be50 100644
--- a/gdb/gdbarch.c
+++ b/gdb/gdbarch.c
@@ -5043,7 +5043,7 @@ 
   gdbarch->infcall_munmap = infcall_munmap;
 }
 
-char *
+std::string
 gdbarch_gcc_target_options (struct gdbarch *gdbarch)
 {
   gdb_assert (gdbarch != NULL);
diff --git a/gdb/gdbarch.h b/gdb/gdbarch.h
index 3c6efae..01b5aef 100644
--- a/gdb/gdbarch.h
+++ b/gdb/gdbarch.h
@@ -1596,10 +1596,10 @@ 
 /* Return string (caller has to use xfree for it) with options for GCC
    to produce code for this target, typically "-m64", "-m32" or "-m31".
    These options are put before CU's DW_AT_producer compilation options so that
-   they can override it.  Method may also return NULL. */
+   they can override it. */
 
-typedef char * (gdbarch_gcc_target_options_ftype) (struct gdbarch *gdbarch);
-extern char * gdbarch_gcc_target_options (struct gdbarch *gdbarch);
+typedef std::string (gdbarch_gcc_target_options_ftype) (struct gdbarch *gdbarch);
+extern std::string gdbarch_gcc_target_options (struct gdbarch *gdbarch);
 extern void set_gdbarch_gcc_target_options (struct gdbarch *gdbarch, gdbarch_gcc_target_options_ftype *gcc_target_options);
 
 /* Return a regular expression that matches names used by this
diff --git a/gdb/gdbarch.sh b/gdb/gdbarch.sh
index d589b2c..62f68dc 100755
--- a/gdb/gdbarch.sh
+++ b/gdb/gdbarch.sh
@@ -1184,8 +1184,8 @@ 
 # Return string (caller has to use xfree for it) with options for GCC
 # to produce code for this target, typically "-m64", "-m32" or "-m31".
 # These options are put before CU's DW_AT_producer compilation options so that
-# they can override it.  Method may also return NULL.
-m;char *;gcc_target_options;void;;;default_gcc_target_options;;0
+# they can override it.
+m;std::string;gcc_target_options;void;;;default_gcc_target_options;;0
 
 # Return a regular expression that matches names used by this
 # architecture in GNU configury triplets.  The result is statically
diff --git a/gdb/nios2-tdep.c b/gdb/nios2-tdep.c
index cb17e4f..c2683b5 100644
--- a/gdb/nios2-tdep.c
+++ b/gdb/nios2-tdep.c
@@ -2260,11 +2260,11 @@ 
 }
 
 /* Implement the gcc_target_options gdbarch method.  */
-static char *
+static std::string
 nios2_gcc_target_options (struct gdbarch *gdbarch)
 {
   /* GCC doesn't know "-m32".  */
-  return NULL;
+  return {};
 }
 
 /* Initialize the Nios II gdbarch.  */
diff --git a/gdb/s390-tdep.c b/gdb/s390-tdep.c
index 6bd0528..329a122 100644
--- a/gdb/s390-tdep.c
+++ b/gdb/s390-tdep.c
@@ -6779,10 +6779,10 @@ 
 /* Implement gdbarch_gcc_target_options.  GCC does not know "-m32" or
    "-mcmodel=large".  */
 
-static char *
+static std::string
 s390_gcc_target_options (struct gdbarch *gdbarch)
 {
-  return xstrdup (gdbarch_ptr_bit (gdbarch) == 64 ? "-m64" : "-m31");
+  return gdbarch_ptr_bit (gdbarch) == 64 ? "-m64" : "-m31";
 }
 
 /* Implement gdbarch_gnu_triplet_regexp.  Target triplets are "s390-*"