[PATCH] D158296: [Clang] Add assertion to check the value of NumSubExprs/ResultIndex does not overflow

Yurong via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Aug 18 10:40:39 PDT 2023


yronglin created this revision.
Herald added a subscriber: arphaman.
Herald added a project: All.
yronglin requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Signed-off-by: yronglin <yronglin777 at gmail.com>


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D158296

Files:
  clang/include/clang/AST/Expr.h
  clang/lib/AST/Expr.cpp
  clang/lib/Serialization/ASTReaderStmt.cpp


Index: clang/lib/Serialization/ASTReaderStmt.cpp
===================================================================
--- clang/lib/Serialization/ASTReaderStmt.cpp
+++ clang/lib/Serialization/ASTReaderStmt.cpp
@@ -1378,9 +1378,8 @@
 void ASTStmtReader::VisitPseudoObjectExpr(PseudoObjectExpr *E) {
   VisitExpr(E);
   unsigned numSemanticExprs = Record.readInt();
-  assert(numSemanticExprs + 1 == E->PseudoObjectExprBits.NumSubExprs);
-  E->PseudoObjectExprBits.ResultIndex = Record.readInt();
-
+  assert(numSemanticExprs + 1 == E->getNumSubExprs());
+  E->setResultExprIndex(Record.readInt());
   // Read the syntactic expression.
   E->getSubExprsBuffer()[0] = Record.readSubExpr();
 
Index: clang/lib/AST/Expr.cpp
===================================================================
--- clang/lib/AST/Expr.cpp
+++ clang/lib/AST/Expr.cpp
@@ -4839,7 +4839,7 @@
 
 PseudoObjectExpr::PseudoObjectExpr(EmptyShell shell, unsigned numSemanticExprs)
   : Expr(PseudoObjectExprClass, shell) {
-  PseudoObjectExprBits.NumSubExprs = numSemanticExprs + 1;
+  setNumSubExprs(numSemanticExprs + 1);
 }
 
 PseudoObjectExpr *PseudoObjectExpr::Create(const ASTContext &C, Expr *syntax,
@@ -4870,8 +4870,9 @@
                                    Expr *syntax, ArrayRef<Expr *> semantics,
                                    unsigned resultIndex)
     : Expr(PseudoObjectExprClass, type, VK, OK_Ordinary) {
-  PseudoObjectExprBits.NumSubExprs = semantics.size() + 1;
-  PseudoObjectExprBits.ResultIndex = resultIndex + 1;
+  
+  setNumSubExprs(semantics.size() + 1);
+  setResultExprIndex(resultIndex + 1);
 
   for (unsigned i = 0, e = semantics.size() + 1; i != e; ++i) {
     Expr *E = (i == 0 ? syntax : semantics[i-1]);
Index: clang/include/clang/AST/Expr.h
===================================================================
--- clang/include/clang/AST/Expr.h
+++ clang/include/clang/AST/Expr.h
@@ -6310,6 +6310,18 @@
     return PseudoObjectExprBits.NumSubExprs;
   }
 
+  void setNumSubExprs(unsigned Val) {
+    assert(Val <= std::numeric_limits<uint16_t>::max() &&
+           "The value of NumSubExprs too large.");
+    PseudoObjectExprBits.NumSubExprs = Val;
+  }
+
+  void setResultExprIndex(unsigned Val) {
+    assert(Val <= std::numeric_limits<uint16_t>::max() &&
+           "The value of NumSubExprs too large.");
+    PseudoObjectExprBits.ResultIndex = Val;
+  }
+
 public:
   /// NoResult - A value for the result index indicating that there is
   /// no semantic result.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D158296.551566.patch
Type: text/x-patch
Size: 2476 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230818/3d5ae0a8/attachment.bin>


More information about the cfe-commits mailing list