From patchwork Thu Apr 11 19:19:21 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Keith Seitz X-Patchwork-Id: 32264 Received: (qmail 51357 invoked by alias); 11 Apr 2019 19:19:26 -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 51346 invoked by uid 89); 11 Apr 2019 19:19:26 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-14.2 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, SPF_HELO_PASS autolearn=ham version=3.3.1 spammy=METHODS, sk:string_, substitute, avoided X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Thu, 11 Apr 2019 19:19:24 +0000 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 8BEDE316E90A for ; Thu, 11 Apr 2019 19:19:23 +0000 (UTC) Received: from theo.uglyboxes.com.com (ovpn-116-63.phx2.redhat.com [10.3.116.63]) by smtp.corp.redhat.com (Postfix) with ESMTP id 5AAE31001E7C for ; Thu, 11 Apr 2019 19:19:23 +0000 (UTC) From: Keith Seitz To: gdb-patches@sourceware.org Subject: [PATCH] c++/24367: Infinite recursion of typedef substitution Date: Thu, 11 Apr 2019 12:19:21 -0700 Message-Id: <20190411191921.9525-1-keiths@redhat.com> MIME-Version: 1.0 X-IsSubscribed: yes This is asking for official review of a previous RFC: https://sourceware.org/ml/gdb-patches/2019-03/msg00720.html Revisions from original series: - double check whitespace ----- This bug finds another usage where we end up segfaulting while normalizing user input. inspect_type and replace_type recurse, attempting to substitute the "real" symbol name for the typedef name. However, since the both these names are the same, they keep calling each other until the stack overflows. A simple reproducer for it is given by typedef struct foo foo; int qux (foo *f) { return 0; } (gdb) b qux(foo*) Segmentation fault inspect_type already contains some special handling to prevent a similar situation from occurring with namespaces. I wonder, however, whether we need be so pedantic about the exact nature of the substitution. This patch implements this rather more aggressive assumption that these substitutions should be avoided whenever the replacement symbol's name is exactly the same as the one we're trying to substitute. [In the above example, we're trying to substitute the tyepdef named "foo" with the symbol named "foo" (a struct).] gdb/ChangeLog: PR c++/24367 * cp-support.c (inspect_type): Don't attempt substitutions of symbol with the same name. gdb/testsuite/ChangeLog: PR c++/24367 * gdb.cp/meth-typedefs.cc (incomplete_struct) (another_incomplete_struct, test_incomplete): New definitions. (main): Use new definitions. * gdb.cp/meth-typedefs.exp: Add new tests for `test_incomplete' functions. --- gdb/cp-support.c | 16 +++++++++++++--- gdb/testsuite/gdb.cp/meth-typedefs.cc | 13 +++++++++++++ gdb/testsuite/gdb.cp/meth-typedefs.exp | 5 +++++ 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/gdb/cp-support.c b/gdb/cp-support.c index b79dd5c086..dc807b5292 100644 --- a/gdb/cp-support.c +++ b/gdb/cp-support.c @@ -191,10 +191,20 @@ inspect_type (struct demangle_parse_info *info, /* Get the real type of the typedef. */ type = check_typedef (otype); - /* If the symbol is a namespace and its type name is no different + /* If the symbol name is the same as the original type name, + don't substitute. That would cause infinite recursion in + symbol lookups, as the typedef symbol is often the first + found symbol in the symbol table. + + However, this can happen in a number of situations, such as: + + If the symbol is a namespace and its type name is no different than the name we looked up, this symbol is not a namespace - alias and does not need to be substituted. */ - if (TYPE_CODE (otype) == TYPE_CODE_NAMESPACE + alias and does not need to be substituted. + + If the symbol is typedef and its type name is the same + as the symbol's name, e.g., "typedef struct foo foo;". */ + if (TYPE_NAME (type) != nullptr && strcmp (TYPE_NAME (type), name) == 0) return 0; diff --git a/gdb/testsuite/gdb.cp/meth-typedefs.cc b/gdb/testsuite/gdb.cp/meth-typedefs.cc index 0c4d095c7f..f65478e8c0 100644 --- a/gdb/testsuite/gdb.cp/meth-typedefs.cc +++ b/gdb/testsuite/gdb.cp/meth-typedefs.cc @@ -36,6 +36,13 @@ typedef void (*fptr2) (fptr1, my_other_type_2); typedef void (*fptr3) (fptr2, my_other_type); typedef void (*fptr4) (anon_enum a, anon_struct const& b, anon_union const*** c); +// For c++/24367 testing +typedef struct incomplete_struct incomplete_struct; +typedef struct _incomplete_struct another_incomplete_struct; +int test_incomplete (incomplete_struct *p) { return 0; } // test_incomplete(incomplete_struct*) +int test_incomplete (another_incomplete_struct *p) { return 1; } // test_incomplete(another_incomplete_struct*) +int test_incomplete (int *p) { return -1; } // test_incomplete(int*) + namespace A { class foo @@ -147,5 +154,11 @@ main (void) fptr4 f4; + // Tests for c++/24367 + int *i = nullptr; + incomplete_struct *is = nullptr; + another_incomplete_struct *ais = nullptr; + int result = (test_incomplete (i) + test_incomplete (is) + + test_incomplete (ais)); return 0; } diff --git a/gdb/testsuite/gdb.cp/meth-typedefs.exp b/gdb/testsuite/gdb.cp/meth-typedefs.exp index b9c383bb25..76a8fc9780 100644 --- a/gdb/testsuite/gdb.cp/meth-typedefs.exp +++ b/gdb/testsuite/gdb.cp/meth-typedefs.exp @@ -137,6 +137,11 @@ foreach t $typedefs(_BAR_) { add methods "test" "$t&" {_BAR_&} } +# Tests for c++/24367 +foreach t {int incomplete_struct another_incomplete_struct} { + add methods "test_incomplete" "${t}*" [string_to_regexp "${t}*"] +} + gdb_test_no_output "set listsize 1" "" # Finally, for each method in the list METHODS, check whether