[llvm] 17cd344 - Fix two bugs in TGParser::ParseValue

Nicolai Hähnle via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 21 14:20:10 PDT 2020


Author: Nicolai Hähnle
Date: 2020-08-21T23:19:36+02:00
New Revision: 17cd34409a3ab1c46ff55960b7b89c11e1d5674d

URL: https://github.com/llvm/llvm-project/commit/17cd34409a3ab1c46ff55960b7b89c11e1d5674d
DIFF: https://github.com/llvm/llvm-project/commit/17cd34409a3ab1c46ff55960b7b89c11e1d5674d.diff

LOG: Fix two bugs in TGParser::ParseValue

TGParser::ParseValue contains two recursive calls, one to parse the RHS of a list paste operator and one to parse the RHS of a paste operator in a class/def name. Both of these calls neglect to check the return value to see if it is null (because of some error). This causes a crash in the next line of code, which uses the return value. The code now checks for null returns.

Differential Revision: https://reviews.llvm.org/D85852

Added: 
    llvm/test/TableGen/paste-reserved.td

Modified: 
    llvm/lib/TableGen/TGParser.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/TableGen/TGParser.cpp b/llvm/lib/TableGen/TGParser.cpp
index 47f471ae2c4b..39c29d6108d5 100644
--- a/llvm/lib/TableGen/TGParser.cpp
+++ b/llvm/lib/TableGen/TGParser.cpp
@@ -2203,6 +2203,8 @@ Init *TGParser::ParseValue(Record *CurRec, RecTy *ItemType, IDParseMode Mode) {
           break;
         default:
           Init *RHSResult = ParseValue(CurRec, ItemType, ParseNameMode);
+          if (!RHSResult)
+            return nullptr;
           Result = BinOpInit::getListConcat(LHS, RHSResult);
         }
         break;
@@ -2239,6 +2241,8 @@ Init *TGParser::ParseValue(Record *CurRec, RecTy *ItemType, IDParseMode Mode) {
 
       default:
         Init *RHSResult = ParseValue(CurRec, nullptr, ParseNameMode);
+        if (!RHSResult)
+          return nullptr;
         RHS = dyn_cast<TypedInit>(RHSResult);
         if (!RHS) {
           Error(PasteLoc, "RHS of paste is not typed!");

diff  --git a/llvm/test/TableGen/paste-reserved.td b/llvm/test/TableGen/paste-reserved.td
new file mode 100644
index 000000000000..8e61e5526d20
--- /dev/null
+++ b/llvm/test/TableGen/paste-reserved.td
@@ -0,0 +1,22 @@
+// RUN: not llvm-tblgen -DERROR1 %s 2>&1 | FileCheck --check-prefix=ERROR1 %s
+// RUN: not llvm-tblgen -DERROR2 %s 2>&1 | FileCheck --check-prefix=ERROR2 %s
+
+defvar list1 = ["foo", "bar", "snork"];
+
+// Pasting a list with a reserved word should produce an error.
+
+#ifdef ERROR1
+def list_paste {
+  list<string> the_list = list1 # in;
+}
+// ERROR1: error: Unknown token when parsing a value
+#endif
+
+
+// Pasting an identifier with a reserved word should produce an error.
+
+#ifdef ERROR2
+def name_paste#in {
+}
+// ERROR2: error: Unknown token when parsing a value
+#endif


        


More information about the llvm-commits mailing list