aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPhilip Herron <philip.herron@embecosm.com>2022-07-07 12:11:00 +0100
committerPhilip Herron <philip.herron@embecosm.com>2022-07-08 12:39:26 +0100
commit2128789a386d098eb2068514baf3c85782c6a74e (patch)
tree3c121631f705aeb9bac49205d1f537393a38c775
parent444bb5b818315d278914df10dc1c5c650bae6946 (diff)
downloadgcc-2128789a386d098eb2068514baf3c85782c6a74e.zip
gcc-2128789a386d098eb2068514baf3c85782c6a74e.tar.gz
gcc-2128789a386d098eb2068514baf3c85782c6a74e.tar.bz2
Fix HIR::LetStmt Dref nullptr
HIR::LetStmts can have optional specifiedtrs or patterns. This patch updates the copy constructors to respect this properly to avoid nullptr deref.
-rw-r--r--gcc/rust/hir/tree/rust-hir-stmt.h35
1 files changed, 28 insertions, 7 deletions
diff --git a/gcc/rust/hir/tree/rust-hir-stmt.h b/gcc/rust/hir/tree/rust-hir-stmt.h
index 54d3e21..5247b0a 100644
--- a/gcc/rust/hir/tree/rust-hir-stmt.h
+++ b/gcc/rust/hir/tree/rust-hir-stmt.h
@@ -91,20 +91,41 @@ public:
// Copy constructor with clone
LetStmt (LetStmt const &other)
: Stmt (other.mappings), outer_attrs (other.outer_attrs),
- variables_pattern (other.variables_pattern->clone_pattern ()),
- type (other.type->clone_type ()),
- init_expr (other.init_expr->clone_expr ()), locus (other.locus)
- {}
+ locus (other.locus)
+ {
+ // guard to prevent null dereference (only required if error state)
+ if (other.variables_pattern != nullptr)
+ variables_pattern = other.variables_pattern->clone_pattern ();
+
+ // guard to prevent null dereference (always required)
+ if (other.init_expr != nullptr)
+ init_expr = other.init_expr->clone_expr ();
+ if (other.type != nullptr)
+ type = other.type->clone_type ();
+ }
// Overloaded assignment operator to clone
LetStmt &operator= (LetStmt const &other)
{
- variables_pattern = other.variables_pattern->clone_pattern ();
- init_expr = other.init_expr->clone_expr ();
- type = other.type->clone_type ();
outer_attrs = other.outer_attrs;
locus = other.locus;
+ // guard to prevent null dereference (only required if error state)
+ if (other.variables_pattern != nullptr)
+ variables_pattern = other.variables_pattern->clone_pattern ();
+ else
+ variables_pattern = nullptr;
+
+ // guard to prevent null dereference (always required)
+ if (other.init_expr != nullptr)
+ init_expr = other.init_expr->clone_expr ();
+ else
+ init_expr = nullptr;
+ if (other.type != nullptr)
+ type = other.type->clone_type ();
+ else
+ type = nullptr;
+
return *this;
}