[COMMITTED 34/35] gccrs: fix crash in parse repr options and missing delete call

arthur.cohen@embecosm.com arthur.cohen@embecosm.com
Mon Mar 31 19:25:16 GMT 2025


From: Philip Herron <herron.philip@googlemail.com>

Fixes Rust-GCC#3606

gcc/rust/ChangeLog:

	* typecheck/rust-hir-type-check-base.cc (TypeCheckBase::parse_repr_options):
	check for null and empty and add missing delete call

gcc/testsuite/ChangeLog:

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

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
---
 .../typecheck/rust-hir-type-check-base.cc     | 20 +++++++++++++++++--
 gcc/testsuite/rust/compile/issue-3606.rs      |  6 ++++++
 2 files changed, 24 insertions(+), 2 deletions(-)
 create mode 100644 gcc/testsuite/rust/compile/issue-3606.rs

diff --git a/gcc/rust/typecheck/rust-hir-type-check-base.cc b/gcc/rust/typecheck/rust-hir-type-check-base.cc
index 7fc6467e8ae..34a726cc665 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-base.cc
+++ b/gcc/rust/typecheck/rust-hir-type-check-base.cc
@@ -321,8 +321,22 @@ TypeCheckBase::parse_repr_options (const AST::AttrVec &attrs, location_t locus)
 	  AST::AttrInputMetaItemContainer *meta_items
 	    = option.parse_to_meta_item ();
 
-	  const std::string inline_option
-	    = meta_items->get_items ().at (0)->as_string ();
+	  if (meta_items == nullptr)
+	    {
+	      rust_error_at (attr.get_locus (), "malformed %qs attribute",
+			     "repr");
+	      continue;
+	    }
+
+	  auto &items = meta_items->get_items ();
+	  if (items.size () == 0)
+	    {
+	      // nothing to do with this its empty
+	      delete meta_items;
+	      continue;
+	    }
+
+	  const std::string inline_option = items.at (0)->as_string ();
 
 	  // TODO: it would probably be better to make the MetaItems more aware
 	  // of constructs with nesting like #[repr(packed(2))] rather than
@@ -359,6 +373,8 @@ TypeCheckBase::parse_repr_options (const AST::AttrVec &attrs, location_t locus)
 	  else if (is_align)
 	    repr.align = value;
 
+	  delete meta_items;
+
 	  // Multiple repr options must be specified with e.g. #[repr(C,
 	  // packed(2))].
 	  break;
diff --git a/gcc/testsuite/rust/compile/issue-3606.rs b/gcc/testsuite/rust/compile/issue-3606.rs
new file mode 100644
index 00000000000..73b0bd6743b
--- /dev/null
+++ b/gcc/testsuite/rust/compile/issue-3606.rs
@@ -0,0 +1,6 @@
+// { dg-options "-w" }
+#[repr()]
+pub struct Coord {
+    x: u32,
+    y: u32,
+}
-- 
2.49.0



More information about the Gcc-rust mailing list