Skip to content

Commit eb74baf

Browse files
committed
Use cell for FileList
as casting & to *mut is invalid
1 parent 51b6833 commit eb74baf

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

alpm/src/filelist.rs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::Result;
33

44
use alpm_sys::*;
55

6+
use std::cell::UnsafeCell;
67
use std::ffi::CString;
78
use std::fmt;
89
use std::mem;
@@ -38,8 +39,9 @@ impl File {
3839
}
3940
}
4041

42+
// TODO unsound: needs lifetime on handle
4143
pub struct FileList {
42-
pub(crate) inner: alpm_filelist_t,
44+
inner: UnsafeCell<alpm_filelist_t>,
4345
}
4446

4547
impl fmt::Debug for FileList {
@@ -49,22 +51,28 @@ impl fmt::Debug for FileList {
4951
}
5052

5153
impl FileList {
54+
pub(crate) unsafe fn new(files: alpm_filelist_t) -> FileList {
55+
FileList {
56+
inner: UnsafeCell::new(files),
57+
}
58+
}
59+
60+
pub(crate) fn as_ptr(&self) -> *mut alpm_filelist_t {
61+
self.inner.get()
62+
}
63+
5264
pub fn files(&self) -> &[File] {
53-
if self.inner.files.is_null() {
65+
let files = unsafe { *self.as_ptr() };
66+
if files.files.is_null() {
5467
unsafe { slice::from_raw_parts(mem::align_of::<File>() as *const File, 0) }
5568
} else {
56-
unsafe { slice::from_raw_parts(self.inner.files as *const File, self.inner.count) }
69+
unsafe { slice::from_raw_parts(files.files as *const File, files.count) }
5770
}
5871
}
5972

6073
pub fn contains<S: Into<Vec<u8>>>(&self, path: S) -> Result<Option<File>> {
6174
let path = CString::new(path).unwrap();
62-
let file = unsafe {
63-
alpm_filelist_contains(
64-
&self.inner as *const alpm_filelist_t as *mut alpm_filelist_t,
65-
path.as_ptr(),
66-
)
67-
};
75+
let file = unsafe { alpm_filelist_contains(self.as_ptr(), path.as_ptr()) };
6876

6977
if file.is_null() {
7078
Ok(None)

alpm/src/package.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ impl<'h> Pkg<'h> {
232232

233233
pub fn files(&self) -> FileList {
234234
let files = unsafe { *alpm_pkg_get_files(self.as_ptr()) };
235-
FileList { inner: files }
235+
unsafe { FileList::new(files) }
236236
}
237237

238238
pub fn backup(&self) -> AlpmList<'h, Backup> {
@@ -330,6 +330,16 @@ mod tests {
330330
assert_eq!(pkg.filename(), "");
331331
}
332332

333+
#[test]
334+
fn test_files_null() {
335+
let handle = Alpm::new("/", "tests/db").unwrap();
336+
let db = handle.register_syncdb("core", SigLevel::NONE).unwrap();
337+
let pkg = db.pkg("filesystem").unwrap();
338+
let files = pkg.files();
339+
340+
assert_eq!(files.files().len(), 0);
341+
}
342+
333343
#[test]
334344
fn test_groups() {
335345
let handle = Alpm::new("/", "tests/db").unwrap();

0 commit comments

Comments
 (0)