If util::read_file_part's count argument is 0 (which can currently only
happen in unit tests), we'll do &result[0] where result is
default-initialized std::unique_ptr<uint8_t[]>. This is not allowed even
though we won't actually dereference the pointer. Found by compiling
with -D_GLIBCXX_ASSERTIONS.
Fixes #1288.
nonstd::expected<T, std::string>
read_file_part(const std::string& path, size_t pos, size_t count)
{
+ T result;
+ if (count == 0) {
+ return result;
+ }
+
Fd fd(open(path.c_str(), O_RDONLY | O_BINARY));
if (!fd) {
LOG("Failed to open {}: {}", path, strerror(errno));
int64_t ret = 0;
size_t bytes_read = 0;
- T result;
result.resize(count);
while (true) {
SUBCASE("util::Bytes")
{
- CHECK(util::read_file_part<util::Bytes>("test", 0, 0) == util::to_span(""));
+ auto lhs = util::read_file_part<util::Bytes>("test", 0, 0);
+ auto rhs = util::to_span("");
+ CHECK(lhs == rhs);
CHECK(util::read_file_part<util::Bytes>("test", 0, 6)
== util::to_span("banana"));
CHECK(util::read_file_part<util::Bytes>("test", 0, 1000)