Skip to content

Commit 9643740

Browse files
committed
Test that toll-free bridged CFArray's are still bounds-checked
1 parent e44d293 commit 9643740

3 files changed

Lines changed: 29 additions & 2 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/tests/Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ block2 = { workspace = true, features = ["std"] }
2828
objc2 = { workspace = true, features = ["std", "relax-void-encoding"] }
2929
objc2-foundation = { workspace = true, features = [
3030
"std",
31+
"objc2-core-foundation",
3132
"NSArray",
3233
"NSAttributedString",
3334
"NSDecimalNumber",
@@ -48,6 +49,12 @@ objc2-foundation = { workspace = true, features = [
4849
"NSThread",
4950
"NSValue",
5051
] }
52+
objc2-core-foundation = { workspace = true, features = [
53+
"std",
54+
"objc2",
55+
"CFArray",
56+
"CFNumber",
57+
] }
5158
rustc-demangle = "0.1.26"
5259
# Unbreak CI until this gets a new release:
5360
# https://github.com/mcountryman/simd-adler32/pull/37

crates/tests/src/bound_checks.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
#![cfg(target_vendor = "apple")]
66
#![cfg(feature = "exception")] // For `should_panic` to work
77

8-
use core::{panic::AssertUnwindSafe, ptr::NonNull};
8+
use core::panic::AssertUnwindSafe;
99
use std::string::ToString;
1010

1111
use objc2::AnyThread;
12+
use objc2_core_foundation::{CFArray, CFNumber};
1213
use objc2_foundation::{
1314
NSArray, NSAttributedString, NSDictionary, NSException, NSIndexSet, NSMethodSignature,
1415
NSMutableArray, NSNotFound, NSObject, NSOrderedSet, NSPointerArray, NSRange, NSString,
@@ -63,6 +64,24 @@ fn array() {
6364
});
6465
}
6566

67+
#[test]
68+
// FIXME: Encoding checks shouldn't be this strict.
69+
#[cfg_attr(
70+
debug_assertions,
71+
ignore = "type encoding is apparently... ^{__CFNumber=}"
72+
)]
73+
fn cf_array_toll_free_bridged() {
74+
let arr = CFArray::from_retained_objects(&[CFNumber::new_i32(42), CFNumber::new_f32(64.0)]);
75+
let arr: &NSArray<CFNumber> = arr.as_ref();
76+
77+
let _ = arr.objectAtIndex(0);
78+
let _ = arr.objectAtIndex(1);
79+
80+
assert_throws("index 100 beyond bounds [0 .. 1]", || {
81+
arr.objectAtIndex(100)
82+
});
83+
}
84+
6685
#[test]
6786
fn attributed_string() {
6887
let arr = unsafe {
@@ -90,7 +109,7 @@ fn index_set() {
90109
#[test]
91110
fn method_signature() {
92111
let sig = c"c@:@";
93-
let sig = unsafe { NSMethodSignature::signatureWithObjCTypes(sig) }.unwrap();
112+
let sig = NSMethodSignature::signatureWithObjCTypes(sig).unwrap();
94113
assert_throws("index (100) out of bounds [0, 2]", || {
95114
sig.getArgumentTypeAtIndex(100)
96115
});

0 commit comments

Comments
 (0)