GitFileSystemObjectSink: catch an overflow

This commit is contained in:
Robert Hensing 2024-07-01 17:14:27 +02:00
parent a409c1a882
commit f0329568b5
2 changed files with 22 additions and 0 deletions

View file

@ -931,6 +931,8 @@ struct GitFileSystemObjectSinkImpl : GitFileSystemObjectSink
++dir;
relTargetLeft = relTargetLeft.substr(3);
}
if (dir == pendingDirs.rend())
throw Error("invalid hard link target '%s' for path '%s'", target, path);
// Look up the remainder of the target, starting at the
// top-most `git_treebuilder`.

View file

@ -87,4 +87,24 @@ TEST_F(GitUtilsTest, sink_basic)
ASSERT_EQ(accessor->readFile(CanonPath("links/foo")), "hello world");
};
TEST_F(GitUtilsTest, sink_hardlink)
{
auto repo = openRepo();
auto sink = repo->getFileSystemObjectSink();
sink->createDirectory("foo-1.1");
sink->createRegularFile(
"foo-1.1/hello", [](CreateRegularFileSink & fileSink) { writeString(fileSink, "hello world", false); });
try {
sink->createHardlink("foo-1.1/link", CanonPath("hello"));
FAIL() << "Expected an exception";
} catch (const nix::Error & e) {
ASSERT_THAT(e.msg(), testing::HasSubstr("invalid hard link target"));
ASSERT_THAT(e.msg(), testing::HasSubstr("/hello"));
ASSERT_THAT(e.msg(), testing::HasSubstr("foo-1.1/link"));
}
};
} // namespace nix