Skip to content
This repository was archived by the owner on Mar 29, 2024. It is now read-only.

Commit b0a90fb

Browse files
committed
Refactor smart_dereference() (#21)
Reducing Cognitive Complexity in Code Climate
1 parent fea17bb commit b0a90fb

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

TWindbg/context.py

+16-8
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
ARCH = None
1111
PTRMASK = None
1212
PTRSIZE = None
13-
MAX_DEREF = 20
13+
MAX_DEREF_DEPTH = 20
1414

1515
def init_arch():
1616
global ARCH, PTRMASK, PTRSIZE
@@ -213,20 +213,28 @@ def enhance_type(self, ptr, val):
213213

214214
def smart_dereference(self, ptr):
215215
ptr_values, is_cyclic = [ptr], False
216-
for _ in range(MAX_DEREF):
217-
val = deref_ptr(ptr)
218-
if val == None: # no more dereference
219-
break
216+
deref_depth = 0
217+
val = deref_ptr(ptr)
220218

219+
while self.can_chain_pointer(val, deref_depth, is_cyclic):
220+
deref_depth += 1
221221
ptr_values.append(val)
222222
# Check if the newly pushed value is in ptr_values[:-1:]
223223
# If it does means there's a cyclic dereference in the current pointer chain
224224
# Otherwise the newly pushed value will become the new pointer to be dereferenced next
225225
if val in ptr_values[:-1:]:
226226
is_cyclic = True
227-
break
228227
else:
229-
ptr = val
228+
val = deref_ptr(val)
230229

231230
return ptr_values, is_cyclic
232-
231+
232+
def can_chain_pointer(self, val, deref_depth, is_cyclic):
233+
"""
234+
Check if we can keep chaining the pointer list, which should match the following conditions:
235+
- val ( The newly dereferenced value ) should not be None
236+
- deref_depth ( deference depth ) should not exceed MAX_DEREF_DEPTH
237+
- No cyclic dereference is detected
238+
"""
239+
return ( val != None ) and ( deref_depth <= MAX_DEREF_DEPTH ) and not is_cyclic
240+

0 commit comments

Comments
 (0)