Skip to content

Commit 1d93ebb

Browse files
committed
gccrs: fix crash in parse repr options and missing delete call
Fixes #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>
1 parent 8b5f1f2 commit 1d93ebb

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

gcc/rust/typecheck/rust-hir-type-check-base.cc

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,8 +321,22 @@ TypeCheckBase::parse_repr_options (const AST::AttrVec &attrs, location_t locus)
321321
AST::AttrInputMetaItemContainer *meta_items
322322
= option.parse_to_meta_item ();
323323

324-
const std::string inline_option
325-
= meta_items->get_items ().at (0)->as_string ();
324+
if (meta_items == nullptr)
325+
{
326+
rust_error_at (attr.get_locus (), "malformed %qs attribute",
327+
"repr");
328+
continue;
329+
}
330+
331+
auto &items = meta_items->get_items ();
332+
if (items.size () == 0)
333+
{
334+
// nothing to do with this its empty
335+
delete meta_items;
336+
continue;
337+
}
338+
339+
const std::string inline_option = items.at (0)->as_string ();
326340

327341
// TODO: it would probably be better to make the MetaItems more aware
328342
// 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)
359373
else if (is_align)
360374
repr.align = value;
361375

376+
delete meta_items;
377+
362378
// Multiple repr options must be specified with e.g. #[repr(C,
363379
// packed(2))].
364380
break;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// { dg-options "-w" }
2+
#[repr()]
3+
pub struct Coord {
4+
x: u32,
5+
y: u32,
6+
}

0 commit comments

Comments
 (0)