11#include < cstring>
22#include < cstdint>
33#include < stdexcept>
4+ #include < unordered_set>
45#include " ASTree.h"
56#include " FastStack.h"
67#include " pyc_numeric.h"
@@ -2779,6 +2780,8 @@ void print_formatted_value(PycRef<ASTFormattedValue> formatted_value, PycModule*
27792780 pyc_output << " }" ;
27802781}
27812782
2783+ static std::unordered_set<ASTNode *> node_seen;
2784+
27822785void print_src (PycRef<ASTNode> node, PycModule* mod, std::ostream& pyc_output)
27832786{
27842787 if (node == NULL ) {
@@ -2787,6 +2790,12 @@ void print_src(PycRef<ASTNode> node, PycModule* mod, std::ostream& pyc_output)
27872790 return ;
27882791 }
27892792
2793+ if (node_seen.find ((ASTNode *)node) != node_seen.end ()) {
2794+ fputs (" WARNING: Circular reference detected\n " , stderr);
2795+ return ;
2796+ }
2797+ node_seen.insert ((ASTNode *)node);
2798+
27902799 switch (node->type ()) {
27912800 case ASTNode::NODE_BINARY:
27922801 case ASTNode::NODE_COMPARE:
@@ -3442,10 +3451,12 @@ void print_src(PycRef<ASTNode> node, PycModule* mod, std::ostream& pyc_output)
34423451 pyc_output << " <NODE:" << node->type () << " >" ;
34433452 fprintf (stderr, " Unsupported Node type: %d\n " , node->type ());
34443453 cleanBuild = false ;
3454+ node_seen.erase ((ASTNode *)node);
34453455 return ;
34463456 }
34473457
34483458 cleanBuild = true ;
3459+ node_seen.erase ((ASTNode *)node);
34493460}
34503461
34513462bool print_docstring (PycRef<PycObject> obj, int indent, PycModule* mod,
@@ -3462,8 +3473,16 @@ bool print_docstring(PycRef<PycObject> obj, int indent, PycModule* mod,
34623473 return false ;
34633474}
34643475
3476+ static std::unordered_set<PycCode *> code_seen;
3477+
34653478void decompyle (PycRef<PycCode> code, PycModule* mod, std::ostream& pyc_output)
34663479{
3480+ if (code_seen.find ((PycCode *)code) != code_seen.end ()) {
3481+ fputs (" WARNING: Circular reference detected\n " , stderr);
3482+ return ;
3483+ }
3484+ code_seen.insert ((PycCode *)code);
3485+
34673486 PycRef<ASTNode> source = BuildFromCode (code, mod);
34683487
34693488 PycRef<ASTNodeList> clean = source.cast <ASTNodeList>();
@@ -3557,4 +3576,6 @@ void decompyle(PycRef<PycCode> code, PycModule* mod, std::ostream& pyc_output)
35573576 start_line (cur_indent, pyc_output);
35583577 pyc_output << " # WARNING: Decompyle incomplete\n " ;
35593578 }
3579+
3580+ code_seen.erase ((PycCode *)code);
35603581}
0 commit comments