Fix BZ#24331 -- set print elem doesn't limit repeated elements correctly

Message ID CALoOobPe4A4ZzfX3m0B-CLmd6W6Kw-rDwMxGOfTh1ZCCKVqdXg@mail.gmail.com
State New, archived
Headers

Commit Message

Terekhov, Mikhail via Gdb-patches July 3, 2019, 11:53 p.m. UTC
  Greetings,

Attached patch fixes https://sourceware.org/bugzilla/show_bug.cgi?id=24331.
I did not add a new test: since existing test already recorded
incorrect behavior, I adjusted it instead.

Thanks,

2019-07-03  Paul Pluzhnikov  <ppluzhnikov@google.com>

        [PR gdb/24331]
        * gdb/valprint.c (generic_printstr): Respect print_max.
        * gdb/testsuite/gdb.python/py-format-string.exp (test_max_elements):
        Adjust.
  

Comments

Tom Tromey July 9, 2019, 1:53 p.m. UTC | #1
>>>>> "Paul" == Paul Pluzhnikov via gdb-patches <gdb-patches@sourceware.org> writes:

Paul> Attached patch fixes https://sourceware.org/bugzilla/show_bug.cgi?id=24331.
Paul> I did not add a new test: since existing test already recorded
Paul> incorrect behavior, I adjusted it instead.

Thanks for the patch.
It looks good but there are some formatting nits.

Paul> --- a/gdb/testsuite/gdb.python/py-format-string.exp
Paul> +++ b/gdb/testsuite/gdb.python/py-format-string.exp
Paul> @@ -621,7 +621,7 @@ proc test_max_elements {} {
Paul>      # This will print four characters instead of three, see
Paul>      # <https://sourceware.org/bugzilla/show_bug.cgi?id=24331>.

I think this comment can be removed now.

Paul> +      if (i + r > options->print_max)
Paul> +        /* Printing "r" repeats would push over print_max.
Paul> +         * Adjust repeats down.  Note: converted_chars.back() is the next
Paul> +	 * character.  */
Paul> +        r = (&converted_chars.back () - 1)->repeat_count
Paul> +	  = options->print_max - i;

Normally gdb doesn't use "*" at the start of the body lines of a
comment.  Nor does it generally use multiple assignments like this.
Finally, multi-line if bodies, even when some of the lines are comments,
are braced.

This is ok with the nits fixed, thanks again.

Tom
  

Patch

diff --git a/gdb/testsuite/gdb.python/py-format-string.exp b/gdb/testsuite/gdb.python/py-format-string.exp
index ebb2074ce8..5518769689 100644
--- a/gdb/testsuite/gdb.python/py-format-string.exp
+++ b/gdb/testsuite/gdb.python/py-format-string.exp
@@ -621,7 +621,7 @@  proc test_max_elements {} {
     # This will print four characters instead of three, see
     # <https://sourceware.org/bugzilla/show_bug.cgi?id=24331>.
     check_format_string "a_binary_string_array" $opts \
-      "\"hell\"..."
+      "\"hel\"..."
     check_format_string "a_big_string" $opts \
       [get_cut_big_string 3]
     check_format_string "an_array" $opts
diff --git a/gdb/valprint.c b/gdb/valprint.c
index 6f9b47da80..dba0d28fb5 100644
--- a/gdb/valprint.c
+++ b/gdb/valprint.c
@@ -2770,6 +2770,13 @@  generic_printstr (struct ui_file *stream, struct type *type,
       if (r < 0)
 	break;
 
+      if (i + r > options->print_max)
+        /* Printing "r" repeats would push over print_max.
+         * Adjust repeats down.  Note: converted_chars.back() is the next
+	 * character.  */
+        r = (&converted_chars.back () - 1)->repeat_count
+	  = options->print_max - i;
+
       /* Otherwise, add the count to the total print count and get
 	 the next character.  */
       i += r;