From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 37714 invoked by alias); 15 Feb 2018 20:50:07 -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 37672 invoked by uid 89); 15 Feb 2018 20:50:06 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-25.9 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.2 spammy= X-HELO: gateway33.websitewelcome.com Received: from gateway33.websitewelcome.com (HELO gateway33.websitewelcome.com) (192.185.146.87) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Thu, 15 Feb 2018 20:50:05 +0000 Received: from cm11.websitewelcome.com (cm11.websitewelcome.com [100.42.49.5]) by gateway33.websitewelcome.com (Postfix) with ESMTP id 2B1D68152B69 for ; Thu, 15 Feb 2018 14:50:04 -0600 (CST) Received: from box5379.bluehost.com ([162.241.216.53]) by cmsmtp with SMTP id mQTkexxZjvkfxmQTkePYXp; Thu, 15 Feb 2018 14:50:04 -0600 Received: from 174-29-60-18.hlrn.qwest.net ([174.29.60.18]:46122 helo=bapiya.Home) by box5379.bluehost.com with esmtpsa (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.89) (envelope-from ) id 1emQTj-003pYR-V1; Thu, 15 Feb 2018 14:50:04 -0600 From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [RFA 3/3] Special case NULL when using printf's %s format Date: Thu, 15 Feb 2018 20:50:00 -0000 Message-Id: <20180215205001.337-4-tom@tromey.com> In-Reply-To: <20180215205001.337-1-tom@tromey.com> References: <20180215205001.337-1-tom@tromey.com> X-BWhitelist: no X-Source-L: No X-Exim-ID: 1emQTj-003pYR-V1 X-Source-Sender: 174-29-60-18.hlrn.qwest.net (bapiya.Home) [174.29.60.18]:46122 X-Source-Auth: tom+tromey.com X-Email-Count: 4 X-Source-Cap: ZWx5bnJvYmk7ZWx5bnJvYmk7Ym94NTM3OS5ibHVlaG9zdC5jb20= X-Local-Domain: yes X-SW-Source: 2018-02/txt/msg00209.txt.bz2 This changes the printf command's %s and %ls formats to special-case NULL, and print "(null)" for these. This is PR cli/14977. This behavior seems a bit friendlier; I was undecided on whether other invalid pointers should be handled specially somehow, so for the time being I've left those out. gdb/ChangeLog 2018-02-14 Tom Tromey PR cli/14977: * printcmd.c (printf_c_string, printf_wide_c_string): Special case for NULL. gdb/gdbserver/ChangeLog 2018-02-14 Tom Tromey PR cli/14977: * ax.c (ax_printf): Special case for NULL. gdb/testsuite/ChangeLog 2018-02-14 Tom Tromey PR cli/14977: * gdb.base/printcmds.exp (test_printf): Add printf test of %s with a null pointer. * gdb.base/wchar.exp: Likewise. --- gdb/ChangeLog | 6 ++++++ gdb/gdbserver/ChangeLog | 5 +++++ gdb/gdbserver/ax.c | 5 +++++ gdb/printcmd.c | 10 ++++++++++ gdb/testsuite/ChangeLog | 7 +++++++ gdb/testsuite/gdb.base/printcmds.exp | 3 +++ gdb/testsuite/gdb.base/wchar.exp | 3 +++ 7 files changed, 39 insertions(+) diff --git a/gdb/gdbserver/ax.c b/gdb/gdbserver/ax.c index b42ee54c84..c754383df8 100644 --- a/gdb/gdbserver/ax.c +++ b/gdb/gdbserver/ax.c @@ -847,6 +847,11 @@ ax_printf (CORE_ADDR fn, CORE_ADDR chan, const char *format, int j; tem = args[i]; + if (tem == 0) + { + printf (current_substring, "(null)"); + break; + } /* This is a %s argument. Find the length of the string. */ for (j = 0;; j++) diff --git a/gdb/printcmd.c b/gdb/printcmd.c index d2d13895f6..a399ed761b 100644 --- a/gdb/printcmd.c +++ b/gdb/printcmd.c @@ -2220,6 +2220,11 @@ printf_c_string (struct ui_file *stream, const char *format, int j; tem = value_as_address (value); + if (tem == 0) + { + fprintf_filtered (stream, format, "(null)"); + return; + } /* This is a %s argument. Find the length of the string. */ for (j = 0;; j++) @@ -2260,6 +2265,11 @@ printf_wide_c_string (struct ui_file *stream, const char *format, gdb_byte *buf = (gdb_byte *) alloca (wcwidth); tem = value_as_address (value); + if (tem == 0) + { + fprintf_filtered (stream, format, "(null)"); + return; + } /* This is a %s argument. Find the length of the string. */ for (j = 0;; j += wcwidth) diff --git a/gdb/testsuite/gdb.base/printcmds.exp b/gdb/testsuite/gdb.base/printcmds.exp index 56cedb908f..635bd621fd 100644 --- a/gdb/testsuite/gdb.base/printcmds.exp +++ b/gdb/testsuite/gdb.base/printcmds.exp @@ -780,6 +780,9 @@ proc test_printf {} { # PR cli/19918. gdb_test "printf \"%-16dq\\n\", 0" "0 q" gdb_test "printf \"%-16pq\\n\", 0" "\\(nil\\) q" + + # PR cli/14977. + gdb_test "printf \"%s\\n\", 0" "\\(null\\)" } #Test printing DFP values with printf diff --git a/gdb/testsuite/gdb.base/wchar.exp b/gdb/testsuite/gdb.base/wchar.exp index 80b6513e22..a4d9bce7d0 100644 --- a/gdb/testsuite/gdb.base/wchar.exp +++ b/gdb/testsuite/gdb.base/wchar.exp @@ -69,3 +69,6 @@ gdb_test "print repeat" "= L\"A$cent$cent\"\.\.\." \ gdb_test "print repeat_p" "= $hex L\"A$cent$cent\"\.\.\." \ "print repeat_p (print elements 3)" + +# From PR cli/14977, but here because it requires wchar_t. +gdb_test "printf \"%ls\\n\", 0" "\\(null\\)" -- 2.13.6