[binutils-gdb] Use xstrdup in tui_set_source_content

Tom Tromey tromey@sourceware.org
Thu Jan 3 22:40:00 GMT 2019


https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=986041cda76a312e4285c9ba1e51b1778fcb15ce

commit 986041cda76a312e4285c9ba1e51b1778fcb15ce
Author: Tom Tromey <tom@tromey.com>
Date:   Wed Jan 2 11:41:44 2019 -0700

    Use xstrdup in tui_set_source_content
    
    valgrind pointed out that the string copy in tui_set_source_content
    was not allocating space for the trailing \0:
    
    ==3941== Invalid write of size 1
    ==3941==    at 0x4C3239F: strcpy (vg_replace_strmem.c:512)
    ==3941==    by 0x72036B: strcpy (string_fortified.h:90)
    ==3941==    by 0x72036B: tui_set_source_content(symtab*, int, int) (tui-source.c:203)
    
    Looking closer, I don't think there's a need to check the line width
    here, so this patch changes it to use xstrdup.
    
    Tested by re-running the TUI under valgrind.  There are still other
    valgrind reports from TUI code, but this one is gone.
    
    gdb/ChangeLog
    2019-01-03  Tom Tromey  <tom@tromey.com>
    
    	* tui/tui-source.c (tui_set_source_content): Use xstrdup.

Diff:
---
 gdb/ChangeLog        | 4 ++++
 gdb/tui/tui-source.c | 8 +-------
 2 files changed, 5 insertions(+), 7 deletions(-)

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index 2292eaa..305eb7a 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,5 +1,9 @@
 2019-01-03  Tom Tromey  <tom@tromey.com>
 
+	* tui/tui-source.c (tui_set_source_content): Use xstrdup.
+
+2019-01-03  Tom Tromey  <tom@tromey.com>
+
 	* python/py-symtab.c (salpy_str): Update.
 	(struct salpy_sal_object) <symtab>: Now a PyObject.
 	(salpy_dealloc): Update.
diff --git a/gdb/tui/tui-source.c b/gdb/tui/tui-source.c
index 9c2a982..ed9562a 100644
--- a/gdb/tui/tui-source.c
+++ b/gdb/tui/tui-source.c
@@ -194,15 +194,9 @@ tui_set_source_content (struct symtab *s,
 
 		  xfree (TUI_SRC_WIN->generic.content[cur_line]
 			 ->which_element.source.line);
-		  int alloc_len = text.size ();
-		  if (alloc_len < line_width)
-		    alloc_len = line_width + 1;
 		  TUI_SRC_WIN->generic.content[cur_line]
 		    ->which_element.source.line
-		    = (char *) xmalloc (alloc_len);
-		  strcpy (TUI_SRC_WIN->generic.content[cur_line]
-			  ->which_element.source.line,
-			  text.c_str ());
+		    = xstrdup (text.c_str ());
 
 		  cur_line++;
 		  cur_line_no++;



More information about the Gdb-cvs mailing list