Skip to content

Commit 8366c7a

Browse files
committed
Fix pointer bounds and a lot of nullability in CoreFoundation
1 parent f9dc9cf commit 8366c7a

File tree

10 files changed

+452
-31
lines changed

10 files changed

+452
-31
lines changed

crates/header-translator/src/config.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,9 @@ pub enum PointerBounds {
712712
#[serde(rename = "single")]
713713
Single,
714714
#[serde(rename = "null-terminated")]
715-
NullTerminated, // Or TerminatedBy(b'\0')
715+
NullTerminated, // Equivalent to TerminatedBy(b'\0')
716+
#[serde(rename = "terminated-by")]
717+
TerminatedBy(String),
716718
#[serde(rename = "counted-by")]
717719
CountedBy(String),
718720
#[serde(rename = "sized-by")]

examples/corefoundation/runloop.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,7 @@ unsafe fn create_observer_unchecked<F: Fn(&CFRunLoopObserver, CFRunLoopActivity)
165165
callback(observer, activity);
166166
}
167167

168-
// This is marked `mut` to match the signature of `CFRunLoopObserver::new`,
169-
// but the information is copied, and not actually mutated.
170-
let mut context = CFRunLoopObserverContext {
168+
let context = CFRunLoopObserverContext {
171169
version: 0,
172170
// This pointer is retained by CF on creation.
173171
info: Arc::as_ptr(&callback) as *mut c_void,
@@ -187,7 +185,7 @@ unsafe fn create_observer_unchecked<F: Fn(&CFRunLoopObserver, CFRunLoopActivity)
187185
repeats,
188186
order,
189187
Some(callout::<F>),
190-
&mut context,
188+
Some(&context),
191189
)
192190
}
193191
.unwrap()
@@ -246,9 +244,7 @@ unsafe fn create_timer_unchecked<F: Fn(&CFRunLoopTimer) + 'static>(
246244
callback(timer);
247245
}
248246

249-
// This is marked `mut` to match the signature of `CFRunLoopTimer::new`,
250-
// but the information is copied, and not actually mutated.
251-
let mut context = CFRunLoopTimerContext {
247+
let context = CFRunLoopTimerContext {
252248
version: 0,
253249
// This pointer is retained by CF on creation.
254250
info: Arc::as_ptr(&callback) as *mut c_void,
@@ -269,7 +265,7 @@ unsafe fn create_timer_unchecked<F: Fn(&CFRunLoopTimer) + 'static>(
269265
0, // Documentation says to pass 0 for future compat.
270266
order,
271267
Some(callout::<F>),
272-
&mut context,
268+
Some(&context),
273269
)
274270
}
275271
.unwrap()
@@ -353,9 +349,7 @@ fn create_source<F: Fn(SourceData<'_>) + Send + Sync + 'static>(
353349
(signaller)(SourceData::Perform);
354350
}
355351

356-
// This is marked `mut` to match the signature of `CFRunLoopSource::new`,
357-
// but the information is copied, and not actually mutated.
358-
let mut context = CFRunLoopSourceContext {
352+
let context = CFRunLoopSourceContext {
359353
version: 0, // Version 0 source
360354
// This pointer is retained by CF on creation.
361355
info: Arc::as_ptr(&callback) as *mut c_void,
@@ -373,5 +367,5 @@ fn create_source<F: Fn(SourceData<'_>) + Send + Sync + 'static>(
373367
// with `Send + Sync`, so that is thread-safe too.
374368
//
375369
// `F: 'static`, so extending the lifetime of the closure is fine.
376-
unsafe { CFRunLoopSource::new(kCFAllocatorDefault, order, &mut context) }.unwrap()
370+
unsafe { CFRunLoopSource::new(kCFAllocatorDefault, order, &context) }.unwrap()
377371
}

framework-crates/objc2-core-foundation/src/array.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ impl<T: ?Sized> CFArray<T> {
7777
//
7878
// The objects are retained internally by the array, so we do not need
7979
// to keep them alive ourselves after this.
80-
let array = unsafe { CFArray::new(None, ptr, len, &kCFTypeArrayCallBacks) }
80+
let array = unsafe { CFArray::new(None, ptr, len, Some(&kCFTypeArrayCallBacks)) }
8181
.unwrap_or_else(|| failed_creating_array(len));
8282

8383
// SAFETY: The objects came from `T`.
@@ -107,7 +107,7 @@ impl<T: ?Sized> CFArray<T> {
107107
let ptr = objects.as_ptr().cast::<*const c_void>().cast_mut();
108108

109109
// SAFETY: Same as in `from_objects`.
110-
let array = unsafe { CFArray::new(None, ptr, len, &kCFTypeArrayCallBacks) }
110+
let array = unsafe { CFArray::new(None, ptr, len, Some(&kCFTypeArrayCallBacks)) }
111111
.unwrap_or_else(|| failed_creating_array(len));
112112

113113
// SAFETY: The objects came from `T`.
@@ -139,7 +139,7 @@ impl<T: ?Sized> CFMutableArray<T> {
139139

140140
// SAFETY: The objects are CFTypes (`T: Type` bound), and the array
141141
// callbacks are thus correct.
142-
let array = unsafe { CFMutableArray::new(None, capacity, &kCFTypeArrayCallBacks) }
142+
let array = unsafe { CFMutableArray::new(None, capacity, Some(&kCFTypeArrayCallBacks)) }
143143
.unwrap_or_else(|| failed_creating_array(capacity));
144144

145145
// SAFETY: The array contains no objects yet, and thus it's safe to
@@ -540,7 +540,7 @@ mod tests {
540540
fn array_with_invalid_pointers() {
541541
// without_provenance
542542
let ptr = [0 as _, 1 as _, 2 as _, 3 as _, usize::MAX as _].as_mut_ptr();
543-
let array = unsafe { CFArray::new(None, ptr, 1, null()) }.unwrap();
543+
let array = unsafe { CFArray::new(None, ptr, 1, None) }.unwrap();
544544
let value = unsafe { array.value_at_index(0) };
545545
assert!(value.is_null());
546546
}
@@ -550,7 +550,7 @@ mod tests {
550550
#[ignore = "aborts (as expected)"]
551551
fn object_array_cannot_contain_null() {
552552
let ptr = [null()].as_mut_ptr();
553-
let _array = unsafe { CFArray::new(None, ptr, 1, &kCFTypeArrayCallBacks) };
553+
let _array = unsafe { CFArray::new(None, ptr, 1, Some(&kCFTypeArrayCallBacks)) };
554554
}
555555

556556
#[test]

framework-crates/objc2-core-foundation/src/date.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ impl Ord for CFDate {
5151
fn cmp(&self, other: &Self) -> Ordering {
5252
// Documented that one should pass NULL here.
5353
let context = ptr::null_mut();
54-
unsafe { self.compare(Some(other), context) }.into()
54+
unsafe { self.compare(other, context) }.into()
5555
}
5656
}
5757

framework-crates/objc2-core-foundation/src/dictionary.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ impl<K: ?Sized, V: ?Sized> CFDictionary<K, V> {
8787
keys,
8888
values,
8989
len,
90-
&kCFTypeDictionaryKeyCallBacks,
91-
&kCFTypeDictionaryValueCallBacks,
90+
Some(&kCFTypeDictionaryKeyCallBacks),
91+
Some(&kCFTypeDictionaryValueCallBacks),
9292
)
9393
}
9494
.unwrap_or_else(|| failed_creating_dictionary(len));
@@ -129,8 +129,8 @@ impl<K: ?Sized, V: ?Sized> CFMutableDictionary<K, V> {
129129
CFMutableDictionary::new(
130130
None,
131131
capacity,
132-
&kCFTypeDictionaryKeyCallBacks,
133-
&kCFTypeDictionaryValueCallBacks,
132+
Some(&kCFTypeDictionaryKeyCallBacks),
133+
Some(&kCFTypeDictionaryValueCallBacks),
134134
)
135135
}
136136
.unwrap_or_else(|| failed_creating_dictionary(capacity));

framework-crates/objc2-core-foundation/src/number.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ impl Ord for CFNumber {
117117
fn cmp(&self, other: &Self) -> Ordering {
118118
// Documented that one should pass NULL here.
119119
let context = ptr::null_mut();
120-
unsafe { self.compare(Some(other), context) }.into()
120+
unsafe { self.compare(other, context) }.into()
121121
}
122122
}
123123

framework-crates/objc2-core-foundation/src/string.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ impl fmt::Display for CFString {
159159
false,
160160
buf.as_mut_ptr(),
161161
buf.len() as _,
162-
&mut read_utf8,
162+
Some(&mut read_utf8),
163163
)
164164
};
165165
if read_utf16 <= 0 {
@@ -243,7 +243,7 @@ mod tests {
243243
];
244244
for (cstr, encoding, expected) in table {
245245
let cstr = CStr::from_bytes_with_nul(cstr).unwrap();
246-
let s = unsafe { CFString::with_c_string(None, cstr.as_ptr(), encoding.0) }.unwrap();
246+
let s = unsafe { CFString::with_c_string(None, cstr, encoding.0) }.unwrap();
247247
assert_eq!(s.to_string(), expected);
248248
}
249249
}
@@ -335,7 +335,7 @@ mod tests {
335335
let s = unsafe {
336336
CFString::with_c_string(
337337
None,
338-
b"\x65\x26\0".as_ptr().cast(),
338+
CStr::from_bytes_with_nul_unchecked(b"\x65\x26\0"),
339339
CFStringBuiltInEncodings::EncodingUnicode.0,
340340
)
341341
}

0 commit comments

Comments
 (0)