From: Simon Marchi <simon.marchi@efficios.com>
To: gdb-patches@sourceware.org
Cc: Simon Marchi <simon.marchi@efficios.com>
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 [thread overview]
Message-ID: <20261020162054.2140671-2-simon.marchi@efficios.com> (raw)
In-Reply-To: <20261020162054.2140671-1-simon.marchi@efficios.com>
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<char> *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<demangle_parse_info> ();
- 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<char>
cp_class_name_from_physname (const char *physname)
{
- void *storage = NULL;
- gdb::unique_xmalloc_ptr<char> demangled_name;
gdb::unique_xmalloc_ptr<char> 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<demangle_parse_info> ();
+ 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<void> demangler_storage;
+
+ /* The demangled name TREE was parsed from and points into, if any. */
+ gdb::unique_xmalloc_ptr<char> 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
next prev parent reply other threads:[~2026-10-20 16:21 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-20 16:17 [PATCH 00/13] Make the C++ name parser recognize more char literal forms Simon Marchi
2026-10-20 16:17 ` Simon Marchi [this message]
2026-10-20 16:17 ` [PATCH 02/13] gdb: remove p_allocated_size parameter of gdb_cplus_demangle_print Simon Marchi
2026-10-20 16:17 ` [PATCH 03/13] gdb: modernize mangled_name_to_comp and cp_class_name_from_physname Simon Marchi
2026-10-20 16:17 ` [PATCH 04/13] gdb: fix crash in "maint canonicalize" without argument Simon Marchi
2026-10-20 16:17 ` [PATCH 05/13] gdb: move C++ name parser parsing selftest to its own test Simon Marchi
2026-10-20 16:17 ` [PATCH 06/13] gdb: check exact results in the canonicalize selftest Simon Marchi
2026-10-21 15:21 ` Tom Tromey
2026-10-21 15:48 ` Simon Marchi
2026-10-21 16:56 ` Tom Tromey
2026-10-21 17:01 ` Simon Marchi
2026-10-21 17:48 ` Tom Tromey
2026-10-22 16:28 ` Simon Marchi
2026-10-20 16:17 ` [PATCH 07/13] gdb: extract lex_char_literal from cpname_yylex Simon Marchi
2026-10-21 15:22 ` Tom Tromey
2026-10-20 16:17 ` [PATCH 08/13] gdb: remove \^ escape sequence support from C++ name parser Simon Marchi
2026-10-20 16:17 ` [PATCH 09/13] gdb: remove backslash-newline handling from cp_parse_escape Simon Marchi
2026-10-20 16:17 ` [PATCH 10/13] gdb: fix out-of-bounds reads in lex_char_literal Simon Marchi
2026-10-20 16:17 ` [PATCH 11/13] gdb: handle hexadecimal escapes in C++ name parser Simon Marchi
2026-10-20 16:17 ` [PATCH 12/13] gdb: handle character literal encoding prefixes " Simon Marchi
2026-10-20 16:17 ` [PATCH 13/13] gdb: handle universal character names " Simon Marchi
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261020162054.2140671-2-simon.marchi@efficios.com \
--to=simon.marchi@efficios.com \
--cc=gdb-patches@sourceware.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox