]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: parser: fix ICE std::out_of_range with path attrs to nonexisting path
authorVishruth-Thimmaiah <vishruththimmaiah@gmail.com>
Tue, 22 Apr 2025 14:34:22 +0000 (20:04 +0530)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 5 Aug 2025 14:36:44 +0000 (16:36 +0200)
Stops an ICE from occuring when path attribute is empty

Fixes Rust-GCC#3607.

gcc/rust/ChangeLog:

* parse/rust-parse.cc (Rust::extract_module_path):
Handle empty or whitespace-only path attributes.

gcc/testsuite/ChangeLog:

* rust/compile/torture/extern_mod2.rs:
New test to ensure an error is emitted for empty path attributes.

Signed-off-by: Vishruth Thimmaiah <vishruththimmaiah@gmail.com>
gcc/rust/parse/rust-parse.cc
gcc/testsuite/rust/compile/torture/extern_mod2.rs

index 4895c6afdaa05b9a2e899f8ea0929d82531144ae..860fd11612adb8860ae557cdbad2a2c842e9f295 100644 (file)
@@ -42,8 +42,7 @@ extract_module_path (const AST::AttrVec &inner_attrs,
     {
       rust_error_at (
        path_attr.get_locus (),
-       // Split the format string so that -Wformat-diag does not complain...
-       "path attributes must contain a filename: '%s'", "#![path = \"file\"]");
+       "path attributes must contain a filename: %<#[path = \"file\"]%>");
       return name;
     }
 
@@ -67,8 +66,7 @@ extract_module_path (const AST::AttrVec &inner_attrs,
     {
       rust_error_at (
        path_attr.get_locus (),
-       // Split the format string so that -Wformat-diag does not complain...
-       "path attributes must contain a filename: '%s'", "#[path = \"file\"]");
+       "path attributes must contain a filename: %<#[path = \"file\"]%>");
       return name;
     }
 
@@ -80,6 +78,15 @@ extract_module_path (const AST::AttrVec &inner_attrs,
   // a character that is not an equal sign or whitespace
   auto filename_begin = path_value.find_first_not_of ("=\t ");
 
+  // If the path consists of only whitespace, then we have an error
+  if (filename_begin == std::string::npos)
+    {
+      rust_error_at (
+       path_attr.get_locus (),
+       "path attributes must contain a filename: %<#[path = \"file\"]%>");
+      return name;
+    }
+
   auto path = path_value.substr (filename_begin);
 
   // On windows, the path might mix '/' and '\' separators. Replace the
index 4984d5dc2c14b063ffe9e99ae966b7328a946a25..f3a4f799c3ee10174cc31cf22e19b892c2da8d7f 100644 (file)
@@ -12,6 +12,12 @@ mod no_leading_equal;
 #[path       =     "modules/valid_path.rs"]
 mod extra_spaces;
 
+#[path = ""]  // { dg-error "path attributes must contain a filename" }
+mod empty_path; // { dg-error "no candidate found" }
+
+#[path = "          "]  // { dg-error "path attributes must contain a filename" }
+mod path_with_spaces; // { dg-error "no candidate found" }
+
 #[path] // { dg-error "path attributes must contain a filename" }
 mod error; // { dg-error "no candidate found" }