From patchwork Mon May 8 01:22:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Pluzhnikov X-Patchwork-Id: 68896 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 82EED3858408 for ; Mon, 8 May 2023 01:23:21 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 82EED3858408 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1683509001; bh=x+rI27n/RtkpiVAnhCECNeXNIQNmnA/2toY4QxcTEpI=; h=Date:Subject:To:Cc:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=kP+jMSSYqBOf+auFrUiBRsVi/m6q1mHTsPM8HqmcO5O9APTTQP69X+Ia9f/zP7Wax nwjXdN2OO8vqMy6/IDHKEPU+cBkg1pl17+bCyEjSHPQbN8CnmvqPgByVmL+qSE2GoH nE06GgCctYju3ZIoM0F1SuG+j6CuHmT08Lx/7WZE= X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by sourceware.org (Postfix) with ESMTPS id AD7FE3858D32 for ; Mon, 8 May 2023 01:22:57 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org AD7FE3858D32 Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-ba22ced2f40so3523709276.1 for ; Sun, 07 May 2023 18:22:57 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683508977; x=1686100977; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=x+rI27n/RtkpiVAnhCECNeXNIQNmnA/2toY4QxcTEpI=; b=MHur73MFyvb1hTxzYy0eGbZ4/Mie2UR3oxB5vvV8J1CF2mQBO7O61HU8OweYp1/jDK /bplSecEslYQDQtZauQnhAVCmZICFY1rE718kKmRcI3u0PoEuiNjZUu5JMJUnx1ktgEj t1d/NxeRkNHMPYYvZN/u6ZNdtgSkif3daqYxSAA/yOKApaY5yM8QaW1rfjr3yBfITaYl BLyv+FpQitGr+TQK0lGe5uqP7GVS+C3DeH7bHWlX4m/mcp+0HodF+t+/rwmhsa8x+SMf G6jRFlj0A7DbKtXn3S8hQ+rumfPEmnCkoH9XPSnrojvtyn3iFLxYnmmw90uLDCrU81D7 s+Gw== X-Gm-Message-State: AC+VfDza1wfAX/IG07x3PhUPXhK1VEPJ3IGwrTQ9McG13JhArMlm4b/v KJcajs8dAGz8Ebcsur2mgOqUEG2e3mPmFtSbHNNRY+YJOksMfZ9237TiLALqofyb7gTRlQrI7dG IJiZXCMJCQX7FYsRC6vu1HASy2TBxAls9lynBuOm86eetL+bLRROMUHgvMeCSBboxmYmI5lqQT7 v3LMb/ X-Google-Smtp-Source: ACHHUZ4JqjzYceEIkWFyZvo0l6WhdresHcPXTU+4WTnaDATDX39wzj4C6g3cmqxt/TDtrIjcavsGdvgUHkYL1DQkuw== X-Received: from elbrus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:12e9]) (user=ppluzhnikov job=sendgmr) by 2002:a05:6902:154f:b0:b78:8bd8:6e77 with SMTP id r15-20020a056902154f00b00b788bd86e77mr5983233ybu.8.1683508977084; Sun, 07 May 2023 18:22:57 -0700 (PDT) Date: Mon, 8 May 2023 01:22:11 +0000 Mime-Version: 1.0 X-Mailer: git-send-email 2.40.1.521.gf1e218fcd8-goog Message-ID: <20230508012211.3770553-1-ppluzhnikov@google.com> Subject: [PATCH] Fix bad interaction between element limit and repeated values (BZ#24331). To: gdb-patches@sourceware.org Cc: Paul Pluzhnikov X-Spam-Status: No, score=-20.3 required=5.0 tests=BAYES_00, DKIMWL_WL_MED, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Paul Pluzhnikov via Gdb-patches From: Paul Pluzhnikov Reply-To: Paul Pluzhnikov Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Sender: "Gdb-patches" Currently print -elements=3 -- "AAAAAA" prints complete string, which is not what the user asked for. Fix two buggy tests exposed by the fix, and add a new test. Reviewed-by: Keith Seitz --- gdb/testsuite/gdb.base/printcmds.exp | 15 +++++++++++++ gdb/testsuite/gdb.python/py-format-string.exp | 2 +- gdb/valprint.c | 22 ++++++++++++++----- 3 files changed, 33 insertions(+), 6 deletions(-) diff --git a/gdb/testsuite/gdb.base/printcmds.exp b/gdb/testsuite/gdb.base/printcmds.exp index db57769c303..f2be549f2a5 100644 --- a/gdb/testsuite/gdb.base/printcmds.exp +++ b/gdb/testsuite/gdb.base/printcmds.exp @@ -1027,6 +1027,9 @@ proc test_repeat_bytes {} { set fmt(SR) "\"%s\", %s" set fmt(SS) "\"%s%s\"" + # We have up to 60 elements to print. + gdb_test_no_output "set print elements 100" + # Set the target-charset to ASCII, because the output varies from # different charset. with_target_charset "ASCII" { @@ -1074,6 +1077,17 @@ proc test_repeat_bytes {} { } } +# Test for BZ#24331 +proc test_repeat_bytes_limit {} { + gdb_test "print -elem 3 -- \"AAAAA\"" "= \"AAA\"..." + gdb_test "print -char 3 -elem 10 -- \"AAAAA\"" "= \"AAA\"..." + gdb_test "print -elem 3 -- \"ABBBB\"" "= \"ABB\"..." + gdb_test "print -char 3 -elem 10 -- \"ABBBB\"" "= \"ABB\"..." + + gdb_test "print -elem 3 -repeats 2 -- \"AAABBB\"" "= 'A' ..." + gdb_test "print -elem 4 -repeats 2 -- \"AAABBB\"" "= 'A' , 'B' " +} + # Test printf of convenience variables. # These tests can be done with or without a running inferior. # PREFIX ensures uniqueness of test names. @@ -1199,3 +1213,4 @@ test_printf_with_strings test_print_symbol test_repeat_bytes test_radices +test_repeat_bytes_limit diff --git a/gdb/testsuite/gdb.python/py-format-string.exp b/gdb/testsuite/gdb.python/py-format-string.exp index 1d50f59dee3..2a011ebd45a 100644 --- a/gdb/testsuite/gdb.python/py-format-string.exp +++ b/gdb/testsuite/gdb.python/py-format-string.exp @@ -748,7 +748,7 @@ proc test_max_string_one { setting unlimited } { # This will print four characters instead of three, see # . check_format_string "a_binary_string_array" $opts \ - "\"hell\"..." + "\"hel\"..." check_format_string "a_big_string" $opts \ [get_cut_big_string 3] if { $setting == "elements"} { diff --git a/gdb/valprint.c b/gdb/valprint.c index b05d90dd26b..b770c93d805 100644 --- a/gdb/valprint.c +++ b/gdb/valprint.c @@ -2363,16 +2363,19 @@ print_converted_chars_to_obstack (struct obstack *obstack, const std::vector &chars, int quote_char, int width, enum bfd_endian byte_order, - const struct value_print_options *options) + const struct value_print_options *options, + int *finished) { - unsigned int idx; + unsigned int idx, num_elements; const converted_character *elem; enum {START, SINGLE, REPEAT, INCOMPLETE, FINISH} state, last; gdb_wchar_t wide_quote_char = gdb_btowc (quote_char); bool need_escape = false; + const int print_max = options->print_max_chars > 0 + ? options->print_max_chars : options->print_max; /* Set the start state. */ - idx = 0; + idx = num_elements = 0; last = state = START; elem = NULL; @@ -2400,7 +2403,13 @@ print_converted_chars_to_obstack (struct obstack *obstack, obstack_grow (obstack, &wide_quote_char, sizeof (gdb_wchar_t)); } /* Output the character. */ - for (j = 0; j < elem->repeat_count; ++j) + int repeat_count = elem->repeat_count; + if (print_max < repeat_count + num_elements) + { + repeat_count = print_max - num_elements; + *finished = 0; + } + for (j = 0; j < repeat_count; ++j) { if (elem->result == wchar_iterate_ok) print_wchar (elem->chars[0], elem->buf, elem->buflen, width, @@ -2408,6 +2417,7 @@ print_converted_chars_to_obstack (struct obstack *obstack, else print_wchar (gdb_WEOF, elem->buf, elem->buflen, width, byte_order, obstack, quote_char, &need_escape); + num_elements += 1; } } break; @@ -2439,6 +2449,7 @@ print_converted_chars_to_obstack (struct obstack *obstack, obstack_grow_wstr (obstack, LCST ("'")); std::string s = string_printf (_(" "), elem->repeat_count); + num_elements += elem->repeat_count; for (j = 0; s[j]; ++j) { gdb_wchar_t w = gdb_btowc (s[j]); @@ -2463,6 +2474,7 @@ print_converted_chars_to_obstack (struct obstack *obstack, print_wchar (gdb_WEOF, elem->buf, elem->buflen, width, byte_order, obstack, 0, &need_escape); obstack_grow_wstr (obstack, LCST (">")); + num_elements += 1; /* We do not attempt to output anything after this. */ state = FINISH; @@ -2597,7 +2609,7 @@ generic_printstr (struct ui_file *stream, struct type *type, /* Print the output string to the obstack. */ print_converted_chars_to_obstack (&wchar_buf, converted_chars, quote_char, - width, byte_order, options); + width, byte_order, options, &finished); if (force_ellipses || !finished) obstack_grow_wstr (&wchar_buf, LCST ("..."));