From patchwork Sun Feb 19 17:32:42 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthew Malcomson X-Patchwork-Id: 19296 Received: (qmail 28784 invoked by alias); 19 Feb 2017 17:32:49 -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 28774 invoked by uid 89); 19 Feb 2017 17:32:48 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-25.6 required=5.0 tests=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=slight, ensured, prohibited X-HELO: mail-wr0-f194.google.com Received: from mail-wr0-f194.google.com (HELO mail-wr0-f194.google.com) (209.85.128.194) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Sun, 19 Feb 2017 17:32:47 +0000 Received: by mail-wr0-f194.google.com with SMTP id q39so9818522wrb.2 for ; Sun, 19 Feb 2017 09:32:47 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:subject:to:message-id:date:user-agent :mime-version:content-transfer-encoding; bh=wHx/8jcSk3S8IQls0WL3sg7yGy6OyxoxAMvhXn5xYVI=; b=fwg4XkStdb/vzg7bI4IzWwqtUBSZ7LV6jzlvSWEP7/grSXmFD72V6eAOi7Am6kieZm NzmnzEPbvEAedogj/3NLQVVhpo58tEMl2GXrYgzj+UiDqzBmQ52Rnk3gcz8cqg5UXU5i X5u9KFtl/9RSSsrGFqh86HjbIe/lN7b7u4h+/MDhWq5ZlZizy55ktm73PBLESSXkD6dF HFgXKRkaPZnNLTN5PV9fnVNmXU1x5YQY8kIyljnMG7z0ZmLlEHp+Fuurdj/OfhI6gScX mlSIAr1XejVWNwSmSg9D/pkgXweb+SKGwkktXcJuYM6ciyelwMUEmqZZVb4qoRsZ+uW0 xsdw== X-Gm-Message-State: AMke39n1sKCiZ6225RbijwZoifSrlMD32QjNcQLntP/Tz/e5RWjM2lnzT3z5tGeATv6UGg== X-Received: by 10.223.166.181 with SMTP id t50mr12650106wrc.80.1487525564663; Sun, 19 Feb 2017 09:32:44 -0800 (PST) Received: from [10.0.0.57] (host86-172-174-119.range86-172.btcentralplus.com. [86.172.174.119]) by smtp.gmail.com with ESMTPSA id c57sm14552564wra.47.2017.02.19.09.32.43 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 19 Feb 2017 09:32:44 -0800 (PST) From: Matthew Malcomson Subject: [PATCH] fix bug with command `printf "%s\n", $_as_string($pc)` To: gdb-patches@sourceware.org Message-ID: <959cdc8e-1e54-a2e7-53d0-d80aaaea9ea8@gmail.com> Date: Sun, 19 Feb 2017 17:32:42 +0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.7.1 MIME-Version: 1.0 Automatic conversion of python string to gdb value leaves off the NULL byte. This doesn't matter with `print $_as_string($pc)` because the print command uses `value_print()` which limits how many characters are printed based on the length of the array. The command `printf "%s\n", $_as_string($pc)` instead writes the value into the inferior with `value_coerce_to_target()` and then treats the string as a NULL terminated string in the inferior. As the value created from `$_as_string($pc)` is not NULL terminated, this sometimes ends up printing extra characters (depending on where the inferior allocated the memory for this string). `printf "%s\n", "Some string"` doesn't have this problem because the gdb value type created here includes the NULL terminating byte, as is ensured in `evaluate_subexp_c()` file and line c-lang.c:677 (at the time of writing this), commented as `/* Write the terminating character. */`. There are two other places where a terminating NULL byte is not included when calling `value_cstring()`. When converting from a guile string into a gdb internal value structure. This doesn't cause any observable bugs because the string can't be stored in a variable (as there's no equivalent of the python `gdb.Function` class) and hence can't be passed to the `printf` command. There appears to have been some thought about terminating NULL bytes commented above `gdbscm_scm_to_string()`. Given there are no observable bugs and there has already been some thought about the NULL byte in this area I've not changed anything there. In `value_of_internalvar()` when the variable is of kind `INTERNALVAR_STRING`. There are only two internal variables of this kind, `$trace_func` and `$trace_file`. The info documentation says that `$trace_file` is not suitable for use in `printf`, and I believe the same should be said about `$trace_func`. Both are prohibited by the check on `get_traceframe_number()` in `call_function_by_hand_dummy()` that is called from `value_coerce_to_target()` during printf on string variables. I have made a slight change to the documentation here. ------------------------------------------------ CHANGELOG: 2017-02-19 Matthew Malcomson * python/py-value.c (convert_value_from_python): Include NULL terminator in result. doc/gdb.texinfo ($trace_func): Mention this value can't be used with printf. ------------------------------------------------- PATCH: commit 5a0ceb374621de8eb2264c6b9ae92cf936a66f6b 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_func} 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))