1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-27 04:30:59 +01:00

libutil: Ensure that CanonPath does not contain NUL bytes

This, alongside the other invariants of the CanonPath is important
to uphold. std::filesystem happily crashes on NUL bytes in the constructor,
as we've seen with `path:%00` prior to c436b7a32a.
Best to stay clear of NUL bytes when we're talking about syscalls, especially
on Unix where strings are null terminated.

Very nice to have if we decide to switch over to pascal-style strings.
This commit is contained in:
Sergei Zimmerman 2025-10-14 02:33:38 +03:00
parent edf9163c22
commit 1633ceaff2
No known key found for this signature in database
3 changed files with 34 additions and 4 deletions

View file

@ -42,6 +42,15 @@ TEST(CanonPath, basic)
}
}
TEST(CanonPath, nullBytes)
{
std::string s = "/hello/world";
s[8] = '\0';
ASSERT_THROW(CanonPath("/").push(std::string(1, '\0')), BadCanonPath);
ASSERT_THROW(CanonPath(std::string_view(s)), BadCanonPath);
ASSERT_THROW(CanonPath(s, CanonPath::root), BadCanonPath);
}
TEST(CanonPath, from_existing)
{
CanonPath p0("foo//bar/");