diff --git a/CHANGES.md b/CHANGES.md index 553651a..23ae91d 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -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 diff --git a/src/iterator.rs b/src/iterator.rs index 7b34a39..5b58bee 100644 --- a/src/iterator.rs +++ b/src/iterator.rs @@ -326,10 +326,17 @@ impl ArchiveIterator { { 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) }