From 604561ba9ac45ee30385670b18f15731c541287b Mon Sep 17 00:00:00 2001 From: Robert Helgesson Date: Wed, 11 Aug 2021 23:30:00 +0200 Subject: [PATCH] lib.gvariant: escape newlines in strings See #2096 --- modules/lib/gvariant.nix | 5 +++-- tests/lib/types/gvariant-merge.nix | 8 ++++++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/modules/lib/gvariant.nix b/modules/lib/gvariant.nix index 92aa7d983..0704635a2 100644 --- a/modules/lib/gvariant.nix +++ b/modules/lib/gvariant.nix @@ -122,8 +122,9 @@ in rec { }; mkString = v: - mkPrimitive type.string v // { - __toString = self: "'${escape [ "'" "\\" ] self.value}'"; + let sanitize = s: replaceStrings [ "\n" ] [ "\\n" ] (escape [ "'" "\\" ] s); + in mkPrimitive type.string v // { + __toString = self: "'${sanitize self.value}'"; }; mkObjectpath = v: diff --git a/tests/lib/types/gvariant-merge.nix b/tests/lib/types/gvariant-merge.nix index 867534c1f..88440c40a 100644 --- a/tests/lib/types/gvariant-merge.nix +++ b/tests/lib/types/gvariant-merge.nix @@ -26,7 +26,11 @@ in { { string = "foo"; } { string = "foo"; } - { escapedString = "' \\"; } + { + escapedString = '' + '\ + ''; + } { tuple = mkTuple [ 1 [ "foo" ] ]; } @@ -47,7 +51,7 @@ in { bool = true emptyArray1 = @as [] emptyArray2 = @as [] - escapedString = '\' \\' + escapedString = '\'\\\n' float = 3.140000 int = 42 list = @as ['one','two']