Skip to content

Commit

Permalink
Avoid segmentation fault when failing to decode entry
Browse files Browse the repository at this point in the history
If operating system doesn't has support for the required locale,
`ffi::archive_entry_pathname(...)` returns NULL and we need to error out
accordingly.

Fixes: #100
Signed-off-by: Otavio Salvador <otavio@ossystems.com.br>
  • Loading branch information
otavio committed Jan 9, 2023
1 parent cd6ccf3 commit 2c86cfa
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
## [Unreleased] - ReleaseDate

* Add illumos compilation support [#99]
* Fix segmentation when failing to decode entry [#100]

[#99]: https://github.com/OSSystems/compress-tools-rs/pull/99
[#100]: https://github.com/OSSystems/compress-tools-rs/issues/100

## [0.14.0] - 2022-11-20

Expand Down
19 changes: 16 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ where
}

let _utf8_guard = ffi::WindowsUTF8LocaleGuard::new();
let cstr = CStr::from_ptr(ffi::archive_entry_pathname(entry));
let cstr = libarchive_entry_pathname(entry)?;
let file_name = decode(cstr.to_bytes())?;
file_list.push(file_name);
}
Expand Down Expand Up @@ -246,7 +246,7 @@ where
}

let _utf8_guard = ffi::WindowsUTF8LocaleGuard::new();
let cstr = CStr::from_ptr(ffi::archive_entry_pathname(entry));
let cstr = libarchive_entry_pathname(entry)?;
let target_path = CString::new(
dest.join(sanitize_destination_path(Path::new(&decode(
cstr.to_bytes(),
Expand Down Expand Up @@ -356,7 +356,7 @@ where
}

let _utf8_guard = ffi::WindowsUTF8LocaleGuard::new();
let cstr = CStr::from_ptr(ffi::archive_entry_pathname(entry));
let cstr = libarchive_entry_pathname(entry)?;
let file_name = decode(cstr.to_bytes())?;
if file_name == path {
break;
Expand Down Expand Up @@ -580,6 +580,19 @@ fn libarchive_copy_data(
}
}

fn libarchive_entry_pathname<'a>(entry: *mut ffi::archive_entry) -> Result<&'a CStr> {
let pathname = unsafe { ffi::archive_entry_pathname(entry) };
if pathname.is_null() {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
"archive entry has unreadable filename.".to_string(),
)
.into());
}

Ok(unsafe { CStr::from_ptr(pathname) })
}

unsafe fn libarchive_write_data_block<W>(
archive_reader: *mut ffi::archive,
mut target: W,
Expand Down

0 comments on commit 2c86cfa

Please sign in to comment.