From patchwork Sat Feb 25 11:45:47 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthew Malcomson X-Patchwork-Id: 19382 Received: (qmail 64142 invoked by alias); 25 Feb 2017 11:45:55 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 64124 invoked by uid 89); 25 Feb 2017 11:45:53 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-25.2 required=5.0 tests=AWL, BAYES_00, FREEMAIL_FROM, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_ASCII_DIVIDERS, RCVD_IN_DNSWL_NONE, RCVD_IN_SORBS_SPAM, SPF_PASS autolearn=ham version=3.3.2 spammy=H*r:sk:19.2017, H*M:825a X-HELO: mail-wm0-f65.google.com Received: from mail-wm0-f65.google.com (HELO mail-wm0-f65.google.com) (74.125.82.65) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Sat, 25 Feb 2017 11:45:51 +0000 Received: by mail-wm0-f65.google.com with SMTP id r18so6569439wmd.3 for ; Sat, 25 Feb 2017 03:45:51 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:cc:from:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding; bh=s64RL78O4lwDvJ7+zqzCsfni4dFhdJromn37pywhy/E=; b=Mqo8lV/6mXpPqEYHNXPC41eMSlJPu22xRUyGNcnjztBD2ifWPeV1PjXBWRVOnlLl14 st9e66Et6CtKhimv41ztjaE5Qp+Dg8o7l7PR+7Yx3veknXziiEwVdQryKKMdn9packU5 x3/sHzyuK7Sg2LbBOJm0cKVGKBrGP5bYkuCrCgNRbCkQZCv28RHF9OsuaAtUM7IVwdY5 IS2SSbeC2Y2Jwn67VTiK2LpQKDs1DmpweQWU2cCoC0TCtP8at22PLDL6/40zcKHyYtKq j1cefFUpIY93K0div32a5OCbU7InzQoAwy4jNcIX3XKdYSZVJ0mPuoslBaJS9wjV95eO ZL3g== X-Gm-Message-State: AMke39lVzBQBOYF0pBi0zulge0Ii+gVhg9OPTuYoFK8MNPWp4pW6+NwUIwr5mSMh4LaNCw== X-Received: by 10.28.134.76 with SMTP id i73mr6483719wmd.51.1488023149549; Sat, 25 Feb 2017 03:45:49 -0800 (PST) Received: from [10.0.0.57] (host86-170-201-9.range86-170.btcentralplus.com. [86.170.201.9]) by smtp.gmail.com with ESMTPSA id 73sm6063740wml.19.2017.02.25.03.45.48 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 25 Feb 2017 03:45:49 -0800 (PST) Subject: Re: [PATCH] fix bug with command `printf "%s\n", $_as_string($pc)` To: Simon Marchi References: <959cdc8e-1e54-a2e7-53d0-d80aaaea9ea8@gmail.com> <1d49ea752aae175256c0278bf3a999bc@polymtl.ca> Cc: gdb-patches@sourceware.org From: Matthew Malcomson Message-ID: <1cbe8b68-b592-825a-c662-56096ef0f795@gmail.com> Date: Sat, 25 Feb 2017 11:45:47 +0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.7.1 MIME-Version: 1.0 In-Reply-To: <1d49ea752aae175256c0278bf3a999bc@polymtl.ca> On 20/02/17 18:19, Simon Marchi wrote: > On 2017-02-19 12:32, Matthew Malcomson wrote: >> diff --git a/gdb/python/py-value.c b/gdb/python/py-value.c >> index eb3d307b19..c786f68865 100644 >> --- a/gdb/python/py-value.c >> +++ b/gdb/python/py-value.c >> @@ -1615,7 +1615,7 @@ convert_value_from_python (PyObject *obj) >> gdb::unique_xmalloc_ptr s >> = python_string_to_target_string (obj); >> if (s != NULL) >> - value = value_cstring (s.get (), strlen (s.get ()), >> + value = value_cstring (s.get (), strlen (s.get ()) + 1, >> builtin_type_pychar); >> } >> else if (PyObject_TypeCheck (obj, &value_object_type)) > > This fix looks good to me. > > One test (py-mi.exp) needs to be updated though. You can run all the > Python-related tests using: > > $ make check TESTS="gdb.python/*.exp" > > Normally, the Python tests all pass reliably, unlike some other parts > of the testsuite. > > It might also be good to improve gdb.python/py-as-string.exp to > include a test for this bug. > > Thanks, > > Simon Thanks -- I had mistakenly ignored the py-mi.exp failure assuming it was nothing to do with me (my apologies). I've included the test fixes you suggested below. ------------------------------------------------ CHANGELOG: 2017-02-19 Matthew Malcomson * python/py-value.c (convert_value_from_python): Include NULL terminator in result. testsuite/gdb.python/py-as-string.c, testsuite/gdb.python/py-as-string.exp: Update tests to account for NULL terminator from python string values. doc/gdb.texinfo ($trace_func): Mention this value can't be used with printf. ------------------------------------------------- PATCH: commit 44fd8bd7af5cf4c6b32846dd78ebfecb7b8d9fa5 Author: Matthew Malcomson Date: Sun Feb 19 14:35:09 2017 +0000 convert_value_from_python include terminating NULL When converting python strings to internal gdb Value strings, the NULL byte was initially left out, this can result in extra data from the inferior being printed when the resulting value is used with printf "%s\n", value diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo index c465dc2f9f..5fb34853f1 100644 --- a/gdb/doc/gdb.texinfo +++ b/gdb/doc/gdb.texinfo @@ -13645,8 +13645,8 @@ The source file for the current trace snapshot. The name of the function containing @code{$tracepoint}. @end table -Note: @code{$trace_file} is not suitable for use in @code{printf}, -use @code{output} instead. +Note: @code{$trace_file} and @code{$trace_file} are not suitable for use in +@code{printf}, use @code{output} instead. Here's a simple example of using these convenience variables for stepping through all the trace snapshots and printing some of their diff --git a/gdb/python/py-value.c b/gdb/python/py-value.c index eb3d307b19..c786f68865 100644 --- a/gdb/python/py-value.c +++ b/gdb/python/py-value.c @@ -1615,7 +1615,7 @@ convert_value_from_python (PyObject *obj) gdb::unique_xmalloc_ptr s = python_string_to_target_string (obj); if (s != NULL) - value = value_cstring (s.get (), strlen (s.get ()), + value = value_cstring (s.get (), strlen (s.get ()) + 1, builtin_type_pychar); } else if (PyObject_TypeCheck (obj, &value_object_type)) diff --git a/gdb/testsuite/gdb.python/py-as-string.c b/gdb/testsuite/gdb.python/py-as-string.c index de2e8a1951..c35a692712 100644 --- a/gdb/testsuite/gdb.python/py-as-string.c +++ b/gdb/testsuite/gdb.python/py-as-string.c @@ -15,6 +15,8 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . */ +#include + enum EnumType { ENUM_VALUE_A, ENUM_VALUE_B, @@ -22,6 +24,19 @@ enum EnumType { ENUM_VALUE_D, }; +static char arena[51] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; + +/* Override malloc() so value_coerce_to_target() gets a known pointer, and we + know we'll see an error if $_as_string() returns a string that isn't NULL + terminated. */ +void *malloc(size_t size) +{ + if (size > sizeof(arena)) + return NULL; + + return arena; +} + static enum EnumType enum_valid = ENUM_VALUE_B; static enum EnumType enum_invalid = 20; diff --git a/gdb/testsuite/gdb.python/py-as-string.exp b/gdb/testsuite/gdb.python/py-as-string.exp index 0c44d5f174..819442834c 100644 --- a/gdb/testsuite/gdb.python/py-as-string.exp +++ b/gdb/testsuite/gdb.python/py-as-string.exp @@ -35,6 +35,12 @@ proc test_as_string { } { gdb_test "p \$_as_string(2)" "\"2\"" gdb_test "p \$_as_string(enum_valid)" "\"ENUM_VALUE_B\"" gdb_test "p \$_as_string(enum_invalid)" "\"20\"" + # Test that the NULL character is included in the returned value. + gdb_test "printf \"%s\\n\", \$_as_string(\"hi\")" "\"hi\"" + # Quote once to define the string, and once for the regexp. + gdb_test "interpreter-exec mi '-var-create test * \$_as_string(\"Hello\")'" \ + "\\^done,name=\"test\",numchild=\"8\",value=\"\\\[8]\",type=\"char \\\[8]\",has_more=\"0\"" + gdb_test "interpreter-exec mi '-var-delete test'" "\\^done,ndeleted=\"1\"" } test_as_string diff --git a/gdb/testsuite/gdb.python/py-mi.exp b/gdb/testsuite/gdb.python/py-mi.exp index 736dc7a0d6..a5ad3f0f44 100644 --- a/gdb/testsuite/gdb.python/py-mi.exp +++ b/gdb/testsuite/gdb.python/py-mi.exp @@ -281,7 +281,7 @@ mi_create_dynamic_varobj nstype2 nstype2 1 \ "create nstype2 varobj" mi_list_varobj_children nstype2 { - { {nstype2.} {} 6 {char \[6\]} } + { {nstype2.} {} 7 {char \[7\]} } } "list children after setting exception flag" mi_create_varobj me me \