[2/4] tui-disasm: Fix line buffer size calculation

Message ID 1478631454-9447-3-git-send-email-arnez@linux.vnet.ibm.com
State New, archived
Headers

Commit Message

Andreas Arnez Nov. 8, 2016, 6:56 p.m. UTC
  The code that fills the TUI disassembly window content first calculates
the maximum full length of a displayed disassembly line.  This
calculation typically yields the wrong result.  The result is too large,
so the bug does not cause any run-time failures, but unnecessary
confusion for the reader.  This patch fixes the calculation.

gdb/ChangeLog:

	* tui/tui-disasm.c (tui_set_disassem_content): Fix calculation of
	the longest disassembly line's length.
---
 gdb/tui/tui-disasm.c | 19 ++++++++++---------
 1 file changed, 10 insertions(+), 9 deletions(-)
  

Comments

Pedro Alves Nov. 8, 2016, 7:30 p.m. UTC | #1
On 11/08/2016 06:56 PM, Andreas Arnez wrote:
> The code that fills the TUI disassembly window content first calculates
> the maximum full length of a displayed disassembly line.  This
> calculation typically yields the wrong result.  The result is too large,
> so the bug does not cause any run-time failures, but unnecessary
> confusion for the reader.  This patch fixes the calculation.
> 
> gdb/ChangeLog:
> 
> 	* tui/tui-disasm.c (tui_set_disassem_content): Fix calculation of
> 	the longest disassembly line's length.

OK.

Thanks,
Pedro Alves
  

Patch

diff --git a/gdb/tui/tui-disasm.c b/gdb/tui/tui-disasm.c
index 5368aa4..6811be3 100644
--- a/gdb/tui/tui-disasm.c
+++ b/gdb/tui/tui-disasm.c
@@ -178,7 +178,7 @@  tui_set_disassem_content (struct gdbarch *gdbarch, CORE_ADDR pc)
   int tab_len = tui_default_tab_len ();
   struct tui_asm_line *asm_lines;
   int insn_pos;
-  int addr_size, max_size;
+  int addr_size, insn_size;
   char *line;
   
   if (pc == 0)
@@ -203,9 +203,9 @@  tui_set_disassem_content (struct gdbarch *gdbarch, CORE_ADDR pc)
 
   tui_disassemble (gdbarch, asm_lines, pc, max_lines);
 
-  /* See what is the maximum length of an address and of a line.  */
+  /* Determine maximum address- and instruction lengths.  */
   addr_size = 0;
-  max_size = 0;
+  insn_size = 0;
   for (i = 0; i < max_lines; i++)
     {
       size_t len = strlen (asm_lines[i].addr_string);
@@ -213,16 +213,17 @@  tui_set_disassem_content (struct gdbarch *gdbarch, CORE_ADDR pc)
       if (len > addr_size)
         addr_size = len;
 
-      len = strlen (asm_lines[i].insn) + tab_len;
-      if (len > max_size)
-        max_size = len;
+      len = strlen (asm_lines[i].insn);
+      if (len > insn_size)
+	insn_size = len;
     }
-  max_size += addr_size + tab_len;
 
-  /* Allocate memory to create each line.  */
-  line = (char*) alloca (max_size);
+  /* Align instructions to the same column.  */
   insn_pos = (1 + (addr_size / tab_len)) * tab_len;
 
+  /* Allocate memory to create each line.  */
+  line = (char*) alloca (insn_pos + insn_size + 1);
+
   /* Now construct each line.  */
   for (i = 0; i < max_lines; i++)
     {