Skip to content

Commit cddd02a

Browse files
authored
Merge pull request #56 from archlinux/siglist
Fix UB when using empty SigList
2 parents b0a9e24 + c533f02 commit cddd02a

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

alpm/src/signing.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use alpm_sys::*;
77

88
use std::ffi::{c_void, CString};
99
use std::mem::transmute;
10+
use std::ptr::NonNull;
1011
use std::{fmt, ptr, slice};
1112

1213
#[derive(Debug, Eq, PartialEq, Copy, Clone, Ord, PartialOrd, Hash)]
@@ -67,7 +68,7 @@ impl fmt::Debug for PgpKey {
6768
f.debug_struct("PgpKey")
6869
.field("name", &self.name())
6970
.field("email", &self.email())
70-
.field("uid", &self.uid())
71+
.field("uid", &self.uid_optional())
7172
.field("fingerprint", &self.fingerprint())
7273
.field("created", &self.created())
7374
.field("expires", &self.expires())
@@ -82,8 +83,13 @@ impl PgpKey {
8283
unsafe { from_cstr(self.inner.fingerprint) }
8384
}
8485

86+
pub fn uid_optional(&self) -> Option<&str> {
87+
unsafe { from_cstr_optional(self.inner.uid) }
88+
}
89+
90+
#[deprecated = "uid may be none. Use uid_optional() instead. This function will return an option instead in the next major release."]
8591
pub fn uid(&self) -> &str {
86-
unsafe { from_cstr(self.inner.uid) }
92+
unsafe { from_cstr_optional(self.inner.uid).unwrap_or("") }
8793
}
8894

8995
pub fn name(&self) -> Option<&str> {
@@ -181,7 +187,7 @@ impl SigList {
181187

182188
pub fn results(&self) -> &[SigResult] {
183189
if self.inner.results.is_null() {
184-
unsafe { slice::from_raw_parts(1 as *const SigResult, 0) }
190+
unsafe { slice::from_raw_parts(NonNull::dangling().as_ptr(), 0) }
185191
} else {
186192
unsafe {
187193
slice::from_raw_parts(self.inner.results as *const SigResult, self.inner.count)

0 commit comments

Comments
 (0)