From: jayant chauhan <[email protected]>

The check_deprecated_attribute function previously assumed that the
attribute always contained an input (arguments). However, #[deprecated]
is valid without arguments. Accessing the input on a bare #[deprecated]
attribute resulted in a null pointer dereference and a segmentation fault.

This patch adds a guard clause to check if the attribute has input
before attempting to access it, preventing the crash.

Fixes Rust-GCC#4410

gcc/rust/ChangeLog:

        * util/rust-attributes.cc (check_deprecated_attribute): Guard against
        attributes without input.

gcc/testsuite/ChangeLog:

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

Signed-off-by: Jayant Chauhan <[email protected]>
---
This change was merged into the gccrs repository and is posted here for
upstream visibility and potential drive-by review, as requested by GCC
release managers.
Each commit email contains a link to its details on github from where you can
find the Pull-Request and associated discussions.


Commit on github: 
https://github.com/Rust-GCC/gccrs/commit/291a2f40903783fe3544c3cf02065ddeed75defe

The commit has been mentioned in the following pull-request(s):
 - https://github.com/Rust-GCC/gccrs/pull/4419

 gcc/rust/util/rust-attributes.cc         |  3 +++
 gcc/testsuite/rust/compile/issue-4410.rs | 11 +++++++++++
 2 files changed, 14 insertions(+)
 create mode 100644 gcc/testsuite/rust/compile/issue-4410.rs

diff --git a/gcc/rust/util/rust-attributes.cc b/gcc/rust/util/rust-attributes.cc
index e4429d8a5..8da23c5d1 100644
--- a/gcc/rust/util/rust-attributes.cc
+++ b/gcc/rust/util/rust-attributes.cc
@@ -298,6 +298,9 @@ check_doc_attribute (const AST::Attribute &attribute)
 static void
 check_deprecated_attribute (const AST::Attribute &attribute)
 {
+  if (!attribute.has_attr_input ())
+    return;
+
   const auto &input = attribute.get_attr_input ();
 
   if (input.get_attr_input_type () != AST::AttrInput::META_ITEM)
diff --git a/gcc/testsuite/rust/compile/issue-4410.rs 
b/gcc/testsuite/rust/compile/issue-4410.rs
new file mode 100644
index 000000000..9ece6ba41
--- /dev/null
+++ b/gcc/testsuite/rust/compile/issue-4410.rs
@@ -0,0 +1,11 @@
+// { dg-options "-frust-incomplete-and-experimental-compiler-do-not-use -w" }
+#![feature(no_core)]
+#![no_core]
+
+// The bug was that a bare #[deprecated] (with no arguments) caused a Segfault.
+// This test ensures it compiles without crashing.
+
+#[deprecated]
+pub mod a {}
+
+fn main() {}
\ No newline at end of file

base-commit: 7f614dcacecf57c637a5359b87d3b5c6b19fb6e8
-- 
2.52.0

Reply via email to