fix the infinite loop in tf importer.
authorZihao Mu <zihaomu@outlook.com>
Mon, 31 Oct 2022 12:10:25 +0000 (20:10 +0800)
committerZihao Mu <zihaomu@outlook.com>
Mon, 31 Oct 2022 12:10:25 +0000 (20:10 +0800)
modules/dnn/src/tensorflow/tf_graph_simplifier.cpp

index 72f546e..25ef850 100644 (file)
@@ -829,12 +829,19 @@ void RemoveIdentityOps(tensorflow::GraphDef& net)
             IdentityOpsMap::iterator it = identity_ops.find(input_op_name);
 
             if (it != identity_ops.end()) {
+                std::set<String> loopCheckSet;
                 // In case of Identity after Identity
                 while (true)
                 {
                     IdentityOpsMap::iterator nextIt = identity_ops.find(it->second);
                     if (nextIt != identity_ops.end())
+                    {
+                        // Loop check
+                        if (loopCheckSet.find(it->second) != loopCheckSet.end())
+                            CV_Error(Error::StsError, "Found a loop in your input Tensorflow model, which is illegal!");
+                        loopCheckSet.insert(it->second);
                         it = nextIt;
+                    }
                     else
                         break;
                 }