[edk2] [PATCH] BaseTools: Fix eval parse string issue

Feng, YunhuaX posted 1 patch 6 years, 9 months ago
Failed in applying to current master (apply log)
BaseTools/Source/Python/Common/Misc.py | 30 ++++++++++++++++++++++++++----
1 file changed, 26 insertions(+), 4 deletions(-)
[edk2] [PATCH] BaseTools: Fix eval parse string issue
Posted by Feng, YunhuaX 6 years, 9 months ago
eval argument start with " or ', but it is unicode string,
will encounter error:
    List = list(eval(Value)) # translate escape character
  File "<string>", line 1
    'jà=îµ¢Ã"Fà
             ^
SyntaxError: EOL while scanning string literal

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>
---
 BaseTools/Source/Python/Common/Misc.py | 30 ++++++++++++++++++++++++++----
 1 file changed, 26 insertions(+), 4 deletions(-)

diff --git a/BaseTools/Source/Python/Common/Misc.py b/BaseTools/Source/Python/Common/Misc.py
index a7e7797d04..af374d804d 100644
--- a/BaseTools/Source/Python/Common/Misc.py
+++ b/BaseTools/Source/Python/Common/Misc.py
@@ -1552,37 +1552,59 @@ def ParseFieldValue (Value):
             raise BadExpression('%s' % Message)
         Value, Size = ParseFieldValue(Value)
         return Value, 16
     if Value.startswith('L"') and Value.endswith('"'):
         # Unicode String
-        List = list(eval(Value[1:]))  # translate escape character
+        # translate escape character
+        Value = Value[1:]
+        try:
+            Value = eval(Value)
+        except:
+            Value = Value[1:-1]
+        List = list(Value)
         List.reverse()
         Value = 0
         for Char in List:
             Value = (Value << 16) | ord(Char)
         return Value, (len(List) + 1) * 2
     if Value.startswith('"') and Value.endswith('"'):
         # ASCII String
-        List = list(eval(Value))  # translate escape character
+        # translate escape character
+        try:
+            Value = eval(Value)
+        except:
+            Value = Value[1:-1]
+        List = list(Value)
         List.reverse()
         Value = 0
         for Char in List:
             Value = (Value << 8) | ord(Char)
         return Value, len(List) + 1
     if Value.startswith("L'") and Value.endswith("'"):
         # Unicode Character Constant
-        List = list(eval(Value[1:])) # translate escape character
+        # translate escape character
+        Value = Value[1:]
+        try:
+            Value = eval(Value)
+        except:
+            Value = Value[1:-1]
+        List = list(Value)
         if len(List) == 0:
             raise BadExpression('Length %s is %s' % (Value, len(List)))
         List.reverse()
         Value = 0
         for Char in List:
             Value = (Value << 16) | ord(Char)
         return Value, len(List) * 2
     if Value.startswith("'") and Value.endswith("'"):
         # Character constant
-        List = list(eval(Value)) # translate escape character
+        # translate escape character
+        try:
+            Value = eval(Value)
+        except:
+            Value = Value[1:-1]
+        List = list(Value)
         if len(List) == 0:
             raise BadExpression('Length %s is %s' % (Value, len(List)))
         List.reverse()
         Value = 0
         for Char in List:
-- 
2.12.2.windows.2

_______________________________________________
edk2-devel mailing list
edk2-devel@lists.01.org
https://lists.01.org/mailman/listinfo/edk2-devel
Re: [edk2] [PATCH] BaseTools: Fix eval parse string issue
Posted by Zhu, Yonghong 6 years, 9 months ago
Reviewed-by: Yonghong Zhu <yonghong.zhu@intel.com> 

Best Regards,
Zhu Yonghong


-----Original Message-----
From: Feng, YunhuaX 
Sent: Thursday, March 01, 2018 10:16 AM
To: edk2-devel@lists.01.org
Cc: Zhu, Yonghong <yonghong.zhu@intel.com>; Gao, Liming <liming.gao@intel.com>
Subject: [PATCH] BaseTools: Fix eval parse string issue

eval argument start with " or ', but it is unicode string, will encounter error:
    List = list(eval(Value)) # translate escape character
  File "<string>", line 1
    'jà=îµ¢Ã"Fà
             ^
SyntaxError: EOL while scanning string literal

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>
---
 BaseTools/Source/Python/Common/Misc.py | 30 ++++++++++++++++++++++++++----
 1 file changed, 26 insertions(+), 4 deletions(-)

diff --git a/BaseTools/Source/Python/Common/Misc.py b/BaseTools/Source/Python/Common/Misc.py
index a7e7797d04..af374d804d 100644
--- a/BaseTools/Source/Python/Common/Misc.py
+++ b/BaseTools/Source/Python/Common/Misc.py
@@ -1552,37 +1552,59 @@ def ParseFieldValue (Value):
             raise BadExpression('%s' % Message)
         Value, Size = ParseFieldValue(Value)
         return Value, 16
     if Value.startswith('L"') and Value.endswith('"'):
         # Unicode String
-        List = list(eval(Value[1:]))  # translate escape character
+        # translate escape character
+        Value = Value[1:]
+        try:
+            Value = eval(Value)
+        except:
+            Value = Value[1:-1]
+        List = list(Value)
         List.reverse()
         Value = 0
         for Char in List:
             Value = (Value << 16) | ord(Char)
         return Value, (len(List) + 1) * 2
     if Value.startswith('"') and Value.endswith('"'):
         # ASCII String
-        List = list(eval(Value))  # translate escape character
+        # translate escape character
+        try:
+            Value = eval(Value)
+        except:
+            Value = Value[1:-1]
+        List = list(Value)
         List.reverse()
         Value = 0
         for Char in List:
             Value = (Value << 8) | ord(Char)
         return Value, len(List) + 1
     if Value.startswith("L'") and Value.endswith("'"):
         # Unicode Character Constant
-        List = list(eval(Value[1:])) # translate escape character
+        # translate escape character
+        Value = Value[1:]
+        try:
+            Value = eval(Value)
+        except:
+            Value = Value[1:-1]
+        List = list(Value)
         if len(List) == 0:
             raise BadExpression('Length %s is %s' % (Value, len(List)))
         List.reverse()
         Value = 0
         for Char in List:
             Value = (Value << 16) | ord(Char)
         return Value, len(List) * 2
     if Value.startswith("'") and Value.endswith("'"):
         # Character constant
-        List = list(eval(Value)) # translate escape character
+        # translate escape character
+        try:
+            Value = eval(Value)
+        except:
+            Value = Value[1:-1]
+        List = list(Value)
         if len(List) == 0:
             raise BadExpression('Length %s is %s' % (Value, len(List)))
         List.reverse()
         Value = 0
         for Char in List:
--
2.12.2.windows.2

_______________________________________________
edk2-devel mailing list
edk2-devel@lists.01.org
https://lists.01.org/mailman/listinfo/edk2-devel