Merge "libutil/args: fix crash when NIX_GET_COMPLETIONS is not a number" into main

This commit is contained in:
71rd
2025-05-27 23:31:40 +00:00
committed by Lix Systems Gerrit
2 changed files with 12 additions and 1 deletions
+7 -1
View File
@@ -86,7 +86,13 @@ void RootArgs::parseCmdline(const Strings & _cmdline)
Strings cmdline(_cmdline);
if (auto s = getEnv("NIX_GET_COMPLETIONS")) {
size_t n = std::stoi(*s);
size_t n = [&] {
if (auto parsed = string2Int<size_t>(*s)) {
return *parsed;
}
throw UsageError("Invalid value for environment variable NIX_GET_COMPLETIONS: %s", *s);
}();
if (!(n > 0 && n <= cmdline.size()))
throw UsageError("Invalid word number to get completion for: %zu\n. Your autocompletions might be misconfigured", n);
*std::next(cmdline.begin(), n - 1) += completionMarker;
+5
View File
@@ -37,6 +37,11 @@ EOF
[[ "$(NIX_GET_COMPLETIONS=1 nix buil)" == $'normal\nbuild\t' ]]
[[ "$(NIX_GET_COMPLETIONS=2 nix flake metad)" == $'normal\nmetadata\t' ]]
# Test how completion fails if the value cant be parsed as a number
NIX_GET_COMPLETIONS="-" expectStderr 1 nix \
| grepQuiet "error: Invalid value for environment variable NIX_GET_COMPLETIONS:"
# Test how completion fails if the number is not a valid index for the number of arguments
NIX_GET_COMPLETIONS=0 expectStderr 1 nix \
| grepQuiet "error: Invalid word number to get completion for:"