Skip to content

fix segfault on macro not found#4333

Open
Villosse wants to merge 7 commits intoRust-GCC:masterfrom
Villosse:4187
Open

fix segfault on macro not found#4333
Villosse wants to merge 7 commits intoRust-GCC:masterfrom
Villosse:4187

Conversation

@Villosse
Copy link
Contributor

Fixes #4187

@Villosse Villosse force-pushed the 4187 branch 4 times, most recently from 4905f0c to 9a20b54 Compare December 17, 2025 15:12
Comment on lines -43 to 51
rust_assert (!rules_def.is_marked_for_strip ());
/* Note: We can't safely check if rules_def is marked for strip here because
* rules_def might be a dangling pointer to a deleted macro definition.
* This can happen when a macro definition has an invalid attribute (such as
* #[x] where x is not an attribute macro), causing the item to be deleted
* during attribute processing, but the pointer in in the invocation map still
* exists.
*/
// rust_assert (!rules_def.is_marked_for_strip ());
rust_assert (rules_def.get_macro_rules ().size () > 0);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably should prevent rules_def from becoming a dangling pointer (somehow). Maybe by delaying the insertion of names corresponding to macro rules definitions into namespaces until after attributes on the definitions have been processed?

Villosse and others added 4 commits December 22, 2025 19:35
Fixes Rust-GCC#4187

We don't want to insert a nullpointer in the invocation map.
Also, the check for rules_def marked for strip is not safe as it could
be refering to a dangling pointer.

gcc/rust/ChangeLog:

	* expand/rust-macro-expand.cc (MacroExpander::expand_decl_macro): Remove unsafe check.
	(MacroExpander::expand_invoc): Check if rdef is null.
	* util/rust-hir-map.cc (Mappings::insert_macro_invocation): We
	stop to insert nullpointers to the invocation map.

Signed-off-by: lenny.chiadmi-delage <lenny.chiadmi-delage@epita.fr>
Adds Tests of the issue 4187.

gcc/testsuite/ChangeLog:

	* rust/compile/issue-4187-1.rs: New test.
	* rust/compile/issue-4187-2.rs: New test.

Signed-off-by: lenny.chiadmi-delage <lenny.chiadmi-delage@epita.fr>
gcc/rust/ChangeLog:

	* expand/rust-derive-cmp-common.h: Take ownership of strings in EnumMatchBuilder
	instead of keeping a const reference to them. This matters as the builder is
	used in a lambda but constructed for each iteration of the loop, which gets messy.
gcc/rust/ChangeLog:

	* expand/rust-expand-visitor.cc (enum VectorExpandError): New error enum.
	(derive_item): Switch return type and add better error handling.
	(expand_item_attribute): Likewise.
	(expand_tail_expr): Use better error handling.
	(ExpandVisitor::expand_inner_stmts): Likewise.

gcc/testsuite/ChangeLog:

	* rust/compile/issue-4187-2.rs: Fix test.
@CohenArthur CohenArthur force-pushed the 4187 branch 2 times, most recently from 60a8bfc to c4930eb Compare December 23, 2025 16:56
@CohenArthur CohenArthur self-assigned this Jan 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

segfault DefaultHIRVisitor, macros

3 participants