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
Comment on lines +214 to +215
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am confused. In the example, the 'points to the container itself' happens only if the container is empty and thus this line should not be reached (see line 208).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is probably related to the odd implementation mentioned at https://github.com/root-project/root/pull/21072/changes#r2742975180

Copy link
Copy Markdown
Contributor Author

@devajithvs devajithvs Jan 30, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a problem in nlohmann::json, scalar values are iterable and can dereference to themselves.

root [0] nlohmann::json j;
root [1] j[0] = 1;
root [2] auto it = j.begin();
root [3] bool(it == j.end())
(bool) false
root [4] auto& elem = *it;
root [5] auto elem_it = elem.begin();
root [6] bool(elem_it == elem.end()) // line 208 check
(bool) false
root [7] bool(&elem == &(*elem_it)) // iterator dereferences to itself and infinite recursion
(bool) true

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was trying to somewhat mimic what scalar values does in nlohmann::json, I have simplified the test case to avoid this confusion.

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 :)
Comment thread
pcanal marked this conversation as resolved.
//
// 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()
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
auto end() const { return this + 1; } // just to make sure begin() != end()
auto end() const { return this + 1; } // Hard code the size of the collection to be exactly one and it 'just' contains this object.

};
.rawInput 0

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

// expected-no-diagnostics
.q
Loading