From 9b88bf8adf721d6b2d1a5666a97a0c6e9046a2d7 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Sat, 29 Jun 2024 13:48:17 +0200 Subject: [PATCH] Fix underflow in Printer::printAttrs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The code that counts the number of elided attrs incorrectly used the per-printer "global" attribute counter instead of a counter that was relevant only to the current attribute set. This bug flew under the radar because often the attribute sets aren't nested, not big enough, or we wouldn't pay attention to the numbers. I've noticed the issue because the difference underflowed. Although this behavior is tested by the functional test lang/eval-fail-bad-string-interpolation-4.nix, the underflow slipped through review. A simpler reproducer would be as follows, but I haven't added it to the test suite to keep it simple and marginally faster. ``` $ nix run nix/2.23.1 -- eval --expr '"" + (let v = { a = { a = 1; b = 2; c = 1; d = 1; e = 1; f = 1; g = 1; h = 1; }; b = { a = 1; b = 1; c = 1; }; }; in builtins.deepSeq v v)' error: … while evaluating a path segment at «string»:1:6: 1| "" + (let v = { a = { a = 1; b = 2; c = 1; d = 1; e = 1; f = 1; g = 1; h = 1; }; b = { a = 1; b = 1; c = 1; }; }; in builtins.deepSeq v v) | ^ error: cannot coerce a set to a string: { a = { a = 1; b = 2; c = 1; d = 1; e = 1; f = 1; g = 1; h = 1; }; b = { a = 1; «4294967289 attributes elided» }; } ``` --- src/libexpr/print.cc | 5 ++++- .../lang/eval-fail-bad-string-interpolation-4.err.exp | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/libexpr/print.cc b/src/libexpr/print.cc index 10fe7923f..4e44fa721 100644 --- a/src/libexpr/print.cc +++ b/src/libexpr/print.cc @@ -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"); + printElided(sorted.size() - currentAttrsPrinted, "attribute", "attributes"); break; } @@ -358,6 +360,7 @@ private: print(*i.second, depth + 1); output << ";"; attrsPrinted++; + currentAttrsPrinted++; } decreaseIndent(); diff --git a/tests/functional/lang/eval-fail-bad-string-interpolation-4.err.exp b/tests/functional/lang/eval-fail-bad-string-interpolation-4.err.exp index 6f907106b..b262e814d 100644 --- a/tests/functional/lang/eval-fail-bad-string-interpolation-4.err.exp +++ b/tests/functional/lang/eval-fail-bad-string-interpolation-4.err.exp @@ -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» }