Skip to content

Commit

Permalink
Added machenism to avoid Python run freezes.
Browse files Browse the repository at this point in the history
  • Loading branch information
namsonx committed Mar 7, 2024
1 parent ce226c1 commit 03b5112
Showing 1 changed file with 37 additions and 8 deletions.
45 changes: 37 additions & 8 deletions JsonPreprocessor/CJsonPreprocessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,12 +469,15 @@ def __handleDotInNestedParam(sNestedParam : str) -> str:
sVar = __handleDotInNestedParam(var)
sInputStr = sInputStr.replace(var, sVar)
tmpPattern = pattern + rf'(\[\s*\d+\s*\]|\[\s*\'[^{re.escape(self.specialCharacters)}]+\'\s*\])*'
sNestedParam = sInputStr.replace("$$", "$")
while re.search(tmpPattern, sInputStr, re.UNICODE) and sInputStr.count("$$")>1:
sLoopCheck = sInputStr
referVars = re.findall(r'(' + tmpPattern + r')[^\[]', sInputStr, re.UNICODE)
for var in referVars:
sVar = __handleDotInNestedParam(var[0]) if "." in var[0] else var[0]
tmpValue = __getNestedValue(sVar)
while var[0] in sInputStr:
sLoopCheck1 = sInputStr
dReplacements = {"$" : "\$", "[" : "\[", "]" : "\]"}
varPattern = self.__multipleReplace(var[0], dReplacements)
if re.search(r"\[['\s]*" + varPattern + r"['\s]*\]", sInputStr):
Expand All @@ -492,6 +495,12 @@ def __handleDotInNestedParam(sNestedParam : str) -> str:
raise Exception(errorMsg)
else:
sInputStr = sInputStr.replace(var[0], str(tmpValue))
if sInputStr==sLoopCheck1:
self.__reset(bCleanGlobalVars=True)
raise Exception(f"Infinity loop detection while handling the parameter '{sNestedParam}'.")
if sInputStr==sLoopCheck:
self.__reset(bCleanGlobalVars=True)
raise Exception(f"Infinity loop detection while handling the parameter '{sNestedParam}'.")
if sInputStr.count("$$")==1:
tmpPattern = pattern + rf'(\[\s*\d+\s*\]|\[\s*\'[^{re.escape(self.specialCharacters)}]+\'\s*\])*'
if re.match("^" + tmpPattern + "$", sInputStr.strip(), re.UNICODE) and bKey and not bConvertToStr:
Expand Down Expand Up @@ -728,7 +737,11 @@ def __loadNestedValue(initValue: str, sInputStr: str, bKey=False, key=''):
oJson[keyNested] = v
bNested = True
while "${" in k:
sLoopCheck = k
k = __loadNestedValue(keyNested, k, bKey=True, key=keyNested)
if k == sLoopCheck:
self.__reset(bCleanGlobalVars=True)
raise Exception(f"Infinity loop detection while handling the parameter '{keyNested}'.")
elif re.match(r"^\s*" + pattern + r"\s*$", k, re.UNICODE):
keyNested = k
if re.search(r"\[\s*'*" + pattern + r"'*\s*\]", keyNested, re.UNICODE) or \
Expand Down Expand Up @@ -756,8 +769,11 @@ def __loadNestedValue(initValue: str, sInputStr: str, bKey=False, key=''):
bNested = True
initItem = item
while isinstance(item, str) and "${" in item:
sLoopCheck = item
item = __loadNestedValue(initItem, item)

if item==sLoopCheck:
self.__reset(bCleanGlobalVars=True)
raise Exception(f"Infinity loop detection while handling the parameter '{initItem}'.")
tmpValue.append(item)
v = tmpValue
del tmpValue
Expand All @@ -766,16 +782,25 @@ def __loadNestedValue(initValue: str, sInputStr: str, bKey=False, key=''):
v = v[1]
if "${" in str(v):
bNested = True
while isinstance(v, str) and "${" in v:
v = __loadNestedValue(initValue=v, sInputStr=v)
initValue = v
while isinstance(v, str) and "${" in v:
sLoopCheck = v
v = __loadNestedValue(initValue=v, sInputStr=v)
if v == sLoopCheck:
self.__reset(bCleanGlobalVars=True)
raise Exception(f"Infinity loop detection while handling the parameter '{initValue}'.")
else:
i=1
while i<len(v):
while re.search(pattern, v[i], re.UNICODE):
bNested = True
initItem = v[i]
tmpValue = __loadNestedValue(initItem, v[i])
v[i] = tmpValue
if isinstance(v[i], str) and "${" in v[i]:
while re.search(pattern, v[i], re.UNICODE):
bNested = True
initItem = v[i]
tmpValue = __loadNestedValue(initItem, v[i])
v[i] = tmpValue
if v[i]==initItem:
self.__reset(bCleanGlobalVars=True)
raise Exception(f"Infinity loop detection while handling the parameter '{initItem}'.")
tmpValue = v[i]
i+=1
v = tmpValue
Expand All @@ -786,7 +811,11 @@ def __loadNestedValue(initValue: str, sInputStr: str, bKey=False, key=''):
bNested = True
initValue = v
while isinstance(v, str) and "${" in v:
sLoopCheck = v
v = __loadNestedValue(initValue, v)
if v == sLoopCheck:
self.__reset(bCleanGlobalVars=True)
raise Exception(f"Infinity loop detection while handling the parameter '{initValue}'.")
if bDuplicatedHandle:
if "${" not in dupKey and parentParams != "":
sExec = parentParams + "['" + k + "'] = \"" + v + "\"" if isinstance(v, str) else \
Expand Down

0 comments on commit 03b5112

Please sign in to comment.