Skip to content

Commit b8f4026

Browse files
thomas-chauchefoin-tobdguidoclaude
committed
Restore cycle detection in ASTProperties visitor
This was erroneously added and later removed in PR #210. The visitor override tracks visited nodes by id to prevent infinite recursion when traversing cyclic AST structures created via MEMOIZE + GET opcodes. Co-Authored-By: Dan Guido <dan@trailofbits.com> Co-Authored-By: Claude Code <noreply@anthropic.com>
1 parent c28d2d4 commit b8f4026

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

fickling/fickle.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,19 @@ def __init__(self):
528528
self.calls: list[ast.Call] = []
529529
self.non_setstate_calls: list[ast.Call] = []
530530
self.likely_safe_imports: set[str] = set()
531+
self._visited: set[int] = set() # Track visited nodes by id to detect cycles
532+
533+
def visit(self, node: ast.AST) -> Any:
534+
"""Override visit to detect and skip cycles in the AST.
535+
536+
Pickle files can create cyclic AST structures via MEMOIZE + GET opcodes.
537+
Without cycle detection, visiting such structures causes infinite recursion.
538+
"""
539+
node_id = id(node)
540+
if node_id in self._visited:
541+
return None # Skip already-visited nodes
542+
self._visited.add(node_id)
543+
return super().visit(node)
531544

532545
def _process_import(self, node: ast.Import | ast.ImportFrom):
533546
self.imports.append(node)

0 commit comments

Comments
 (0)