Merge pull request #10994 from hercules-ci/fix-value-print-elided

Fix underflow in `printAttrs`, `printList`
This commit is contained in:
John Ericson 2024-06-30 10:11:47 -04:00 committed by GitHub
commit dc538ad2f7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 38 additions and 11 deletions

View file

@ -163,8 +163,8 @@ private:
EvalState & state;
PrintOptions options;
std::optional<ValuesSeen> seen;
size_t attrsPrinted = 0;
size_t listItemsPrinted = 0;
size_t totalAttrsPrinted = 0;
size_t totalListItemsPrinted = 0;
std::string indent;
void increaseIndent()
@ -345,11 +345,13 @@ private:
auto prettyPrint = shouldPrettyPrintAttrs(sorted);
size_t currentAttrsPrinted = 0;
for (auto & i : sorted) {
printSpace(prettyPrint);
if (attrsPrinted >= options.maxAttrs) {
printElided(sorted.size() - attrsPrinted, "attribute", "attributes");
if (totalAttrsPrinted >= options.maxAttrs) {
printElided(sorted.size() - currentAttrsPrinted, "attribute", "attributes");
break;
}
@ -357,7 +359,8 @@ private:
output << " = ";
print(*i.second, depth + 1);
output << ";";
attrsPrinted++;
totalAttrsPrinted++;
currentAttrsPrinted++;
}
decreaseIndent();
@ -402,11 +405,14 @@ private:
output << "[";
auto listItems = v.listItems();
auto prettyPrint = shouldPrettyPrintList(listItems);
size_t currentListItemsPrinted = 0;
for (auto elem : listItems) {
printSpace(prettyPrint);
if (listItemsPrinted >= options.maxListItems) {
printElided(listItems.size() - listItemsPrinted, "item", "items");
if (totalListItemsPrinted >= options.maxListItems) {
printElided(listItems.size() - currentListItemsPrinted, "item", "items");
break;
}
@ -415,7 +421,8 @@ private:
} else {
printNullptr();
}
listItemsPrinted++;
totalListItemsPrinted++;
currentListItemsPrinted++;
}
decreaseIndent();
@ -588,8 +595,8 @@ public:
void print(Value & v)
{
attrsPrinted = 0;
listItemsPrinted = 0;
totalAttrsPrinted = 0;
totalListItemsPrinted = 0;
indent.clear();
if (options.trackRepeated) {

View file

@ -6,4 +6,4 @@ error:
| ^
10|
error: cannot coerce a set to a string: { a = { a = { a = { a = "ha"; b = "ha"; c = "ha"; d = "ha"; e = "ha"; f = "ha"; g = "ha"; h = "ha"; j = "ha"; }; «4294967295 attributes elided» }; «4294967294 attributes elided» }; «4294967293 attributes elided» }
error: cannot coerce a set to a string: { a = { a = { a = { a = "ha"; b = "ha"; c = "ha"; d = "ha"; e = "ha"; f = "ha"; g = "ha"; h = "ha"; j = "ha"; }; «8 attributes elided» }; «8 attributes elided» }; «8 attributes elided» }

View file

@ -0,0 +1,9 @@
error:
… while evaluating a path segment
at /pwd/lang/eval-fail-nested-list-items.nix:11:6:
10|
11| "" + (let v = [ [ 1 2 3 4 5 6 7 8 ] [1 2 3 4]]; in builtins.deepSeq v v)
| ^
12|
error: cannot coerce a list to a string: [ [ 1 2 3 4 5 6 7 8 ] [ 1 «3 items elided» ] ]

View file

@ -0,0 +1,11 @@
# This reproduces https://github.com/NixOS/nix/issues/10993, for lists
# $ nix run nix/2.23.1 -- eval --expr '"" + (let v = [ [ 1 2 3 4 5 6 7 8 ] [1 2 3 4]]; in builtins.deepSeq v v)'
# error:
# … while evaluating a path segment
# at «string»:1:6:
# 1| "" + (let v = [ [ 1 2 3 4 5 6 7 8 ] [1 2 3 4]]; in builtins.deepSeq v v)
# | ^
#
# error: cannot coerce a list to a string: [ [ 1 2 3 4 5 6 7 8 ] [ 1 «4294967290 items elided» ] ]
"" + (let v = [ [ 1 2 3 4 5 6 7 8 ] [1 2 3 4]]; in builtins.deepSeq v v)