From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 106175 invoked by alias); 27 Sep 2019 21:25:31 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Received: (qmail 106041 invoked by uid 89); 27 Sep 2019 21:25:30 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-18.7 required=5.0 tests=AWL,BAYES_00,GIT_PATCH_0,GIT_PATCH_1,GIT_PATCH_2,GIT_PATCH_3,RCVD_IN_DNSWL_NONE,SPF_HELO_PASS autolearn=ham version=3.3.1 spammy=ari X-HELO: gateway34.websitewelcome.com Received: from gateway34.websitewelcome.com (HELO gateway34.websitewelcome.com) (192.185.148.196) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Fri, 27 Sep 2019 21:25:25 +0000 Received: from cm14.websitewelcome.com (cm14.websitewelcome.com [100.42.49.7]) by gateway34.websitewelcome.com (Postfix) with ESMTP id C1E831AA9B8 for ; Fri, 27 Sep 2019 16:25:23 -0500 (CDT) Received: from box5379.bluehost.com ([162.241.216.53]) by cmsmtp with SMTP id DxjvihcwcBnGaDxjvisxkj; Fri, 27 Sep 2019 16:25:23 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=tromey.com; s=default; h=References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From: Sender:Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=v91Hgpw4imI/p4cuJVh6ItVnTKV+ORWRdMpBz+Jczw8=; b=TAMX2xkXf8ijJwrz2r9powazq5 gRKYFQt9wEpEw4WG5lpzHlL+/8ru62Z9MKWHnXVE4Bb2xxEnU76/NA8r7r25S736teVfv88bLdLby azY8kr6/XiYRIptgFBO/CvsUF; Received: from 71-218-73-27.hlrn.qwest.net ([71.218.73.27]:34758 helo=bapiya.Home) by box5379.bluehost.com with esmtpsa (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.92) (envelope-from ) id 1iDxjv-002PvY-Fs; Fri, 27 Sep 2019 16:25:23 -0500 From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [PATCH 2/8] Don't create empty literal pieces Date: Fri, 27 Sep 2019 21:25:00 -0000 Message-Id: <20190927212520.20073-3-tom@tromey.com> In-Reply-To: <20190927212520.20073-1-tom@tromey.com> References: <20190927212520.20073-1-tom@tromey.com> X-SW-Source: 2019-09/txt/msg00565.txt.bz2 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 * 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