Skip to content

Commit 2c86cfa

Browse files
committed
Avoid segmentation fault when failing to decode entry
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>
1 parent cd6ccf3 commit 2c86cfa

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
## [Unreleased] - ReleaseDate
66

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

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

1113
## [0.14.0] - 2022-11-20
1214

src/lib.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ where
131131
}
132132

133133
let _utf8_guard = ffi::WindowsUTF8LocaleGuard::new();
134-
let cstr = CStr::from_ptr(ffi::archive_entry_pathname(entry));
134+
let cstr = libarchive_entry_pathname(entry)?;
135135
let file_name = decode(cstr.to_bytes())?;
136136
file_list.push(file_name);
137137
}
@@ -246,7 +246,7 @@ where
246246
}
247247

248248
let _utf8_guard = ffi::WindowsUTF8LocaleGuard::new();
249-
let cstr = CStr::from_ptr(ffi::archive_entry_pathname(entry));
249+
let cstr = libarchive_entry_pathname(entry)?;
250250
let target_path = CString::new(
251251
dest.join(sanitize_destination_path(Path::new(&decode(
252252
cstr.to_bytes(),
@@ -356,7 +356,7 @@ where
356356
}
357357

358358
let _utf8_guard = ffi::WindowsUTF8LocaleGuard::new();
359-
let cstr = CStr::from_ptr(ffi::archive_entry_pathname(entry));
359+
let cstr = libarchive_entry_pathname(entry)?;
360360
let file_name = decode(cstr.to_bytes())?;
361361
if file_name == path {
362362
break;
@@ -580,6 +580,19 @@ fn libarchive_copy_data(
580580
}
581581
}
582582

583+
fn libarchive_entry_pathname<'a>(entry: *mut ffi::archive_entry) -> Result<&'a CStr> {
584+
let pathname = unsafe { ffi::archive_entry_pathname(entry) };
585+
if pathname.is_null() {
586+
return Err(io::Error::new(
587+
io::ErrorKind::InvalidData,
588+
"archive entry has unreadable filename.".to_string(),
589+
)
590+
.into());
591+
}
592+
593+
Ok(unsafe { CStr::from_ptr(pathname) })
594+
}
595+
583596
unsafe fn libarchive_write_data_block<W>(
584597
archive_reader: *mut ffi::archive,
585598
mut target: W,

0 commit comments

Comments
 (0)