Skip to content

Commit e4a7b46

Browse files
committed
Reject empty cfg_attr
gcc/rust/ChangeLog: * ast/rust-ast.cc (Attribute::check_cfg_predicate): Emit an error with empty cfg_attr input. gcc/testsuite/ChangeLog: * rust/compile/issue-3966.rs: New test. Signed-off-by: Pierre-Emmanuel Patry <[email protected]>
1 parent d80cc0f commit e4a7b46

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

gcc/rust/ast/rust-ast.cc

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4165,11 +4165,12 @@ AttrInputMetaItemContainer::separate_cfg_attrs () const
41654165
bool
41664166
Attribute::check_cfg_predicate (const Session &session) const
41674167
{
4168+
auto string_path = path.as_string ();
41684169
/* assume that cfg predicate actually can exist, i.e. attribute has cfg or
41694170
* cfg_attr path */
41704171
if (!has_attr_input ()
4171-
|| (path.as_string () != Values::Attributes::CFG
4172-
&& path.as_string () != Values::Attributes::CFG_ATTR))
4172+
|| (string_path != Values::Attributes::CFG
4173+
&& string_path != Values::Attributes::CFG_ATTR))
41734174
{
41744175
// DEBUG message
41754176
rust_debug (
@@ -4185,6 +4186,13 @@ Attribute::check_cfg_predicate (const Session &session) const
41854186
return false;
41864187

41874188
auto &meta_item = static_cast<AttrInputMetaItemContainer &> (*attr_input);
4189+
if (meta_item.get_items ().empty ()
4190+
&& string_path == Values::Attributes::CFG_ATTR)
4191+
{
4192+
rust_error_at (path.get_locus (),
4193+
"malformed %<cfg_attr%> attribute input");
4194+
return false;
4195+
}
41884196
return meta_item.get_items ().front ()->check_cfg_predicate (session);
41894197
}
41904198

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
struct S {
2+
#[cfg_attr()]
3+
field: u8,
4+
// { dg-error "malformed .cfg_attr. attribute input" "" { target *-*-* } .-2 }
5+
}

0 commit comments

Comments
 (0)