public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
* [Patch] BaseTools: Add special handle for '\' use in Pcd Value
@ 2018-10-30 13:27 Yonghong Zhu
  2018-10-30 14:21 ` Carsey, Jaben
  0 siblings, 1 reply; 2+ messages in thread
From: Yonghong Zhu @ 2018-10-30 13:27 UTC (permalink / raw)
  To: edk2-devel; +Cc: zhijufan, Liming Gao

From: zhijufan <zhijux.fan@intel.com>

Case:
gEfiOzmosisPkgTokenSpaceGuid.PcdBootLogFolderPath|L"\\Logs\\"|VOID*|12

Fixes: https://bugzilla.tianocore.org/show_bug.cgi?id=1287
Cc: Liming Gao <liming.gao@intel.com>
Cc: Yonghong Zhu <yonghong.zhu@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Zhiju.Fan <zhijux.fan@intel.com>
---
 BaseTools/Source/Python/Common/Expression.py | 13 ++++++++++++-
 BaseTools/Source/Python/Common/Misc.py       |  9 ++++++---
 2 files changed, 18 insertions(+), 4 deletions(-)

diff --git a/BaseTools/Source/Python/Common/Expression.py b/BaseTools/Source/Python/Common/Expression.py
index 05459b9..6eec0de 100644
--- a/BaseTools/Source/Python/Common/Expression.py
+++ b/BaseTools/Source/Python/Common/Expression.py
@@ -20,10 +20,11 @@ from CommonDataClass.Exceptions import WrnExpression
 from .Misc import GuidStringToGuidStructureString, ParseFieldValue
 import Common.EdkLogger as EdkLogger
 import copy
 from Common.DataType import *
 import sys
+import random, string
 
 ERR_STRING_EXPR         = 'This operator cannot be used in string expression: [%s].'
 ERR_SNYTAX              = 'Syntax error, the rest of expression cannot be evaluated: [%s].'
 ERR_MATCH               = 'No matching right parenthesis.'
 ERR_STRING_TOKEN        = 'Bad string token: [%s].'
@@ -53,10 +54,12 @@ PcdPattern = re.compile(r'[_a-zA-Z][0-9A-Za-z_]*\.[_a-zA-Z][0-9A-Za-z_]*$')
 #  Split string to list according double quote
 #  For example: abc"de\"f"ghi"jkl"mn will be: ['abc', '"de\"f"', 'ghi', '"jkl"', 'mn']
 #
 def SplitString(String):
     # There might be escaped quote: "abc\"def\\\"ghi", 'abc\'def\\\'ghi'
+    RanStr = ''.join(random.sample(string.ascii_letters + string.digits, 8))
+    String = String.replace('\\\\', RanStr).strip()
     RetList = []
     InSingleQuote = False
     InDoubleQuote = False
     Item = ''
     for i, ch in enumerate(String):
@@ -85,15 +88,20 @@ def SplitString(String):
         Item += String[i]
     if InSingleQuote or InDoubleQuote:
         raise BadExpression(ERR_STRING_TOKEN % Item)
     if Item:
         RetList.append(Item)
+    for i, ch in enumerate(RetList):
+        if RanStr in ch:
+            RetList[i] = ch.replace(RanStr,'\\\\')
     return RetList
 
 def SplitPcdValueString(String):
     # There might be escaped comma in GUID() or DEVICE_PATH() or " "
     # or ' ' or L' ' or L" "
+    RanStr = ''.join(random.sample(string.ascii_letters + string.digits, 8))
+    String = String.replace('\\\\', RanStr).strip()
     RetList = []
     InParenthesis = 0
     InSingleQuote = False
     InDoubleQuote = False
     Item = ''
@@ -122,10 +130,13 @@ def SplitPcdValueString(String):
         Item += String[i]
     if InSingleQuote or InDoubleQuote or InParenthesis:
         raise BadExpression(ERR_STRING_TOKEN % Item)
     if Item:
         RetList.append(Item)
+    for i, ch in enumerate(RetList):
+        if RanStr in ch:
+            RetList[i] = ch.replace(RanStr,'\\\\')
     return RetList
 
 def IsValidCName(Str):
     return True if __ValidString.match(Str) else False
 
@@ -388,11 +399,11 @@ class ValueExpression(BaseExpression):
             if Val == 'L""':
                 Val = False
             elif not Val:
                 Val = False
                 RealVal = '""'
-            elif not Val.startswith('L"') and not Val.startswith('{') and not Val.startswith("L'"):
+            elif not Val.startswith('L"') and not Val.startswith('{') and not Val.startswith("L'") and not Val.startswith("'"):
                 Val = True
                 RealVal = '"' + RealVal + '"'
 
         # The expression has been parsed, but the end of expression is not reached
         # It means the rest does not comply EBNF of <Expression>
diff --git a/BaseTools/Source/Python/Common/Misc.py b/BaseTools/Source/Python/Common/Misc.py
index b32b7cd..b27268a 100644
--- a/BaseTools/Source/Python/Common/Misc.py
+++ b/BaseTools/Source/Python/Common/Misc.py
@@ -15,11 +15,11 @@
 # Import Modules
 #
 from __future__ import absolute_import
 import Common.LongFilePathOs as os
 import sys
-import string
+import random, string
 import threading
 import time
 import re
 import pickle
 import array
@@ -1234,11 +1234,12 @@ def IsFieldValueAnArray (Value):
     if Value[0] == "'" and Value[-1] == "'" and len(list(Value[1:-1])) > 1:
         return True
     return False
 
 def AnalyzePcdExpression(Setting):
-    Setting = Setting.strip()
+    RanStr = ''.join(random.sample(string.ascii_letters + string.digits, 8))
+    Setting = Setting.replace('\\\\', RanStr).strip()
     # There might be escaped quote in a string: \", \\\" , \', \\\'
     Data = Setting
     # There might be '|' in string and in ( ... | ... ), replace it with '-'
     NewStr = ''
     InSingleQuoteStr = False
@@ -1267,11 +1268,13 @@ def AnalyzePcdExpression(Setting):
         if Pos < 0:
             FieldList.append(Setting[StartPos:].strip())
             break
         FieldList.append(Setting[StartPos:Pos].strip())
         StartPos = Pos + 1
-
+    for i, ch in enumerate(FieldList):
+        if RanStr in ch:
+            FieldList[i] = ch.replace(RanStr,'\\\\')
     return FieldList
 
 def ParseDevPathValue (Value):
     if '\\' in Value:
         Value.replace('\\', '/').replace(' ', '')
-- 
2.6.1.windows.1



^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [Patch] BaseTools: Add special handle for '\' use in Pcd Value
  2018-10-30 13:27 [Patch] BaseTools: Add special handle for '\' use in Pcd Value Yonghong Zhu
@ 2018-10-30 14:21 ` Carsey, Jaben
  0 siblings, 0 replies; 2+ messages in thread
From: Carsey, Jaben @ 2018-10-30 14:21 UTC (permalink / raw)
  To: Zhu, Yonghong, edk2-devel@lists.01.org; +Cc: Gao, Liming

Please separate includes to each on their own line. PEP8 says no to multiples on one line.
Not: 
include random, string

But instead:
include string
include random

Also, if we only need sample from random, can we just use "from random import sample"?

-Jaben

> -----Original Message-----
> From: edk2-devel [mailto:edk2-devel-bounces@lists.01.org] On Behalf Of
> Yonghong Zhu
> Sent: Tuesday, October 30, 2018 6:28 AM
> To: edk2-devel@lists.01.org
> Cc: Gao, Liming <liming.gao@intel.com>
> Subject: [edk2] [Patch] BaseTools: Add special handle for '\' use in Pcd Value
> 
> From: zhijufan <zhijux.fan@intel.com>
> 
> Case:
> gEfiOzmosisPkgTokenSpaceGuid.PcdBootLogFolderPath|L"\\Logs\\"|VOID*
> |12
> 
> Fixes: https://bugzilla.tianocore.org/show_bug.cgi?id=1287
> Cc: Liming Gao <liming.gao@intel.com>
> Cc: Yonghong Zhu <yonghong.zhu@intel.com>
> Contributed-under: TianoCore Contribution Agreement 1.1
> Signed-off-by: Zhiju.Fan <zhijux.fan@intel.com>
> ---
>  BaseTools/Source/Python/Common/Expression.py | 13 ++++++++++++-
>  BaseTools/Source/Python/Common/Misc.py       |  9 ++++++---
>  2 files changed, 18 insertions(+), 4 deletions(-)
> 
> diff --git a/BaseTools/Source/Python/Common/Expression.py
> b/BaseTools/Source/Python/Common/Expression.py
> index 05459b9..6eec0de 100644
> --- a/BaseTools/Source/Python/Common/Expression.py
> +++ b/BaseTools/Source/Python/Common/Expression.py
> @@ -20,10 +20,11 @@ from CommonDataClass.Exceptions import
> WrnExpression
>  from .Misc import GuidStringToGuidStructureString, ParseFieldValue
>  import Common.EdkLogger as EdkLogger
>  import copy
>  from Common.DataType import *
>  import sys
> +import random, string
> 
>  ERR_STRING_EXPR         = 'This operator cannot be used in string expression:
> [%s].'
>  ERR_SNYTAX              = 'Syntax error, the rest of expression cannot be
> evaluated: [%s].'
>  ERR_MATCH               = 'No matching right parenthesis.'
>  ERR_STRING_TOKEN        = 'Bad string token: [%s].'
> @@ -53,10 +54,12 @@ PcdPattern = re.compile(r'[_a-zA-Z][0-9A-Za-
> z_]*\.[_a-zA-Z][0-9A-Za-z_]*$')
>  #  Split string to list according double quote
>  #  For example: abc"de\"f"ghi"jkl"mn will be: ['abc', '"de\"f"', 'ghi', '"jkl"',
> 'mn']
>  #
>  def SplitString(String):
>      # There might be escaped quote: "abc\"def\\\"ghi", 'abc\'def\\\'ghi'
> +    RanStr = ''.join(random.sample(string.ascii_letters + string.digits, 8))
> +    String = String.replace('\\\\', RanStr).strip()
>      RetList = []
>      InSingleQuote = False
>      InDoubleQuote = False
>      Item = ''
>      for i, ch in enumerate(String):
> @@ -85,15 +88,20 @@ def SplitString(String):
>          Item += String[i]
>      if InSingleQuote or InDoubleQuote:
>          raise BadExpression(ERR_STRING_TOKEN % Item)
>      if Item:
>          RetList.append(Item)
> +    for i, ch in enumerate(RetList):
> +        if RanStr in ch:
> +            RetList[i] = ch.replace(RanStr,'\\\\')
>      return RetList
> 
>  def SplitPcdValueString(String):
>      # There might be escaped comma in GUID() or DEVICE_PATH() or " "
>      # or ' ' or L' ' or L" "
> +    RanStr = ''.join(random.sample(string.ascii_letters + string.digits, 8))
> +    String = String.replace('\\\\', RanStr).strip()
>      RetList = []
>      InParenthesis = 0
>      InSingleQuote = False
>      InDoubleQuote = False
>      Item = ''
> @@ -122,10 +130,13 @@ def SplitPcdValueString(String):
>          Item += String[i]
>      if InSingleQuote or InDoubleQuote or InParenthesis:
>          raise BadExpression(ERR_STRING_TOKEN % Item)
>      if Item:
>          RetList.append(Item)
> +    for i, ch in enumerate(RetList):
> +        if RanStr in ch:
> +            RetList[i] = ch.replace(RanStr,'\\\\')
>      return RetList
> 
>  def IsValidCName(Str):
>      return True if __ValidString.match(Str) else False
> 
> @@ -388,11 +399,11 @@ class ValueExpression(BaseExpression):
>              if Val == 'L""':
>                  Val = False
>              elif not Val:
>                  Val = False
>                  RealVal = '""'
> -            elif not Val.startswith('L"') and not Val.startswith('{') and not
> Val.startswith("L'"):
> +            elif not Val.startswith('L"') and not Val.startswith('{') and not
> Val.startswith("L'") and not Val.startswith("'"):
>                  Val = True
>                  RealVal = '"' + RealVal + '"'
> 
>          # The expression has been parsed, but the end of expression is not
> reached
>          # It means the rest does not comply EBNF of <Expression>
> diff --git a/BaseTools/Source/Python/Common/Misc.py
> b/BaseTools/Source/Python/Common/Misc.py
> index b32b7cd..b27268a 100644
> --- a/BaseTools/Source/Python/Common/Misc.py
> +++ b/BaseTools/Source/Python/Common/Misc.py
> @@ -15,11 +15,11 @@
>  # Import Modules
>  #
>  from __future__ import absolute_import
>  import Common.LongFilePathOs as os
>  import sys
> -import string
> +import random, string
>  import threading
>  import time
>  import re
>  import pickle
>  import array
> @@ -1234,11 +1234,12 @@ def IsFieldValueAnArray (Value):
>      if Value[0] == "'" and Value[-1] == "'" and len(list(Value[1:-1])) > 1:
>          return True
>      return False
> 
>  def AnalyzePcdExpression(Setting):
> -    Setting = Setting.strip()
> +    RanStr = ''.join(random.sample(string.ascii_letters + string.digits, 8))
> +    Setting = Setting.replace('\\\\', RanStr).strip()
>      # There might be escaped quote in a string: \", \\\" , \', \\\'
>      Data = Setting
>      # There might be '|' in string and in ( ... | ... ), replace it with '-'
>      NewStr = ''
>      InSingleQuoteStr = False
> @@ -1267,11 +1268,13 @@ def AnalyzePcdExpression(Setting):
>          if Pos < 0:
>              FieldList.append(Setting[StartPos:].strip())
>              break
>          FieldList.append(Setting[StartPos:Pos].strip())
>          StartPos = Pos + 1
> -
> +    for i, ch in enumerate(FieldList):
> +        if RanStr in ch:
> +            FieldList[i] = ch.replace(RanStr,'\\\\')
>      return FieldList
> 
>  def ParseDevPathValue (Value):
>      if '\\' in Value:
>          Value.replace('\\', '/').replace(' ', '')
> --
> 2.6.1.windows.1
> 
> _______________________________________________
> edk2-devel mailing list
> edk2-devel@lists.01.org
> https://lists.01.org/mailman/listinfo/edk2-devel


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2018-10-30 14:21 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-10-30 13:27 [Patch] BaseTools: Add special handle for '\' use in Pcd Value Yonghong Zhu
2018-10-30 14:21 ` Carsey, Jaben

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox