Merge pull request #221204 from tweag/deprecate-paths-to-strings

lib.strings: Prevent paths as inputs in some functions
This commit is contained in:
Silvan Mosberger 2023-04-04 18:35:06 +02:00 committed by GitHub
commit 4f35a58184
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,7 +2,9 @@
{ lib }: { lib }:
let let
inherit (builtins) length; inherit (builtins) length;
inherit (lib.trivial) warnIf;
asciiTable = import ./ascii-table.nix; asciiTable = import ./ascii-table.nix;
@ -207,7 +209,20 @@ rec {
normalizePath "/a//b///c/" normalizePath "/a//b///c/"
=> "/a/b/c/" => "/a/b/c/"
*/ */
normalizePath = s: (builtins.foldl' (x: y: if y == "/" && hasSuffix "/" x then x else x+y) "" (stringToCharacters s)); normalizePath = s:
warnIf
(isPath s)
''
lib.strings.normalizePath: The argument (${toString s}) is a path value, but only strings are supported.
Path values are always normalised in Nix, so there's no need to call this function on them.
This function also copies the path to the Nix store and returns the store path, the same as "''${path}" will, which may not be what you want.
This behavior is deprecated and will throw an error in the future.''
(
builtins.foldl'
(x: y: if y == "/" && hasSuffix "/" x then x else x+y)
""
(stringToCharacters s)
);
/* Depending on the boolean `cond', return either the given string /* Depending on the boolean `cond', return either the given string
or the empty string. Useful to concatenate against a bigger string. or the empty string. Useful to concatenate against a bigger string.
@ -240,7 +255,17 @@ rec {
# Prefix to check for # Prefix to check for
pref: pref:
# Input string # Input string
str: substring 0 (stringLength pref) str == pref; str:
# Before 23.05, paths would be copied to the store before converting them
# to strings and comparing. This was surprising and confusing.
warnIf
(isPath pref)
''
lib.strings.hasPrefix: The first argument (${toString pref}) is a path value, but only strings are supported.
There is almost certainly a bug in the calling code, since this function always returns `false` in such a case.
This function also copies the path to the Nix store, which may not be what you want.
This behavior is deprecated and will throw an error in the future.''
(substring 0 (stringLength pref) str == pref);
/* Determine whether a string has given suffix. /* Determine whether a string has given suffix.
@ -260,8 +285,20 @@ rec {
let let
lenContent = stringLength content; lenContent = stringLength content;
lenSuffix = stringLength suffix; lenSuffix = stringLength suffix;
in lenContent >= lenSuffix && in
substring (lenContent - lenSuffix) lenContent content == suffix; # Before 23.05, paths would be copied to the store before converting them
# to strings and comparing. This was surprising and confusing.
warnIf
(isPath suffix)
''
lib.strings.hasSuffix: The first argument (${toString suffix}) is a path value, but only strings are supported.
There is almost certainly a bug in the calling code, since this function always returns `false` in such a case.
This function also copies the path to the Nix store, which may not be what you want.
This behavior is deprecated and will throw an error in the future.''
(
lenContent >= lenSuffix
&& substring (lenContent - lenSuffix) lenContent content == suffix
);
/* Determine whether a string contains the given infix /* Determine whether a string contains the given infix
@ -278,7 +315,16 @@ rec {
=> false => false
*/ */
hasInfix = infix: content: hasInfix = infix: content:
builtins.match ".*${escapeRegex infix}.*" "${content}" != null; # Before 23.05, paths would be copied to the store before converting them
# to strings and comparing. This was surprising and confusing.
warnIf
(isPath infix)
''
lib.strings.hasInfix: The first argument (${toString infix}) is a path value, but only strings are supported.
There is almost certainly a bug in the calling code, since this function always returns `false` in such a case.
This function also copies the path to the Nix store, which may not be what you want.
This behavior is deprecated and will throw an error in the future.''
(builtins.match ".*${escapeRegex infix}.*" "${content}" != null);
/* Convert a string to a list of characters (i.e. singleton strings). /* Convert a string to a list of characters (i.e. singleton strings).
This allows you to, e.g., map a function over each character. However, This allows you to, e.g., map a function over each character. However,
@ -570,14 +616,23 @@ rec {
prefix: prefix:
# Input string # Input string
str: str:
let # Before 23.05, paths would be copied to the store before converting them
# to strings and comparing. This was surprising and confusing.
warnIf
(isPath prefix)
''
lib.strings.removePrefix: The first argument (${toString prefix}) is a path value, but only strings are supported.
There is almost certainly a bug in the calling code, since this function never removes any prefix in such a case.
This function also copies the path to the Nix store, which may not be what you want.
This behavior is deprecated and will throw an error in the future.''
(let
preLen = stringLength prefix; preLen = stringLength prefix;
sLen = stringLength str; sLen = stringLength str;
in in
if hasPrefix prefix str then if substring 0 preLen str == prefix then
substring preLen (sLen - preLen) str substring preLen (sLen - preLen) str
else else
str; str);
/* Return a string without the specified suffix, if the suffix matches. /* Return a string without the specified suffix, if the suffix matches.
@ -594,14 +649,23 @@ rec {
suffix: suffix:
# Input string # Input string
str: str:
let # Before 23.05, paths would be copied to the store before converting them
# to strings and comparing. This was surprising and confusing.
warnIf
(isPath suffix)
''
lib.strings.removeSuffix: The first argument (${toString suffix}) is a path value, but only strings are supported.
There is almost certainly a bug in the calling code, since this function never removes any suffix in such a case.
This function also copies the path to the Nix store, which may not be what you want.
This behavior is deprecated and will throw an error in the future.''
(let
sufLen = stringLength suffix; sufLen = stringLength suffix;
sLen = stringLength str; sLen = stringLength str;
in in
if sufLen <= sLen && suffix == substring (sLen - sufLen) sufLen str then if sufLen <= sLen && suffix == substring (sLen - sufLen) sufLen str then
substring 0 (sLen - sufLen) str substring 0 (sLen - sufLen) str
else else
str; str);
/* Return true if string v1 denotes a version older than v2. /* Return true if string v1 denotes a version older than v2.