Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Tom Tromey <tom@tromey.com>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tom@tromey.com>
Subject: [PATCH 2/8] Don't create empty literal pieces
Date: Fri, 27 Sep 2019 21:25:00 -0000	[thread overview]
Message-ID: <20190927212520.20073-3-tom@tromey.com> (raw)
In-Reply-To: <20190927212520.20073-1-tom@tromey.com>

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-09-27  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                           |  6 ++++++
 gdb/gdbsupport/format.c                 | 16 ++++++++++------
 gdb/unittests/format_pieces-selftests.c |  6 ++++--
 3 files changed, 20 insertions(+), 8 deletions(-)

diff --git a/gdb/gdbsupport/format.c b/gdb/gdbsupport/format.c
index fb3421e62bf..a5a367015f1 100644
--- a/gdb/gdbsupport/format.c
+++ b/gdb/gdbsupport/format.c
@@ -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);
+    }
 }
diff --git a/gdb/unittests/format_pieces-selftests.c b/gdb/unittests/format_pieces-selftests.c
index 7d31b3cb93f..862b2da0f48 100644
--- a/gdb/unittests/format_pieces-selftests.c
+++ b/gdb/unittests/format_pieces-selftests.c
@@ -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),
     });
 }
 
-- 
2.17.2


  parent reply	other threads:[~2019-09-27 21:25 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-09-27 21:25 [PATCH 0/8] More gdb styling improvements Tom Tromey
2019-09-27 21:25 ` [PATCH 6/8] Introduce metadata style Tom Tromey
2019-09-30 14:07   ` Pedro Alves
2019-09-27 21:25 ` [PATCH 1/8] Remove the ui_out_style_kind enum Tom Tromey
2019-09-27 21:25 ` [PATCH 8/8] Use styled_string for "show logging filename" Tom Tromey
2019-09-27 21:25 ` [PATCH 4/8] Use new %p format suffixes in gdb Tom Tromey
2019-09-30 14:05   ` Pedro Alves
2019-09-27 21:25 ` [PATCH 5/8] Style "pwd" output Tom Tromey
2019-09-27 21:25 ` [PATCH 3/8] Introduce gdb-specific %p format suffixes Tom Tromey
2019-09-30 14:06   ` Pedro Alves
2021-08-18  9:02   ` Andreas Schwab
2021-08-27 20:17     ` Tom Tromey
2019-09-27 21:25 ` Tom Tromey [this message]
2019-09-27 21:25 ` [PATCH 7/8] Use styled_string in more places Tom Tromey
2019-09-30 14:09 ` [PATCH 0/8] More gdb styling improvements Pedro Alves
2019-10-01 21:11   ` Tom Tromey

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190927212520.20073-3-tom@tromey.com \
    --to=tom@tromey.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox