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 authored and otavio committed Jun 24, 2024
1 parent 8a335bf commit e2fc318
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
## [Unreleased] - ReleaseDate

* Raise MSRV to 1.63.0
* Fix use slice::from_raw_parts only if size > 0 [#126]

[#126]: https://github.com/OSSystems/compress-tools-rs/pull/126

## [0.14.3] - 2023-05-26

Expand Down
15 changes: 11 additions & 4 deletions src/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,10 +326,17 @@ 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 e2fc318

Please sign in to comment.