]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: fix ICE segfault with empty feature gate
authorMatty Kuhn <matty.kuhn.1@gmail.com>
Sat, 5 Apr 2025 00:09:41 +0000 (18:09 -0600)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 8 Apr 2025 08:17:20 +0000 (10:17 +0200)
This patch fixes an issue where an empty feature gate would segfault,
instead of reporting a syntax error to the user.

gcc/rust/ChangeLog:

* ast/rust-ast.h: (AST::Attribute): add empty_input function
* checks/errors/rust-feature-gate.cc: (FeatureGate::visit): check for empty feature gate

gcc/testsuite/ChangeLog:

* rust/compile/feature.rs: add an invalid empty feature to produce an error

Signed-off-by: Matty Kuhn <matty.kuhn.1@gmail.com>
gcc/rust/ast/rust-ast.h
gcc/rust/checks/errors/rust-feature-gate.cc
gcc/testsuite/rust/compile/feature.rs

index 1091ba0593e0559f5257e53e2f1d094296167622..09e0fce4f1906bb19194cc06baf8189b7ed61491 100644 (file)
@@ -657,6 +657,9 @@ public:
   // Returns whether the attribute is considered an "empty" attribute.
   bool is_empty () const { return attr_input == nullptr && path.is_empty (); }
 
+  // Returns whether the attribute has no input
+  bool empty_input () const { return !attr_input; }
+
   location_t get_locus () const { return locus; }
 
   AttrInput &get_attr_input () const { return *attr_input; }
index f3daa61f170b13dd17e4bb4c1f962a771132da8e..44007f99e5cfb418beb0a023e2a4d20e230b6ffa 100644 (file)
@@ -40,6 +40,13 @@ FeatureGate::visit (AST::Crate &crate)
     {
       if (attr.get_path ().as_string () == "feature")
        {
+         // check for empty feature, such as `#![feature], this is an error
+         if (attr.empty_input ())
+           {
+             rust_error_at (attr.get_locus (), ErrorCode::E0556,
+                            "malformed %<feature%> attribute input");
+             continue;
+           }
          const auto &attr_input = attr.get_attr_input ();
          auto type = attr_input.get_attr_input_type ();
          if (type == AST::AttrInput::AttrInputType::TOKEN_TREE)
index f743f9229b689e1e1ab1d05186581dc3d6e1a5ea..6f428f075a1b754c0d9df1b8c0cb4a10327cee27 100644 (file)
@@ -2,5 +2,7 @@
 #![feature(AA)] //{ dg-error "unknown feature .AA." }
 #![feature(iamcrabby)] // { dg-error "unknown feature .iamcrabby." }
 #![feature(nonexistent_gccrs_feature)] // { dg-error "unknown feature .nonexistent_gccrs_feature." }
+// ErrorCode - E0556
+#![feature] // { dg-error "malformed .feature. attribute input" }
 
 fn main() {}