This is the mail archive of the gdb-cvs@sourceware.org mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[binutils-gdb] Special case NULL when using printf's %s format


https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=3ae9ce5dd7d1119ca2c94c63a07b04921048ebe3

commit 3ae9ce5dd7d1119ca2c94c63a07b04921048ebe3
Author: Tom Tromey <tom@tromey.com>
Date:   Wed Feb 14 20:11:16 2018 -0700

    Special case NULL when using printf's %s format
    
    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-03-14  Tom Tromey  <tom@tromey.com>
    
    	PR cli/14977:
    	* printcmd.c (printf_c_string, printf_wide_c_string): Special case
    	for NULL.
    
    gdb/gdbserver/ChangeLog
    2018-03-14  Tom Tromey  <tom@tromey.com>
    
    	PR cli/14977:
    	* ax.c (ax_printf): Special case for NULL.
    
    gdb/testsuite/ChangeLog
    2018-03-14  Tom Tromey  <tom@tromey.com>
    
    	PR cli/14977:
    	* gdb.base/printcmds.exp (test_printf): Add printf test of %s with
    	a null pointer.
    	* gdb.base/wchar.exp: Likewise.

Diff:
---
 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/ChangeLog b/gdb/ChangeLog
index da59b39..a62255f 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,5 +1,11 @@
 2018-03-14  Tom Tromey  <tom@tromey.com>
 
+	PR cli/14977:
+	* printcmd.c (printf_c_string, printf_wide_c_string): Special case
+	for NULL.
+
+2018-03-14  Tom Tromey  <tom@tromey.com>
+
 	PR cli/19918:
 	* printcmd.c (printf_pointer): Allow "-" in format.
 
diff --git a/gdb/gdbserver/ChangeLog b/gdb/gdbserver/ChangeLog
index 053cf17..2be87a6 100644
--- a/gdb/gdbserver/ChangeLog
+++ b/gdb/gdbserver/ChangeLog
@@ -1,3 +1,8 @@
+2018-03-14  Tom Tromey  <tom@tromey.com>
+
+	PR cli/14977:
+	* ax.c (ax_printf): Special case for NULL.
+
 2018-03-08  Simon Marchi  <simon.marchi@polymtl.ca>
 
 	* linux-low.c (linux_qxfer_libraries_svr4): Use
diff --git a/gdb/gdbserver/ax.c b/gdb/gdbserver/ax.c
index b42ee54..c754383 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 17c67ee..9fb2e02 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/ChangeLog b/gdb/testsuite/ChangeLog
index abac29f..f31f91e 100644
--- a/gdb/testsuite/ChangeLog
+++ b/gdb/testsuite/ChangeLog
@@ -1,5 +1,12 @@
 2018-03-14  Tom Tromey  <tom@tromey.com>
 
+	PR cli/14977:
+	* gdb.base/printcmds.exp (test_printf): Add printf test of %s with
+	a null pointer.
+	* gdb.base/wchar.exp: Likewise.
+
+2018-03-14  Tom Tromey  <tom@tromey.com>
+
 	PR cli/19918:
 	* gdb.base/printcmds.exp (test_printf): Add printf test using '-'
 	flag.
diff --git a/gdb/testsuite/gdb.base/printcmds.exp b/gdb/testsuite/gdb.base/printcmds.exp
index 56cedb9..635bd62 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 80b6513..a4d9bce 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\\)"


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]