libutil: canonPath: error instead of panic on empty path

This could previously crash lix:

Before:
$ nix eval -E '{type="derivation"; drvPath="";}'
nix: lix/libutil/file-system.cc:45: Path nix::canonPath(PathView, bool): Assertion `path != ""' failed.
Aborted (core dumped)

After:
$ nix eval -E '{type="derivation"; drvPath="";}'
error:
       … while evaluating the drvPath of a derivation
         at «string»:1:21:
            1| {type="derivation"; drvPath="";}
             |                     ^

       error: path '' is not in the Nix store

Fixes #536

Change-Id: I406dc9e58047be8f263cf2e4bc3ed5da75a46602
This commit is contained in:
gilice
2025-04-12 16:31:28 +02:00
parent 81d4769849
commit 341e6049a7
3 changed files with 4 additions and 6 deletions
+1 -1
View File
@@ -77,7 +77,7 @@ std::optional<StorePath> Store::maybeParseStorePath(std::string_view path) const
{
// If it's not an absolute path, or if the dirname of the path isn't /nix/store
// (or whatever our storeDir is), then it can't be a store path.
if ((path.size() > 0 && path[0] != '/') || dirOf(canonPath(path)) != config().storeDir) {
if (path.size() == 0 || path[0] != '/' || dirOf(canonPath(path)) != config().storeDir) {
return std::nullopt;
}
try {
+2 -4
View File
@@ -42,14 +42,12 @@ Path absPath(Path path, std::optional<PathView> dir, bool resolveSymlinks)
Path canonPath(PathView path, bool resolveSymlinks)
{
assert(path != "");
if (path == "" || path[0] != '/')
throw Error("not an absolute path: '%1%'", path);
std::string s;
s.reserve(256);
if (path[0] != '/')
throw Error("not an absolute path: '%1%'", path);
std::string temp;
/* Count the number of times we follow a symlink and stop at some
+1 -1
View File
@@ -89,7 +89,7 @@ namespace nix {
ASSERT_ANY_THROW(canonPath("."));
ASSERT_ANY_THROW(canonPath(".."));
ASSERT_ANY_THROW(canonPath("../"));
ASSERT_DEATH({ canonPath(""); }, "path != \"\"");
ASSERT_ANY_THROW(canonPath(""));
}
/* ----------------------------------------------------------------------------