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.
This commit is contained in:
Tom Tromey
2018-02-14 20:11:16 -07:00
parent b8c2339b2f
commit 3ae9ce5dd7
7 changed files with 39 additions and 0 deletions

View File

@ -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)