TUI: avoid calling strcpy() on indentical string objects

Message ID 1430061440-20379-1-git-send-email-patrick@parcs.ath.cx
State New, archived
Headers

Commit Message

Patrick Palka April 26, 2015, 3:17 p.m. UTC
  In tui_set_source_content(), when offset == 0 the source and destination
pointers of the call to strcpy() are actually the same.  In this case
not only is strcpy() unnecessary but it is also UB when the two strings
overlap.

gdb/ChangeLog:

	* tui/tui-source.c (tui_set_source_content): Avoid calling
	strcpy() when offset is 0.
---
 gdb/tui/tui-source.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
  

Comments

Pedro Alves April 27, 2015, 5:51 p.m. UTC | #1
On 04/26/2015 04:17 PM, Patrick Palka wrote:
> In tui_set_source_content(), when offset == 0 the source and destination
> pointers of the call to strcpy() are actually the same.  In this case
> not only is strcpy() unnecessary but it is also UB when the two strings
> overlap.

OK.

Boy is that code messy.

Thanks,
Pedro Alves
  

Patch

diff --git a/gdb/tui/tui-source.c b/gdb/tui/tui-source.c
index 31df0c8..018a1df 100644
--- a/gdb/tui/tui-source.c
+++ b/gdb/tui/tui-source.c
@@ -218,7 +218,9 @@  tui_set_source_content (struct symtab *s,
 			}
 		      /* Now copy the line taking the offset into
 			 account.  */
-		      if (strlen (src_line) > offset)
+		      if (offset == 0)
+			;
+		      else if (strlen (src_line) > offset)
 			strcpy (TUI_SRC_WIN->generic.content[cur_line]
 				  ->which_element.source.line,
 				&src_line[offset]);