From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id iWlyNw2V12pzDTsAWB0awg (envelope-from ) for ; Tue, 20 Oct 2026 12:21:33 -0400 Received: by simark.ca (Postfix, from userid 112) id 9CAE61E06B; Tue, 20 Oct 2026 12:21:32 -0400 (EDT) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-5.3 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_00, MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED autolearn=ham autolearn_force=no version=4.0.1 Received: from vm01.sourceware.org (vm01.sourceware.org [IPv6:2620:52:6:3111::32]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id 66C611E090 for ; Tue, 20 Oct 2026 12:21:28 -0400 (EDT) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 920234A9A8A7 for ; Tue, 20 Oct 2026 16:21:27 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 920234A9A8A7 Received: from simark.ca (simark.ca [158.69.221.121]) by sourceware.org (Postfix) with ESMTPS id 7B14F4A9C01B for ; Tue, 20 Oct 2026 16:20:59 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7B14F4A9C01B Authentication-Results: sourceware.org; dmarc=fail (p=none dis=none) header.from=efficios.com Authentication-Results: sourceware.org; spf=fail smtp.mailfrom=efficios.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 7B14F4A9C01B Authentication-Results: sourceware.org; arc=none smtp.remote-ip=158.69.221.121 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1792513259; cv=none; b=glHEihzc/S/dxNUlkBUKlpU07XTuAbzgqeiY1V14L7fgPaHgmZjypOKnjcLi3xaA4gQ4YAfwWwjqjOxQyvRybFU4I5nX0DYbNRaB/DY5nEjvX11p0toW6jKB+fxtiHfmDCCOmHUkyoHDG8n51p5BWiKOSZW/mM9DndaIEdocbuc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1792513259; c=relaxed/simple; bh=dLz5NE6Gyadpzv8wVr8Y0y8+yGdHKi43l1Orh6alHz4=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=ETPnxmSQiVmneCwN5QJB1c8EZ/+1gG/9jGzmyDzXXeQap9mtbQmE5GbhWQIcEiW8uouJ8iETM9uhyKetgraihHC/xSTjo/4DUuFJpMBBMsIVG0EFuQfVVHuNbYUc1yB2gDSLn5owxhdYAFpxygOdphGyJT+36VIy0jjgBZnJVQw= ARC-Authentication-Results: i=1; sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7B14F4A9C01B Received: by simark.ca (Postfix) id 01AF91E04F; Tue, 20 Oct 2026 12:20:57 -0400 (EDT) From: Simon Marchi To: gdb-patches@sourceware.org Cc: Simon Marchi Subject: [PATCH 01/13] gdb: make demangle_parse_info own the memory its tree points into Date: Tue, 20 Oct 2026 12:17:23 -0400 Message-ID: <20261020162054.2140671-2-simon.marchi@efficios.com> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261020162054.2140671-1-simon.marchi@efficios.com> References: <20261020162054.2140671-1-simon.marchi@efficios.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces~public-inbox=simark.ca@sourceware.org mangled_name_to_comp returns, through output parameters, some memory that the caller needs to hold onto while it uses the returned demangle_component. The `memory` parameter is used if we take the gdb_cplus_demangle_v3_components branch, while the `demangled_p` parameter is used if we take the fallback. To make it easier for the callers, move these two fields inside demangle_parse_info. demangle_parse_info already contains other objects whose lifetime is tied to the demangle_component. Change-Id: Ib22c73d07706096771ba74284da19f8597497286 --- gdb/cp-support.c | 59 ++++++++++++++++++++++-------------------------- gdb/cp-support.h | 7 ++++++ 2 files changed, 34 insertions(+), 32 deletions(-) diff --git a/gdb/cp-support.c b/gdb/cp-support.c index 7a1b07a3b161..0f1ead6a4e98 100644 --- a/gdb/cp-support.c +++ b/gdb/cp-support.c @@ -75,9 +75,8 @@ static void canonicalization_ftype *finder, void *data); -static struct demangle_component * - gdb_cplus_demangle_v3_components (const char *mangled, - int options, void **mem); +static demangle_parse_info_up gdb_cplus_demangle_v3_components + (const char *mangled, int options); /* A convenience function to copy STRING into OBSTACK, returning a pointer to the newly allocated string and saving the number of bytes saved in LEN. @@ -665,32 +664,23 @@ cp_canonicalize_string (const char *string) return us; } -/* Convert a mangled name to a demangle_component tree. *MEMORY is - set to the block of used memory that should be freed when finished - with the tree. DEMANGLED_P is set to the char * that should be - freed when finished with the tree, or NULL if none was needed. - OPTIONS will be passed to the demangler. */ +/* Convert a mangled name to a demangle_component tree. OPTIONS is passed to + the demangler. + + The returned tree may point into MANGLED_NAME, so MANGLED_NAME must + outlive it. */ static demangle_parse_info_up -mangled_name_to_comp (const char *mangled_name, int options, - void **memory, - gdb::unique_xmalloc_ptr *demangled_p) +mangled_name_to_comp (const char *mangled_name, int options) { /* If it looks like a v3 mangled name, then try to go directly to trees. */ if (mangled_name[0] == '_' && mangled_name[1] == 'Z') { - struct demangle_component *ret; - - ret = gdb_cplus_demangle_v3_components (mangled_name, - options, memory); - if (ret) - { - auto info = std::make_unique (); - info->tree = ret; - *demangled_p = NULL; - return info; - } + if (demangle_parse_info_up info + = gdb_cplus_demangle_v3_components (mangled_name, options); + info != nullptr) + return info; } /* If it doesn't, or if that failed, then try to demangle the @@ -708,7 +698,7 @@ mangled_name_to_comp (const char *mangled_name, int options, if (info == NULL) return NULL; - *demangled_p = std::move (demangled_name); + info->demangled_name = std::move (demangled_name); return info; } @@ -717,12 +707,9 @@ mangled_name_to_comp (const char *mangled_name, int options, gdb::unique_xmalloc_ptr cp_class_name_from_physname (const char *physname) { - void *storage = NULL; - gdb::unique_xmalloc_ptr demangled_name; gdb::unique_xmalloc_ptr ret; struct demangle_component *ret_comp, *prev_comp, *cur_comp; - demangle_parse_info_up info - = mangled_name_to_comp (physname, DMGL_ANSI, &storage, &demangled_name); + demangle_parse_info_up info = mangled_name_to_comp (physname, DMGL_ANSI); int done; if (info == nullptr) @@ -796,7 +783,6 @@ cp_class_name_from_physname (const char *physname) ret = cp_comp_to_string (ret_comp, 10); } - xfree (storage); return ret; } @@ -1666,11 +1652,20 @@ gdb_cplus_demangle_print (int options, /* A wrapper for cplus_demangle_v3_components that forces DMGL_VERBOSE. */ -static struct demangle_component * -gdb_cplus_demangle_v3_components (const char *mangled, - int options, void **mem) +static demangle_parse_info_up +gdb_cplus_demangle_v3_components (const char *mangled, int options) { - return cplus_demangle_v3_components (mangled, options | DMGL_VERBOSE, mem); + void *storage; + demangle_component *tree + = cplus_demangle_v3_components (mangled, options | DMGL_VERBOSE, + &storage); + if (tree == nullptr) + return nullptr; + + auto info = std::make_unique (); + info->tree = tree; + info->demangler_storage.reset (storage); + return info; } /* See cp-support.h. */ diff --git a/gdb/cp-support.h b/gdb/cp-support.h index 00703c52c880..103cbf0cd652 100644 --- a/gdb/cp-support.h +++ b/gdb/cp-support.h @@ -67,6 +67,13 @@ struct demangle_parse_info /* Any memory used during processing. */ auto_obstack obstack; + /* Memory allocated by cplus_demangle_v3_components that TREE points + into, if any. */ + gdb::unique_xmalloc_ptr demangler_storage; + + /* The demangled name TREE was parsed from and points into, if any. */ + gdb::unique_xmalloc_ptr demangled_name; + /* True if the parser had to add a dummy '()' at the end of the input text to make it parse. */ bool added_parens = false; -- 2.56.0