Unverified Commit 487a002c authored by Mikael Voss's avatar Mikael Voss
Browse files

lib/strings: Zero‐pad hex digits in escapeC

lib.strings.escapeC produces single‐digit hexadecimal strings for
character values ≤ 15, which results in an ambiguity. If the following
character is a hex digit, it will be interpreted as being part of the
escape sequence.

systemd, which also relies on C‐style escape sequences, does not
decode single‐digit sequences at all, even if unambiguous.

Padding the hexadecimal string with "0" avoids this problem.
parent 76614a7c
Loading
Loading
Loading
Loading
+5 −1
Original line number Diff line number Diff line
@@ -998,7 +998,11 @@ rec {

    :::
  */
  escapeC = list: replaceStrings list (map (c: "\\x${toLower (lib.toHexString (charToInt c))}") list);
  escapeC =
    list:
    replaceStrings list (
      map (c: "\\x${fixedWidthString 2 "0" (toLower (lib.toHexString (charToInt c)))}") list
    );

  /**
    Escape the `string` so it can be safely placed inside a URL
+2 −2
Original line number Diff line number Diff line
@@ -851,8 +851,8 @@ runTests {
  };

  testEscapeC = {
    expr = strings.escapeC [ " " ] "Hello World";
    expected = "Hello\\x20World";
    expr = strings.escapeC [ "\n" " " ] "Hello World\n";
    expected = "Hello\\x20World\\x0a";
  };

  testEscapeURL = testAllTrue [