Fix undefined behavior in TUI's TAB expansion
authorEli Zaretskii <eliz@gnu.org>
Sat, 21 Mar 2015 08:48:34 +0000 (10:48 +0200)
committerEli Zaretskii <eliz@gnu.org>
Sat, 21 Mar 2015 08:48:34 +0000 (10:48 +0200)
gdb/ChangeLog:

* tui/tui-io.c (tui_expand_tabs): Reinitialize the column counter
before the second loop, to avoid undefined behavior.  Reported by
Anton Blanchard <anton@samba.org>.

gdb/ChangeLog
gdb/tui/tui-io.c

index 969faace442070d14864078d3734d0beef0fb7b9..9b6deb446dd1e4cc4072b07109033dd62e14b7a9 100644 (file)
@@ -1,3 +1,9 @@
+2015-03-21  Eli Zaretskii  <eliz@gnu.org>
+
+       * tui/tui-io.c (tui_expand_tabs): Reinitialize the column counter
+       before the second loop, to avoid undefined behavior.  Reported by
+       Anton Blanchard <anton@samba.org>.
+
 2015-03-20  Keven Boell  <keven.boell@intel.com>
 
        * gdbtypes.c (resolve_dynamic_type_internal): Adapt
index c8b85672c0fb529fcf0d04c20b021b3f9dd202ab..97906ceb45eaefb7d5fea9f38e73616ffb80153b 100644 (file)
@@ -668,20 +668,20 @@ tui_getc (FILE *fp)
 char *
 tui_expand_tabs (const char *string, int col)
 {
-  int n_adjust;
+  int n_adjust, ncol;
   const char *s;
   char *ret, *q;
 
   /* 1. How many additional characters do we need?  */
-  for (n_adjust = 0, s = string; s; )
+  for (ncol = col, n_adjust = 0, s = string; s; )
     {
       s = strpbrk (s, "\t");
       if (s)
        {
-         col += (s - string) + n_adjust;
+         ncol += (s - string) + n_adjust;
          /* Adjustment for the next tab stop, minus one for the TAB
             we replace with spaces.  */
-         n_adjust += 8 - (col % 8) - 1;
+         n_adjust += 8 - (ncol % 8) - 1;
          s++;
        }
     }
@@ -690,7 +690,7 @@ tui_expand_tabs (const char *string, int col)
   ret = q = xmalloc (strlen (string) + n_adjust + 1);
 
   /* 2. Copy the original string while replacing TABs with spaces.  */
-  for (s = string; s; )
+  for (ncol = col, s = string; s; )
     {
       const char *s1 = strpbrk (s, "\t");
       if (s1)
@@ -699,12 +699,12 @@ tui_expand_tabs (const char *string, int col)
            {
              strncpy (q, s, s1 - s);
              q += s1 - s;
-             col += s1 - s;
+             ncol += s1 - s;
            }
          do {
            *q++ = ' ';
-           col++;
-         } while ((col % 8) != 0);
+           ncol++;
+         } while ((ncol % 8) != 0);
          s1++;
        }
       else
This page took 0.028037 seconds and 4 git commands to generate.