Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions interpreter/cling/include/cling/Interpreter/RuntimePrintValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,20 @@ namespace cling {
const void* M = TypeTest::isMap(obj);

std::string str("{ ");
str += printValue(&(*iter), M);

// If the dereferenced iterator points to the container itself, we have
// infinite recursion. This occurs with scalar values in nlohmann::json
auto printWithRecursionGuard = [obj](const auto* ptr,
const void* M) -> std::string {
if (static_cast<const void*>(ptr) == static_cast<const void*>(obj))
return "<recursion detected>";
return printValue(ptr, M);
};

str += printWithRecursionGuard(&(*iter), M);
while (++iter != iterEnd) {
str += ", ";
str += printValue(&(*iter), M);
str += printWithRecursionGuard(&(*iter), M);
}
return str + " }";
}
Expand Down
30 changes: 30 additions & 0 deletions interpreter/cling/test/Prompt/ValuePrinter/Recursion.C
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//------------------------------------------------------------------------------
// CLING - the C++ LLVM-based InterpreterG :)
//
// This file is dual-licensed: you can choose to license it under the University
// of Illinois Open Source License or the GNU Lesser General Public License. See
// LICENSE.TXT for details.

//------------------------------------------------------------------------------

// RUN: cat %s | %cling -Xclang -verify 2>&1 | FileCheck %s

.rawInput 1
// When begin() != end() but *begin() points to the container itself
// (in nlohmann::json), printValue_impl infinitely recurses without the
// self-reference check.

class RecursionTest {
public:
RecursionTest() = default;
auto begin() const { return this; } // iterate over self
auto end() const { return this + 1; } // just to make sure begin() != end()
};
.rawInput 0

RecursionTest j;
j
// CHECK: (RecursionTest &) { <recursion detected> }

// expected-no-diagnostics
.q
Loading