aboutsummaryrefslogtreecommitdiff
path: root/src/libexpr/eval.cc
diff options
context:
space:
mode:
authorAlex Ameen <alex.ameen.tx@gmail.com>2023-05-09 09:45:12 -0500
committerAlex Ameen <alex.ameen.tx@gmail.com>2023-05-09 09:45:12 -0500
commitb72bc4a972fe568744d98b89d63adcd504cb586c (patch)
treec68afe392499121d6f5872eb2a7fef6c3b5ddb41 /src/libexpr/eval.cc
parent4539ab530ad23a8558512f784bd72c4cd0e72f13 (diff)
libexpr: quote reserved keys when printing
This fixes a bug in commands like `nix eval' which would emit invalid attribute sets if they contained reserved keywords such as "assert", "let", etc. These keywords will not be quoted when printed, making them valid expressions. All keywords recognized by the lexer are quoted except "or", which does not require quotation.
Diffstat (limited to 'src/libexpr/eval.cc')
-rw-r--r--src/libexpr/eval.cc9
1 files changed, 7 insertions, 2 deletions
diff --git a/src/libexpr/eval.cc b/src/libexpr/eval.cc
index e2b455b91..af9d037ec 100644
--- a/src/libexpr/eval.cc
+++ b/src/libexpr/eval.cc
@@ -94,7 +94,6 @@ RootValue allocRootValue(Value * v)
#endif
}
-
void Value::print(const SymbolTable & symbols, std::ostream & str,
std::set<const void *> * seen) const
{
@@ -122,7 +121,13 @@ void Value::print(const SymbolTable & symbols, std::ostream & str,
else {
str << "{ ";
for (auto & i : attrs->lexicographicOrder(symbols)) {
- str << symbols[i->name] << " = ";
+ // Quote reserved keywords so that the output can be
+ // re-evaluated later without upsetting the lexer.
+ if (isReservedKeyword(symbols[i->name])) {
+ str << "\"" << symbols[i->name] << "\" = ";
+ } else {
+ str << symbols[i->name] << " = ";
+ }
i->value->print(symbols, str, seen);
str << "; ";
}