{"record":{"id":"d6faa2bc1001ebc9","repo":"sxyazi/yazi","slug":"allocation-failed-while-creating-cfstring","errorCode":null,"errorMessage":"Allocation failed while creating CFString","messagePattern":"Allocation failed while creating CFString","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"yazi-ffi/src/cf_string.rs","lineNumber":22,"sourceCode":"use core_foundation_sys::{base::{CFRelease, kCFAllocatorDefault, kCFAllocatorNull}, string::{CFStringCreateWithBytesNoCopy, CFStringGetCString, CFStringGetLength, CFStringGetMaximumSizeForEncoding, CFStringRef, kCFStringEncodingUTF8}};\nuse libc::strlen;\n\npub struct CFString(pub(super) CFStringRef);\n\nimpl CFString {\n\tpub fn new(s: &str) -> Result<Self> {\n\t\tlet key = unsafe {\n\t\t\tCFStringCreateWithBytesNoCopy(\n\t\t\t\tkCFAllocatorDefault,\n\t\t\t\ts.as_ptr(),\n\t\t\t\ts.len() as _,\n\t\t\t\tkCFStringEncodingUTF8,\n\t\t\t\tfalse as _,\n\t\t\t\tkCFAllocatorNull,\n\t\t\t)\n\t\t};\n\t\tif key.is_null() {\n\t\t\tbail!(\"Allocation failed while creating CFString\");\n\t\t}\n\t\tOk(Self(key))\n\t}\n\n\tfn len(&self) -> usize { unsafe { CFStringGetLength(self.0) as _ } }\n\n\tpub(crate) fn os_string(&self) -> Result<OsString> {\n\t\tlet len = self.len();\n\t\tlet capacity =\n\t\t\tunsafe { CFStringGetMaximumSizeForEncoding(len as _, kCFStringEncodingUTF8) } + 1;\n\n\t\tlet mut out: Vec<u8> = Vec::with_capacity(capacity as usize);\n\t\tlet result = unsafe {\n\t\t\tCFStringGetCString(self.0, out.as_mut_ptr().cast(), capacity, kCFStringEncodingUTF8)\n\t\t};\n\t\tif result == 0 {\n\t\t\tbail!(\"Failed to get the C string from CFString\");\n\t\t}","sourceCodeStart":4,"sourceCodeEnd":40,"githubUrl":"https://github.com/sxyazi/yazi/blob/5f901b886b14de1f17460b6e52e9de5d67f8aba9/yazi-ffi/src/cf_string.rs#L4-L40","documentation":"CFString::new converts a Rust &str into a Core Foundation CFString via CFStringCreateWithCString; the bails fires when that allocation returns NULL. This is an out-of-memory / allocation failure inside Core Foundation, not an encoding problem. The wrapper has no string to hand back, so construction fails.","triggerScenarios":"Calling CFString::new(\"...\") (e.g. from CFDict::value to build lookup keys) when CFStringCreateWithCString returns NULL — practically only when the process is out of memory or the allocator is exhausted.","commonSituations":"System under severe memory pressure; huge or malformed allocations elsewhere exhausting the heap; extremely long key strings combined with low memory.","solutions":["Free memory / reduce concurrent allocations and retry the operation.","Check the length of the string being converted; avoid converting pathologically large buffers.","If reproducible, investigate leaks in the CF usage (CFString/CFDictionary objects are not auto-released)."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"match CFString::new(key) {\n    Ok(s) => /* proceed */,\n    Err(e) => { tracing::error!(\"CFString alloc failed: {e}\"); /* degrade gracefully */ }\n}","preventionTips":["Avoid extremely long strings for CF keys/values","Audit CF usage for leaks (CFString/CFDictionary are manual retain/release)","Monitor memory pressure in long-running sessions"],"tags":["macos","core-foundation","out-of-memory","ffi"],"backgroundTag":"allocation-failed","analyzedSha":"5f901b886b14de1f17460b6e52e9de5d67f8aba9","analyzedAt":"2026-09-02T18:38:25.566Z","contentChangedAt":"2026-09-02T18:38:25.566Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}