]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: fix segfault on empty doc attribute
authorLucas Ly Ba <lucas.ly-ba@outlook.com>
Fri, 7 Nov 2025 16:38:46 +0000 (16:38 +0000)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 25 Nov 2025 22:00:51 +0000 (23:00 +0100)
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
gcc/testsuite/rust/compile/issue-4226.rs [new file with mode: 0644]

index 1c8e5b6d2c0e2c70db17f96c33ec9446bf0fa2f7..8984c98daaacd08660b560893f4b8ad00495adcd 100644 (file)
@@ -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 (file)
index 0000000..553faf2
--- /dev/null
@@ -0,0 +1,3 @@
+#[doc]
+// { dg-error "attribute must be of the form ...doc.hidden.inline....... or ...doc = string.." "" { target *-*-* } .-1 }
+pub fn a(){}