|
32 | 32 | #include <cstdint> |
33 | 33 | #include <cstring> |
34 | 34 | #include <iterator> // back_inserter |
| 35 | +#include <set> |
35 | 36 | #include <sstream> |
| 37 | +#include <tuple> |
36 | 38 | #include <utility> |
37 | 39 |
|
38 | 40 | #include "xml.h" |
@@ -507,11 +509,19 @@ static bool findPath(const std::string &callId, |
507 | 509 | const CTU::FileInfo::CallBase *path[10], |
508 | 510 | int index, |
509 | 511 | bool warning, |
510 | | - int maxCtuDepth) |
| 512 | + int maxCtuDepth, |
| 513 | + std::set<std::tuple<std::string, size_t, int>> &visited) |
511 | 514 | { |
512 | 515 | if (index >= maxCtuDepth) |
513 | 516 | return false; // TODO: add bailout message? |
514 | 517 |
|
| 518 | + // Within one search the result only depends on callId, callArgNr and index. |
| 519 | + // A repeated state has failed before (a success ends the whole search). |
| 520 | + // Without this the search is exponential when the same calls are reachable |
| 521 | + // on many paths, for example calls in a header that many files include. |
| 522 | + if (!visited.emplace(callId, callArgNr, index).second) |
| 523 | + return false; |
| 524 | + |
515 | 525 | const auto it = callsMap.find(callId); |
516 | 526 | if (it == callsMap.end()) |
517 | 527 | return false; |
@@ -550,7 +560,7 @@ static bool findPath(const std::string &callId, |
550 | 560 | if (!nestedCall) |
551 | 561 | continue; |
552 | 562 |
|
553 | | - if (findPath(nestedCall->myId, nestedCall->myArgNr, unsafeValue, invalidValue, callsMap, path, index + 1, warning, maxCtuDepth)) { |
| 563 | + if (findPath(nestedCall->myId, nestedCall->myArgNr, unsafeValue, invalidValue, callsMap, path, index + 1, warning, maxCtuDepth, visited)) { |
554 | 564 | path[index] = nestedCall; |
555 | 565 | return true; |
556 | 566 | } |
@@ -584,7 +594,8 @@ std::list<ErrorMessage::FileLocation> CTU::FileInfo::getErrorPath(InvalidValueTy |
584 | 594 | { |
585 | 595 | const CTU::FileInfo::CallBase *path[10] = {nullptr}; |
586 | 596 |
|
587 | | - if (!findPath(unsafeUsage.myId, unsafeUsage.myArgNr, unsafeUsage.value, invalidValue, callsMap, path, 0, warning, maxCtuDepth)) |
| 597 | + std::set<std::tuple<std::string, size_t, int>> visited; |
| 598 | + if (!findPath(unsafeUsage.myId, unsafeUsage.myArgNr, unsafeUsage.value, invalidValue, callsMap, path, 0, warning, maxCtuDepth, visited)) |
588 | 599 | return {}; |
589 | 600 |
|
590 | 601 | if (unknownFunctionReturn && path[0]) { |
|
0 commit comments