https://gcc.gnu.org/g:0bfd81883ebacdfa6b7d68ec5b834c0c58d01f65

commit r16-2834-g0bfd81883ebacdfa6b7d68ec5b834c0c58d01f65
Author: Vishruth-Thimmaiah <vishruththimma...@gmail.com>
Date:   Tue Apr 22 20:04:22 2025 +0530

    gccrs: parser: fix ICE std::out_of_range with path attrs to nonexisting path
    
    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 <vishruththimma...@gmail.com>

Diff:
---
 gcc/rust/parse/rust-parse.cc                      | 15 +++++++++++----
 gcc/testsuite/rust/compile/torture/extern_mod2.rs |  6 ++++++
 2 files changed, 17 insertions(+), 4 deletions(-)

diff --git a/gcc/rust/parse/rust-parse.cc b/gcc/rust/parse/rust-parse.cc
index 4895c6afdaa0..860fd11612ad 100644
--- a/gcc/rust/parse/rust-parse.cc
+++ b/gcc/rust/parse/rust-parse.cc
@@ -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
diff --git a/gcc/testsuite/rust/compile/torture/extern_mod2.rs 
b/gcc/testsuite/rust/compile/torture/extern_mod2.rs
index 4984d5dc2c14..f3a4f799c3ee 100644
--- a/gcc/testsuite/rust/compile/torture/extern_mod2.rs
+++ b/gcc/testsuite/rust/compile/torture/extern_mod2.rs
@@ -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" }

Reply via email to