Unexpected Abort on nix::canonPath(PathView, bool): Assertion path != ""' failed.` #536

Closed
opened 2024-09-29 01:21:14 +00:00 by samueldr · 2 comments
Member

Describe the bug

~ $ nix repl '<nixpkgs>'
Lix 2.91.0
Type :? for help.
Loading installable ''...
Added 22730 variables.
nix-repl> :p (hello // { drvPath = ""; })
nix: src/libutil/file-system.cc:39: nix::Path nix::canonPath(PathView, bool): Assertion `path != ""' failed.
Aborted (core dumped)

Steps To Reproduce

Sorry.

Expected behavior

Unsure. At least not aborting.

nix --version output

~ $ nix --version
nix (Lix, like Nix) 2.91.0

Additional context

Not a huge issue. Just surprising sudden loss of REPL :).

## Describe the bug ``` ~ $ nix repl '<nixpkgs>' Lix 2.91.0 Type :? for help. Loading installable ''... Added 22730 variables. nix-repl> :p (hello // { drvPath = ""; }) nix: src/libutil/file-system.cc:39: nix::Path nix::canonPath(PathView, bool): Assertion `path != ""' failed. Aborted (core dumped) ``` ## Steps To Reproduce Sorry. ## Expected behavior Unsure. At least not aborting. ## `nix --version` output ``` ~ $ nix --version nix (Lix, like Nix) 2.91.0 ``` ## Additional context Not a huge issue. Just surprising sudden loss of REPL :).
Member

The minimal patch for this would be

diff --git a/lix/libutil/file-system.cc b/lix/libutil/file-system.cc
index 9322e96c1..a67bdc718 100644
--- a/lix/libutil/file-system.cc
+++ b/lix/libutil/file-system.cc
@@ -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
diff --git a/tests/unit/libutil/tests.cc b/tests/unit/libutil/tests.cc
index 00fcf8269..bbf285bea 100644
--- a/tests/unit/libutil/tests.cc
+++ b/tests/unit/libutil/tests.cc
@@ -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(""));
     }
 
     /* ----------------------------------------------------------------------------

This passes all tests and results in the following repr:

$ nix repl '<nixpkgs>'
warning: future versions of Lix will require using `--file` to load a file
Lix 2.93.0-dev
Type :? for help.
Loading installable ''...
Added 23751 variables.
nix-repl> :p (hello // { drvPath = ""; })
error: not an absolute path: ''

I am however not sure if the rest of the codebase assumes anything about how canonPath works, so not filing a PR yet. (It is especially suspicious that there was a test asserting it will throw.)
Maybe someone else can weigh in?

The minimal patch for this would be ```diff diff --git a/lix/libutil/file-system.cc b/lix/libutil/file-system.cc index 9322e96c1..a67bdc718 100644 --- a/lix/libutil/file-system.cc +++ b/lix/libutil/file-system.cc @@ -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 diff --git a/tests/unit/libutil/tests.cc b/tests/unit/libutil/tests.cc index 00fcf8269..bbf285bea 100644 --- a/tests/unit/libutil/tests.cc +++ b/tests/unit/libutil/tests.cc @@ -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("")); } /* ---------------------------------------------------------------------------- ``` This passes all tests and results in the following repr: ```bash $ nix repl '<nixpkgs>' warning: future versions of Lix will require using `--file` to load a file Lix 2.93.0-dev Type :? for help. Loading installable ''... Added 23751 variables. nix-repl> :p (hello // { drvPath = ""; }) error: not an absolute path: '' ``` I am however not sure if the rest of the codebase assumes anything about how `canonPath` works, so not filing a PR yet. (It is especially suspicious that there was a test asserting it will throw.) Maybe someone else can weigh in?
Member

This issue was mentioned on Gerrit on the following CLs:

  • commit message in cl/2986 ("libutil: canonPath: error instead of panic on empty path")
<!-- GERRIT_LINKBOT: {"cls": [{"backlink": "https://gerrit.lix.systems/c/lix/+/2986", "number": 2986, "kind": "commit message"}], "cl_meta": {"2986": {"change_title": "libutil: canonPath: error instead of panic on empty path"}}} --> This issue was mentioned on Gerrit on the following CLs: * commit message in [cl/2986](https://gerrit.lix.systems/c/lix/+/2986) ("libutil: canonPath: error instead of panic on empty path")
Sign in to join this conversation.
No milestone
No project
No assignees
3 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
lix-project/lix#536
No description provided.