Skip to content

Commit

Permalink
Add CFString conversion to/from Rust strings
Browse files Browse the repository at this point in the history
  • Loading branch information
madsmtm committed Jan 10, 2025
1 parent 3468e00 commit c76799e
Show file tree
Hide file tree
Showing 10 changed files with 384 additions and 72 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions crates/test-fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ cargo-fuzz = true
libfuzzer-sys = "0.4"
objc2 = { path = "../objc2" }
objc2-foundation = { path = "../../framework-crates/objc2-foundation" }
objc2-core-foundation = { path = "../../framework-crates/objc2-core-foundation" }
arbitrary = { version = "1", features = ["derive"] }
afl = { version = "0.15", optional = true }

Expand All @@ -38,6 +39,19 @@ fuzz-all = [
"objc2-foundation/NSString",
"objc2-foundation/NSZone",
"objc2-foundation/unstable-mutation-return-null",
"objc2-core-foundation/CFBase",
"objc2-core-foundation/CFString",
]

[[bin]]
name = "cfstring"
path = "fuzz_targets/cfstring.rs"
test = false
doc = false
bench = false
required-features = [
"objc2-core-foundation/CFBase",
"objc2-core-foundation/CFString",
]

[[bin]]
Expand Down
17 changes: 17 additions & 0 deletions crates/test-fuzz/fuzz_targets/cfstring.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#![cfg_attr(not(feature = "afl"), no_main)]
use objc2_core_foundation::CFString;

fn run(s: &str) {
let obj = CFString::from_str(s);
assert_eq!(obj.to_string(), s);
}

#[cfg(not(feature = "afl"))]
libfuzzer_sys::fuzz_target!(|s: &str| run(s));

#[cfg(feature = "afl")]
fn main() {
afl::fuzz!(|s: &str| {
run(s);
});
}
3 changes: 3 additions & 0 deletions crates/test-ui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ default = [
"objc2-foundation/NSURLSession",
"objc2-foundation/NSEnumerator",
"objc2-foundation/NSDictionary",
"objc2-core-foundation/CFBase",
"objc2-core-foundation/CFString",
"objc2/unstable-msg-send-always-comma",
]
run = ["trybuild"]
Expand All @@ -40,6 +42,7 @@ trybuild = { version = "1.0.72", optional = true }
block2 = { path = "../block2" }
objc2 = { path = "../objc2" }
objc2-foundation = { path = "../../framework-crates/objc2-foundation", default-features = false, features = ["std"] }
objc2-core-foundation = { path = "../../framework-crates/objc2-core-foundation", default-features = false, features = ["std"] }

# To make CI work
[target.'cfg(not(target_vendor = "apple"))'.dependencies]
Expand Down
17 changes: 17 additions & 0 deletions crates/test-ui/ui/cfstring_from_static_str.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//! Test that a `'static` str used in `CFString` can't later be modified.
use objc2_core_foundation::CFString;

fn main() {
// Create a `&'static mut str`.
let s = String::from("xyz").leak();

// Construct a `CFString` from it.
let cf = CFString::from_static_str(s);
assert_eq!(cf.to_string(), "xyz");

// Modify the string.
s.make_ascii_uppercase();

// This would be invalid, since CFString is expected to be immutable.
assert_eq!(cf.to_string(), "XYZ");
}
11 changes: 11 additions & 0 deletions crates/test-ui/ui/cfstring_from_static_str.stderr

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 10 additions & 2 deletions framework-crates/objc2-core-foundation/src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,16 @@ unsafe impl Type for CFType {}

impl fmt::Debug for CFType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
// TODO: Use `CFCopyDescription` here
f.debug_struct("CFType").finish_non_exhaustive()
#[cfg(feature = "CFString")]
{
let desc = crate::CFCopyDescription(Some(self)).expect("must have description");
write!(f, "{desc}")
}
#[cfg(not(feature = "CFString"))]
{
f.debug_struct("<CoreFoundation type>")
.finish_non_exhaustive()
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions framework-crates/objc2-core-foundation/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ mod generated;
#[cfg(feature = "CFCGTypes")]
mod geometry;
mod retained;
#[cfg(all(feature = "CFBase", feature = "CFString"))]
mod string;
mod type_traits;

#[cfg(feature = "CFBase")]
Expand Down
Loading

0 comments on commit c76799e

Please sign in to comment.