[RFA,2/3] Allow - in %p for printf

Message ID 20180215205001.337-3-tom@tromey.com
State New, archived
Headers

Commit Message

Tom Tromey Feb. 15, 2018, 8:50 p.m. UTC
  PR cli/19918 points out that a printf format like "%-5p" will cause a
gdb crash.  The bug is problem is that printf_pointer doesn't take the
"-" flag into account.

gdb/ChangeLog
2018-02-14  Tom Tromey  <tom@tromey.com>

	PR cli/19918:
	* printcmd.c (printf_pointer): Allow "-" in format.

gdb/testsuite/ChangeLog
2018-02-14  Tom Tromey  <tom@tromey.com>

	PR cli/19918:
	* gdb.base/printcmds.exp (test_printf): Add printf test using '-'
	flag.
---
 gdb/ChangeLog                        | 5 +++++
 gdb/printcmd.c                       | 5 +++--
 gdb/testsuite/ChangeLog              | 6 ++++++
 gdb/testsuite/gdb.base/printcmds.exp | 4 ++++
 4 files changed, 18 insertions(+), 2 deletions(-)
  

Comments

Alan Hayward Feb. 16, 2018, 11:31 a.m. UTC | #1
> On 15 Feb 2018, at 20:50, Tom Tromey <tom@tromey.com> wrote:

> 

> PR cli/19918 points out that a printf format like "%-5p" will cause a

> gdb crash.  The bug is problem is that printf_pointer doesn't take the

> "-" flag into account.

> 

> gdb/ChangeLog

> 2018-02-14  Tom Tromey  <tom@tromey.com>

> 

> 	PR cli/19918:

> 	* printcmd.c (printf_pointer): Allow "-" in format.

> 

> gdb/testsuite/ChangeLog

> 2018-02-14  Tom Tromey  <tom@tromey.com>

> 

> 	PR cli/19918:

> 	* gdb.base/printcmds.exp (test_printf): Add printf test using '-'

> 	flag.

> ---

> gdb/ChangeLog                        | 5 +++++

> gdb/printcmd.c                       | 5 +++--

> gdb/testsuite/ChangeLog              | 6 ++++++

> gdb/testsuite/gdb.base/printcmds.exp | 4 ++++

> 4 files changed, 18 insertions(+), 2 deletions(-)

> 

> diff --git a/gdb/printcmd.c b/gdb/printcmd.c

> index 13b967f0a2..d2d13895f6 100644

> --- a/gdb/printcmd.c

> +++ b/gdb/printcmd.c

> @@ -2399,8 +2399,9 @@ printf_pointer (struct ui_file *stream, const char *format,

>   if (val != 0)

>     *fmt_p++ = '#';

> 

> -  /* Copy any width.  */

> -  while (*p >= '0' && *p < '9')

> +  /* Copy any width or flags.  Only the "-" flag is needed -- see the

> +     format_pieces constructor.  */


I found this comment a little confusing. How about something like:
Copy and width or flags. “-“ is the only valid flag for pointers — see the format_pieces constructor.

Otherwise looks good to me.

> +  while (*p == '-' || (*p >= '0' && *p < '9'))

>     *fmt_p++ = *p++;

> 

>   gdb_assert (*p == 'p' && *(p + 1) == '\0');

> diff --git a/gdb/testsuite/gdb.base/printcmds.exp b/gdb/testsuite/gdb.base/printcmds.exp

> index 9402d97ef3..56cedb908f 100644

> --- a/gdb/testsuite/gdb.base/printcmds.exp

> +++ b/gdb/testsuite/gdb.base/printcmds.exp

> @@ -776,6 +776,10 @@ proc test_printf {} {

> 	"" \

> 	"create hibob command"

>     gdb_test "hibob" "hi bob zzz.*y" "run hibob command"

> +

> +    # PR cli/19918.

> +    gdb_test "printf \"%-16dq\\n\", 0" "0               q"

> +    gdb_test "printf \"%-16pq\\n\", 0" "\\(nil\\)           q"

> }

> 

> #Test printing DFP values with printf

> -- 

> 2.13.6

>
  
Tom Tromey Feb. 27, 2018, 3:50 a.m. UTC | #2
>>>>> "Alan" == Alan Hayward <Alan.Hayward@arm.com> writes:

Alan> I found this comment a little confusing. How about something like:
Alan> Copy and width or flags. “-“ is the only valid flag for pointers —
Alan> see the format_pieces constructor.

I made this change.

Tom
  

Patch

diff --git a/gdb/printcmd.c b/gdb/printcmd.c
index 13b967f0a2..d2d13895f6 100644
--- a/gdb/printcmd.c
+++ b/gdb/printcmd.c
@@ -2399,8 +2399,9 @@  printf_pointer (struct ui_file *stream, const char *format,
   if (val != 0)
     *fmt_p++ = '#';
 
-  /* Copy any width.  */
-  while (*p >= '0' && *p < '9')
+  /* Copy any width or flags.  Only the "-" flag is needed -- see the
+     format_pieces constructor.  */
+  while (*p == '-' || (*p >= '0' && *p < '9'))
     *fmt_p++ = *p++;
 
   gdb_assert (*p == 'p' && *(p + 1) == '\0');
diff --git a/gdb/testsuite/gdb.base/printcmds.exp b/gdb/testsuite/gdb.base/printcmds.exp
index 9402d97ef3..56cedb908f 100644
--- a/gdb/testsuite/gdb.base/printcmds.exp
+++ b/gdb/testsuite/gdb.base/printcmds.exp
@@ -776,6 +776,10 @@  proc test_printf {} {
 	"" \
 	"create hibob command"
     gdb_test "hibob" "hi bob zzz.*y" "run hibob command"
+
+    # PR cli/19918.
+    gdb_test "printf \"%-16dq\\n\", 0" "0               q"
+    gdb_test "printf \"%-16pq\\n\", 0" "\\(nil\\)           q"
 }
 
 #Test printing DFP values with printf