Skip to content

Commit

Permalink
fix: use slice::from_raw_parts only if size > 0
Browse files Browse the repository at this point in the history
libarchive seems to pass a nullptr with size 0 to the archive_read_data_block callback.
This leads to a precondition violated assert in debug builds.
  • Loading branch information
mbehr1 committed Jun 22, 2024
1 parent 8a335bf commit fe31d24
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,10 +326,15 @@ impl<R: Read + Seek> ArchiveIterator<R> {
{
ffi::ARCHIVE_EOF => ArchiveContents::EndOfEntry,
ffi::ARCHIVE_OK | ffi::ARCHIVE_WARN => {
let content = slice::from_raw_parts(buffer as *const u8, size);
let write = target.write_all(content);
if let Err(e) = write {
ArchiveContents::Err(e.into())
if size > 0 {
// fixes: (as buffer is null then) unsafe precondition(s) violated: slice::from_raw_parts requires the pointer to be aligned and non-null, and the total size of the slice not to exceed `isize::MAX`
let content = slice::from_raw_parts(buffer as *const u8, size);
let write = target.write_all(content);
if let Err(e) = write {
ArchiveContents::Err(e.into())
} else {
ArchiveContents::DataChunk(target)
}
} else {
ArchiveContents::DataChunk(target)
}
Expand Down

0 comments on commit fe31d24

Please sign in to comment.