Skip to content

Commit

Permalink
Merge pull request #10994 from hercules-ci/fix-value-print-elided
Browse files Browse the repository at this point in the history
Fix underflow in `printAttrs`, `printList`
  • Loading branch information
Ericson2314 authored Jun 30, 2024
2 parents 32e6cc6 + b2c7f09 commit dc538ad
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 11 deletions.
27 changes: 17 additions & 10 deletions src/libexpr/print.cc
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,8 @@ class Printer
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()
Expand Down Expand Up @@ -345,19 +345,22 @@ class Printer

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;
}

printAttributeName(output, i.first);
output << " = ";
print(*i.second, depth + 1);
output << ";";
attrsPrinted++;
totalAttrsPrinted++;
currentAttrsPrinted++;
}

decreaseIndent();
Expand Down Expand Up @@ -402,11 +405,14 @@ class Printer
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;
}

Expand All @@ -415,7 +421,8 @@ class Printer
} else {
printNullptr();
}
listItemsPrinted++;
totalListItemsPrinted++;
currentListItemsPrinted++;
}

decreaseIndent();
Expand Down Expand Up @@ -588,8 +595,8 @@ class Printer

void print(Value & v)
{
attrsPrinted = 0;
listItemsPrinted = 0;
totalAttrsPrinted = 0;
totalListItemsPrinted = 0;
indent.clear();

if (options.trackRepeated) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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» }
9 changes: 9 additions & 0 deletions tests/functional/lang/eval-fail-nested-list-items.err.exp
Original file line number Diff line number Diff line change
@@ -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» ] ]
11 changes: 11 additions & 0 deletions tests/functional/lang/eval-fail-nested-list-items.nix
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit dc538ad

Please sign in to comment.