Don't create empty literal pieces
authorTom Tromey <tom@tromey.com>
Sun, 22 Sep 2019 22:06:03 +0000 (16:06 -0600)
committerTom Tromey <tom@tromey.com>
Tue, 1 Oct 2019 21:12:37 +0000 (15:12 -0600)
I noticed that format_pieces can create an empty literal piece.
However, there's never a need for one, so this patch removes the
possibility.

gdb/ChangeLog
2019-10-01  Tom Tromey  <tom@tromey.com>

* unittests/format_pieces-selftests.c: Update.  Add final format.
* gdbsupport/format.c (format_pieces::format_pieces): Don't add
empty literal pieces.

gdb/ChangeLog
gdb/gdbsupport/format.c
gdb/unittests/format_pieces-selftests.c

index dd196d7e4f15faf02ff54154098286ca6368d7ca..5c3dec35790fafdc4b5dbbe3ffec9c036935c655 100644 (file)
@@ -1,3 +1,9 @@
+2019-10-01  Tom Tromey  <tom@tromey.com>
+
+       * unittests/format_pieces-selftests.c: Update.  Add final format.
+       * gdbsupport/format.c (format_pieces::format_pieces): Don't add
+       empty literal pieces.
+
 2019-10-01  Tom Tromey  <tom@tromey.com>
 
        * ui-out.h (enum class ui_out_style_kind): Remove.
index fb3421e62bf6b23172fae032599436b6f2607200..a5a367015f1695ab3e1893579356a89125d73d7a 100644 (file)
@@ -129,7 +129,8 @@ format_pieces::format_pieces (const char **arg)
        current_substring += f - 1 - prev_start;
        *current_substring++ = '\0';
 
-       m_pieces.emplace_back (sub_start, literal_piece);
+       if (*sub_start != '\0')
+         m_pieces.emplace_back (sub_start, literal_piece);
 
        percent_loc = f - 1;
 
@@ -340,11 +341,14 @@ format_pieces::format_pieces (const char **arg)
 
   /* Record the remainder of the string.  */
 
-  sub_start = current_substring;
+  if (f > prev_start)
+    {
+      sub_start = current_substring;
 
-  strncpy (current_substring, prev_start, f - prev_start);
-  current_substring += f - prev_start;
-  *current_substring++ = '\0';
+      strncpy (current_substring, prev_start, f - prev_start);
+      current_substring += f - prev_start;
+      *current_substring++ = '\0';
 
-  m_pieces.emplace_back (sub_start, literal_piece);
+      m_pieces.emplace_back (sub_start, literal_piece);
+    }
 }
index 7d31b3cb93fbaee60c395d215810f3114080fba2..862b2da0f48a795e405120684dacadcaa48f6233 100644 (file)
@@ -48,13 +48,15 @@ test_escape_sequences ()
 static void
 test_format_specifier ()
 {
-  check ("Hello %d%llx%%d", /* ARI: %ll */
+  /* The format string here ends with a % sequence, to ensure we don't
+     see a trailing empty literal piece.  */
+  check ("Hello %d%llx%%d%d", /* ARI: %ll */
     {
       format_piece ("Hello ", literal_piece),
       format_piece ("%d", int_arg),
-      format_piece ("", literal_piece),
       format_piece ("%llx", long_long_arg), /* ARI: %ll */
       format_piece ("%%d", literal_piece),
+      format_piece ("%d", int_arg),
     });
 }