{"record":{"id":"0df7fb607bd2da5b","repo":"sxyazi/yazi","slug":"failed-to-get-the-c-string-from-cfstring","errorCode":null,"errorMessage":"Failed to get the C string from CFString","messagePattern":"Failed to get the C string from CFString","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"yazi-ffi/src/cf_string.rs","lineNumber":39,"sourceCode":"\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}\n\n\t\tunsafe { out.set_len(strlen(out.as_ptr().cast())) };\n\t\tout.shrink_to_fit();\n\t\tOk(OsString::from_vec(out))\n\t}\n}\n\nimpl Deref for CFString {\n\ttype Target = CFStringRef;\n\n\tfn deref(&self) -> &Self::Target { &self.0 }\n}\n\nimpl Drop for CFString {\n\tfn drop(&mut self) { unsafe { CFRelease(self.0 as _) }; }\n}\n","sourceCodeStart":21,"sourceCodeEnd":57,"githubUrl":"https://github.com/sxyazi/yazi/blob/5f901b886b14de1f17460b6e52e9de5d67f8aba9/yazi-ffi/src/cf_string.rs#L21-L57","documentation":"CFString::os_string extracts the string's bytes with CFStringGetCString using UTF-8; the bails fires when that function returns 0, meaning the conversion to a C string failed. The CFString content could not be represented (or the buffer was insufficient), so no OsString can be produced.","triggerScenarios":"Calling os_string() on a CFString whose contents are not valid UTF-8 (e.g. legacy MacRoman filenames) or whose length exceeds the computed capacity, causing CFStringGetCString to fail.","commonSituations":"Reading macOS metadata strings that contain legacy non-UTF-8 encodings from old files or Finder aliases; corrupted metadata blobs; strings with embedded NUL bytes.","solutions":["Fall back to CFStringGetCStringPtr or CFStringGetBytes with lossy conversion instead of strict UTF-8 C-string extraction.","Verify the capacity passed to CFStringGetCString accounts for the NUL terminator (len + 1).","Inspect the source data for non-UTF-8 or embedded-NUL content and sanitize it upstream."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// Before extraction you cannot easily validate encoding; guard by checking length fits the buffer\nlet capacity = (unsafe { CFStringGetLength(s.0) } + 1) as usize;\n// if capacity exceeds what os_string allocates, expect failure","typeGuard":null,"tryCatchPattern":"let name = cf_string.os_string().unwrap_or_else(|_| OsString::from(\"<unreadable>\"));","preventionTips":["Prefer CFStringGetBytes with lossy UTF-8 handling for untrusted strings","Always allocate length + 1 for the NUL terminator","Sanitize legacy-encoded metadata before wrapping in CFString"],"tags":["macos","core-foundation","encoding","utf-8","ffi"],"backgroundTag":"string-encoding-failed","analyzedSha":"5f901b886b14de1f17460b6e52e9de5d67f8aba9","analyzedAt":"2026-09-02T18:38:25.566Z","contentChangedAt":"2026-09-02T18:38:25.566Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}