[COMMITTED 08/12] gccrs: fix segfault on empty doc attribute

arthur.cohen@embecosm.com arthur.cohen@embecosm.com
Tue Nov 25 22:04:56 GMT 2025


From: Lucas Ly Ba <lucas.ly-ba@outlook.com>

gcc/rust/ChangeLog:

	* hir/rust-ast-lower-base.cc (ASTLoweringBase::handle_doc_item_attribute): Make error.

gcc/testsuite/ChangeLog:

	* rust/compile/issue-4226.rs: New test.

Signed-off-by: Lucas Ly Ba <lucas.ly-ba@outlook.com>
---
 gcc/rust/hir/rust-ast-lower-base.cc      | 14 +++++++++++---
 gcc/testsuite/rust/compile/issue-4226.rs |  3 +++
 2 files changed, 14 insertions(+), 3 deletions(-)
 create mode 100644 gcc/testsuite/rust/compile/issue-4226.rs

diff --git a/gcc/rust/hir/rust-ast-lower-base.cc b/gcc/rust/hir/rust-ast-lower-base.cc
index 1c8e5b6d2c0..8984c98daaa 100644
--- a/gcc/rust/hir/rust-ast-lower-base.cc
+++ b/gcc/rust/hir/rust-ast-lower-base.cc
@@ -820,9 +820,17 @@ void
 ASTLoweringBase::handle_doc_item_attribute (const ItemWrapper &,
 					    const AST::Attribute &attr)
 {
-  auto simple_doc_comment = attr.has_attr_input ()
-			    && attr.get_attr_input ().get_attr_input_type ()
-				 == AST::AttrInput::AttrInputType::LITERAL;
+  if (!attr.has_attr_input ())
+    {
+      rust_error_at (attr.get_locus (),
+		     "attribute must be of the form %qs or %qs",
+		     "#[doc(hidden|inline|...)]", "#[doc = string]");
+      return;
+    }
+
+  auto simple_doc_comment = attr.get_attr_input ().get_attr_input_type ()
+			    == AST::AttrInput::AttrInputType::LITERAL;
+
   if (simple_doc_comment)
     return;
 
diff --git a/gcc/testsuite/rust/compile/issue-4226.rs b/gcc/testsuite/rust/compile/issue-4226.rs
new file mode 100644
index 00000000000..553faf291f4
--- /dev/null
+++ b/gcc/testsuite/rust/compile/issue-4226.rs
@@ -0,0 +1,3 @@
+#[doc]
+// { dg-error "attribute must be of the form ...doc.hidden.inline....... or ...doc = string.." "" { target *-*-* } .-1 }
+pub fn a(){}
-- 
2.50.1



More information about the Gcc-rust mailing list