[RFA] Fix leak due to assigning a xstrdup-ed string to the std::string gdb_datadir

Message ID 20190929163903.21699-1-philippe.waroquiers@skynet.be
State Superseded
Headers show
Series
  • [RFA] Fix leak due to assigning a xstrdup-ed string to the std::string gdb_datadir
Related show

Commit Message

Philippe Waroquiers Sept. 29, 2019, 4:39 p.m.
Valgrind reports the following leak:
==32623== 56 bytes in 1 blocks are definitely lost in loss record 1,099 of 6,654
==32623==    at 0x4835753: malloc (vg_replace_malloc.c:307)
==32623==    by 0x25CF67: xmalloc (alloc.c:60)
==32623==    by 0x65FBD9: xstrdup (xstrdup.c:34)
==32623==    by 0x413D9E: captured_main_1(captured_main_args*) (main.c:553)
==32623==    by 0x414FFA: captured_main (main.c:1172)
==32623==    by 0x414FFA: gdb_main(captured_main_args*) (main.c:1197)
==32623==    by 0x22531A: main (gdb.c:32)

Commit f2aec7f6d14 changed gdb_datadir to std::string.
So, xstrdup-ing the result of relocate_gdb_directory (returning a std::string)
is not needed and creates a leak.
Fix the leak by removing the xstrdup.
Also removes a useless conversion of gdb_datadir to std::string.

gdb/ChangeLog
YYYY-MM-DD  Philippe Waroquiers  <philippe.waroquiers@skynet.be>

	* main.c (relocate_gdbinit_path_maybe_in_datadir): Remove std::string
	conversion of gdb_datadir.
	(captured_main_1): Remove xstrdup when assigning to gdb_datadir.
---
 gdb/main.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

-- 
2.20.1

Comments

Tom Tromey Sept. 30, 2019, 2:01 p.m. | #1
>>>>> "Philippe" == Philippe Waroquiers <philippe.waroquiers@skynet.be> writes:


Philippe> Valgrind reports the following leak:
Philippe> ==32623== 56 bytes in 1 blocks are definitely lost in loss record 1,099 of 6,654
Philippe> ==32623==    at 0x4835753: malloc (vg_replace_malloc.c:307)
Philippe> ==32623==    by 0x25CF67: xmalloc (alloc.c:60)
Philippe> ==32623==    by 0x65FBD9: xstrdup (xstrdup.c:34)
Philippe> ==32623==    by 0x413D9E: captured_main_1(captured_main_args*) (main.c:553)
Philippe> ==32623==    by 0x414FFA: captured_main (main.c:1172)
Philippe> ==32623==    by 0x414FFA: gdb_main(captured_main_args*) (main.c:1197)
Philippe> ==32623==    by 0x22531A: main (gdb.c:32)

Philippe> Commit f2aec7f6d14 changed gdb_datadir to std::string.
Philippe> So, xstrdup-ing the result of relocate_gdb_directory (returning a std::string)
Philippe> is not needed and creates a leak.
Philippe> Fix the leak by removing the xstrdup.
Philippe> Also removes a useless conversion of gdb_datadir to std::string.

Philippe> gdb/ChangeLog
Philippe> YYYY-MM-DD  Philippe Waroquiers  <philippe.waroquiers@skynet.be>

Philippe> 	* main.c (relocate_gdbinit_path_maybe_in_datadir): Remove std::string
Philippe> 	conversion of gdb_datadir.
Philippe> 	(captured_main_1): Remove xstrdup when assigning to gdb_datadir.

Thank you.

Philippe>    gdb_datadir
Philippe> -    = xstrdup (relocate_gdb_directory (GDB_DATADIR,
Philippe> -				     GDB_DATADIR_RELOCATABLE).c_str ());
Philippe> +    = relocate_gdb_directory (GDB_DATADIR,
Philippe> +			      GDB_DATADIR_RELOCATABLE).c_str ();

It seems to me that the c_str call can also be removed.

Tom
Philippe Waroquiers Sept. 30, 2019, 7:54 p.m. | #2
On Mon, 2019-09-30 at 08:01 -0600, Tom Tromey wrote:

> Philippe>    gdb_datadir

> Philippe> -    = xstrdup (relocate_gdb_directory (GDB_DATADIR,

> Philippe> -				     GDB_DATADIR_RELOCATABLE).c_str ());

> Philippe> +    = relocate_gdb_directory (GDB_DATADIR,

> Philippe> +			      GDB_DATADIR_RELOCATABLE).c_str ();

> 

> It seems to me that the c_str call can also be removed.

Effectively.  I have submitted RFAv2 with this additional change.

Thanks for the review

Philippe

Patch

diff --git a/gdb/main.c b/gdb/main.c
index 7fab8ff8da..4ebb2127fe 100644
--- a/gdb/main.c
+++ b/gdb/main.c
@@ -214,8 +214,7 @@  relocate_gdbinit_path_maybe_in_datadir (const std::string& file)
       size_t start = datadir_len;
       for (; IS_DIR_SEPARATOR (file[start]); ++start)
 	;
-      relocated_path = (std::string (gdb_datadir) + SLASH_STRING
-			+ file.substr (start));
+      relocated_path = gdb_datadir + SLASH_STRING + file.substr (start);
     }
   else
     {
@@ -550,8 +549,8 @@  captured_main_1 (struct captured_main_args *context)
 				     DEBUGDIR_RELOCATABLE).c_str ());
 
   gdb_datadir
-    = xstrdup (relocate_gdb_directory (GDB_DATADIR,
-				     GDB_DATADIR_RELOCATABLE).c_str ());
+    = relocate_gdb_directory (GDB_DATADIR,
+			      GDB_DATADIR_RELOCATABLE).c_str ();
 
 #ifdef WITH_PYTHON_PATH
   {