Skip to content

Commit

Permalink
Fix undefined behavior sanitizer violations
Browse files Browse the repository at this point in the history
- memcpy can't be called with nullptr
- chunk headers should be aligned to 8 bytes as they contain uint64_t
  • Loading branch information
zeux committed Oct 27, 2024
1 parent 68afe90 commit bf86435
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/build.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,8 @@ static ChunkData prepareChunkData(const Chunk& chunk)
const File& f = chunk.files[i];

memcpy(result.data.get() + nameOffset, f.name.c_str(), f.name.length());
memcpy(result.data.get() + dataOffset, f.contents.data(), f.contents.size());
if (f.contents.size())
memcpy(result.data.get() + dataOffset, f.contents.data(), f.contents.size());

DataChunkFileHeader& h = reinterpret_cast<DataChunkFileHeader*>(result.data.get())[i];

Expand Down
7 changes: 5 additions & 2 deletions src/update.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,15 +186,18 @@ static bool processFile(Output* output, BuildContext* builder, UpdateFileIterato
{
std::unique_ptr<char[]> extra(new (std::nothrow) char[chunk.extraSize]);
std::unique_ptr<char[]> index(new (std::nothrow) char[chunk.indexSize]);
std::unique_ptr<char[]> data(new (std::nothrow) char[chunk.compressedSize + chunk.uncompressedSize]);

size_t uncompressedOffset = (chunk.compressedSize + 7) & ~7; // make sure uncompressed data is aligned

std::unique_ptr<char[]> data(new (std::nothrow) char[uncompressedOffset + chunk.uncompressedSize]);

if (!extra || !index || !data || !read(in, extra.get(), chunk.extraSize) || !read(in, index.get(), chunk.indexSize) || !read(in, data.get(), chunk.compressedSize))
{
output->error("Error reading data file %s: malformed chunk\n", path);
return false;
}

char* uncompressed = data.get() + chunk.compressedSize;
char* uncompressed = data.get() + uncompressedOffset;

processChunkData(output, builder, fileit, stats, chunk, uncompressed, data, index, extra);
}
Expand Down

0 comments on commit bf86435

Please sign in to comment.