Skip to content

Commit

Permalink
Use rust_demangle to fix a crash
Browse files Browse the repository at this point in the history
PR rust/30211 points out a crash caused by a particular completion.
This turns out to happen because a Rust minsym winds up in a
C++-specific path in strncmp_iw_with_mode, which ultimately causes the
completer to pass invalid arguments to string::append.

This patch fixes the bug by reordering the language constants so that
Rust comes before C++, and then using rust_demangle.  This ensures
that minsyms are correctly marked as "Rust", avoiding this code and
thus the crash.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=20367
Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=30211
Reviewed-By: Andrew Burgess <[email protected]>
  • Loading branch information
tromey committed Mar 20, 2023
1 parent 0fea10f commit 6e7eef7
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
7 changes: 3 additions & 4 deletions gdb/defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -204,16 +204,16 @@ extern void quit_serial_event_clear (void);
these languages, so some symbols could be successfully demangled by
several languages. For that reason, the constants here are sorted
in the order we'll attempt demangling them. For example: Rust uses
C++ mangling, so must come after C++; Ada must come last (see
ada_sniff_from_mangled_name). (Keep this order in sync with the
'languages' array in language.c.) */
a C++-compatible mangling, so must come before C++; Ada must come
last (see ada_sniff_from_mangled_name). */

enum language
{
language_unknown, /* Language not known */
language_auto, /* Placeholder for automatic setting */
language_c, /* C */
language_objc, /* Objective-C */
language_rust, /* Rust */
language_cplus, /* C++ */
language_d, /* D */
language_go, /* Go */
Expand All @@ -222,7 +222,6 @@ enum language
language_asm, /* Assembly language */
language_pascal, /* Pascal */
language_opencl, /* OpenCL */
language_rust, /* Rust */
language_minimal, /* All other languages, minimal support only */
language_ada, /* Ada */
nr_languages
Expand Down
4 changes: 2 additions & 2 deletions gdb/rust-lang.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class rust_language : public language_defn
(const char *mangled, gdb::unique_xmalloc_ptr<char> *demangled)
const override
{
*demangled = gdb_demangle (mangled, DMGL_PARAMS | DMGL_ANSI);
demangled->reset (rust_demangle (mangled, 0));
return *demangled != NULL;
}

Expand All @@ -102,7 +102,7 @@ class rust_language : public language_defn
gdb::unique_xmalloc_ptr<char> demangle_symbol (const char *mangled,
int options) const override
{
return gdb_demangle (mangled, options);
return gdb::unique_xmalloc_ptr<char> (rust_demangle (mangled, options));
}

/* See language.h. */
Expand Down
3 changes: 3 additions & 0 deletions gdb/testsuite/gdb.rust/methods.exp
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,6 @@ gdb_test "print *self" " = 23"
gdb_test "info functions HasMethods::new" \
"fn methods::HasMethods::new\\(\\) -> methods::HasMethods;"

# Regression test for PR rust/20367 and PR rust/30211. This used to
# crash.
gdb_test_no_output "complete break what"

0 comments on commit 6e7eef7

Please sign in to comment.