]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: Remove undefined behavior with static_cast
authorOwen Avery <powerboat9.gamer@gmail.com>
Tue, 15 Jul 2025 16:04:53 +0000 (12:04 -0400)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 5 Aug 2025 14:37:03 +0000 (16:37 +0200)
gcc/rust/ChangeLog:

* expand/rust-macro-builtins-helpers.cc
(try_extract_string_literal_from_fragment): Perform static_cast
to AST::LiteralExpr only after it's verified that an AST::Expr
is a literal.

Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
gcc/rust/expand/rust-macro-builtins-helpers.cc

index 19af5df4a68cb4fa8e126603f8a1b4077509171c..ee01f65d8a1d8934dff3d70fda2f39928ae67e87 100644 (file)
@@ -111,9 +111,9 @@ std::unique_ptr<AST::LiteralExpr>
 try_extract_string_literal_from_fragment (const location_t &parent_locus,
                                          std::unique_ptr<AST::Expr> &node)
 {
-  auto maybe_lit = static_cast<AST::LiteralExpr *> (node.get ());
   if (!node || !node->is_literal ()
-      || maybe_lit->get_lit_type () != AST::Literal::STRING)
+      || static_cast<AST::LiteralExpr &> (*node).get_lit_type ()
+          != AST::Literal::STRING)
     {
       rust_error_at (parent_locus, "argument must be a string literal");
       if (node)