sxyazi/yazi · error

Allocation failed while creating CFString

Error message

Allocation failed while creating CFString

What it means

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.

Source

Thrown at yazi-ffi/src/cf_string.rs:22

use core_foundation_sys::{base::{CFRelease, kCFAllocatorDefault, kCFAllocatorNull}, string::{CFStringCreateWithBytesNoCopy, CFStringGetCString, CFStringGetLength, CFStringGetMaximumSizeForEncoding, CFStringRef, kCFStringEncodingUTF8}};
use libc::strlen;

pub struct CFString(pub(super) CFStringRef);

impl CFString {
	pub fn new(s: &str) -> Result<Self> {
		let key = unsafe {
			CFStringCreateWithBytesNoCopy(
				kCFAllocatorDefault,
				s.as_ptr(),
				s.len() as _,
				kCFStringEncodingUTF8,
				false as _,
				kCFAllocatorNull,
			)
		};
		if key.is_null() {
			bail!("Allocation failed while creating CFString");
		}
		Ok(Self(key))
	}

	fn len(&self) -> usize { unsafe { CFStringGetLength(self.0) as _ } }

	pub(crate) fn os_string(&self) -> Result<OsString> {
		let len = self.len();
		let capacity =
			unsafe { CFStringGetMaximumSizeForEncoding(len as _, kCFStringEncodingUTF8) } + 1;

		let mut out: Vec<u8> = Vec::with_capacity(capacity as usize);
		let result = unsafe {
			CFStringGetCString(self.0, out.as_mut_ptr().cast(), capacity, kCFStringEncodingUTF8)
		};
		if result == 0 {
			bail!("Failed to get the C string from CFString");
		}

View on GitHub (pinned to 5f901b886b)

Solutions

  1. Free memory / reduce concurrent allocations and retry the operation.
  2. Check the length of the string being converted; avoid converting pathologically large buffers.
  3. If reproducible, investigate leaks in the CF usage (CFString/CFDictionary objects are not auto-released).
Defensive patterns

Strategy: try-catch

Try / catch

match CFString::new(key) {
    Ok(s) => /* proceed */,
    Err(e) => { tracing::error!("CFString alloc failed: {e}"); /* degrade gracefully */ }
}

Prevention

When it happens

Trigger: 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.

Common situations: System under severe memory pressure; huge or malformed allocations elsewhere exhausting the heap; extremely long key strings combined with low memory.

Related errors


AI-assisted analysis of sxyazi/yazi@5f901b886b (2026-09-02). Data as JSON: /api/errors/d6faa2bc1001ebc9. Report an issue: GitHub.