From e2fc3187926f1f62b93ee8e18f765087b0a7a9f9 Mon Sep 17 00:00:00 2001 From: Matthias Behr Date: Sat, 22 Jun 2024 15:52:07 +0200 Subject: [PATCH] fix: use slice::from_raw_parts only if size > 0 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. --- CHANGES.md | 3 +++ src/iterator.rs | 15 +++++++++++---- 2 files changed, 14 insertions(+), 4 deletions(-) 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) }