BaseTools: report error if flag in LABEL() invalid
authorFeng, YunhuaX </o=Intel/ou=Exchange Administrative Group (FYDIBOHF23SPDLT)/cn=Recipients/cn=Feng, YunhuaX4e1>
Thu, 1 Mar 2018 08:22:08 +0000 (16:22 +0800)
committerYonghong Zhu <yonghong.zhu@intel.com>
Fri, 2 Mar 2018 02:18:50 +0000 (10:18 +0800)
Flag in LABEL() is not valid C variable name, will report error.

Cc: Liming Gao <liming.gao@intel.com>
Cc: Yonghong Zhu <yonghong.zhu@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Yunhua Feng <yunhuax.feng@intel.com>
Reviewed-by: Yonghong Zhu <yonghong.zhu@intel.com>
BaseTools/Source/Python/Common/Expression.py

index f1516d5c7bb8c9bd39ffcc5dd1eabad2a2ff6d1d..5a0ade9e7ecad6d1fa6cbeb2dec09f7d4ef26eae 100644 (file)
@@ -119,6 +119,12 @@ def SplitPcdValueString(String):
         RetList.append(Item)\r
     return RetList\r
 \r
+def IsValidCString(Str):\r
+    ValidString = re.compile(r'[_a-zA-Z][_0-9a-zA-Z]*$')\r
+    if not ValidString.match(Str):\r
+        return False\r
+    return True\r
+\r
 ## ReplaceExprMacro\r
 #\r
 def ReplaceExprMacro(String, Macros, ExceptionList = None):\r
@@ -885,13 +891,13 @@ class ValueExpressionEx(ValueExpression):
                         for Index, Item in enumerate(PcdValueList):\r
                             # compute byte offset of every LABEL\r
                             Item = Item.strip()\r
-                            try:\r
-                                LabelList = ReLabel.findall(Item)\r
+                            LabelList = ReLabel.findall(Item)\r
+                            if LabelList:\r
                                 for Label in LabelList:\r
+                                    if not IsValidCString(Label):\r
+                                        raise BadExpression('%s is not a valid c variable name' % Label)\r
                                     if Label not in LabelDict.keys():\r
                                         LabelDict[Label] = str(LabelOffset)\r
-                            except:\r
-                                pass\r
                             if Item.startswith('UINT8'):\r
                                 LabelOffset = LabelOffset + 1\r
                             elif Item.startswith('UINT16'):\r