{"record":{"id":"42f0d3d3b4d7388f","repo":"nautechsystems/nautilus_trader","slug":"invalid-utf-8-in-c-string","errorCode":null,"errorMessage":"Invalid UTF-8 in C string","messagePattern":"Invalid UTF-8 in C string","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/core/src/string/stack_str.rs","lineNumber":206,"sourceCode":"    ///\n    /// # Safety\n    ///\n    /// - `ptr` must be a valid, non-null pointer to a null-terminated C string.\n    /// - The string must contain only valid ASCII (no interior NUL bytes).\n    /// - The string must not exceed 36 characters.\n    ///\n    /// Violating these requirements causes a panic. If this function is called\n    /// from C code, such a panic is undefined behavior.\n    ///\n    /// # Panics\n    ///\n    /// Panics if the C string contains invalid UTF-8 or violates any of the\n    /// safety invariants listed above.\n    #[must_use]\n    pub unsafe fn from_c_ptr(ptr: *const c_char) -> Self {\n        // SAFETY: Caller guarantees ptr is valid and null-terminated\n        let cstr = unsafe { CStr::from_ptr(ptr) };\n        let s = cstr.to_str().expect(\"Invalid UTF-8 in C string\");\n        Self::new(s)\n    }\n\n    /// Creates a [`StackStr`] from a C string pointer with validation.\n    ///\n    /// Returns `None` if the string is null or invalid. This is safe to call from C\n    /// code for null and string-validation failures because it does not panic.\n    ///\n    /// # Safety\n    ///\n    /// - `ptr` must be null or a valid pointer to a null-terminated C string.\n    #[must_use]\n    pub unsafe fn from_c_ptr_checked(ptr: *const c_char) -> Option<Self> {\n        if ptr.is_null() {\n            return None;\n        }\n\n        // SAFETY: Caller guarantees ptr is valid and null-terminated","sourceCodeStart":188,"sourceCodeEnd":224,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/core/src/string/stack_str.rs#L188-L224","documentation":"StackStr::from_c_ptr in crates/core/src/string/stack_str.rs:206 constructs a fixed-capacity stack string from a C string pointer, converting the CStr to &str with to_str().expect(\"Invalid UTF-8 in C string\"). It panics when the pointed-to bytes are not valid UTF-8; the doc comment also warns that violating the safety invariants (valid, null-terminated pointer) is undefined behavior, so callers must uphold those before this expect is even reached.","triggerScenarios":"Calling unsafe StackStr::from_c_ptr with a pointer to non-UTF-8 bytes: legacy-encoded text, binary data mistaken for a string, or a multi-byte character truncated at the fixed capacity boundary.","commonSituations":"Fixed-size char buffers from C code carrying locale-encoded text; protocol frames where a length prefix was misread so the slice lands mid-character; symbol or label fields populated from non-UTF-8 vendor data.","solutions":["Ensure the C producer writes UTF-8 and null-terminates the buffer (the invariants this unsafe function requires).","Prefer the safe validated variant (from_c_ptr's documented safe counterpart returning Option) when input trustworthiness is uncertain.","Truncate on a char boundary: trim the source string with floor_char_boundary-style logic before copying into the fixed buffer.","Re-encode non-UTF-8 sources explicitly (decode with the true encoding, encode UTF-8 with replacement) before crossing the boundary."],"exampleFix":"// before (C producer)\nstrcpy(buf, \"caf\\xe9\");              // Latin-1 bytes into shared buffer\n// after\nconst char *s = \"caf\\xc3\\xa9\";       // UTF-8 bytes\nstrncpy(buf, s, buf_len - 1); buf[buf_len - 1] = '\\0';","handlingStrategy":"type-guard","validationCode":"// Caller-side check before the unsafe call\nlet bytes = CStr::from_ptr(ptr).to_bytes();\nassert!(std::str::from_utf8(bytes).is_ok(), \"non-UTF-8 would panic from_c_ptr\");","typeGuard":"fn stack_str_input_ok(ptr: *const c_char) -> bool {\n    if ptr.is_null() { return false; }\n    unsafe { std::str::from_utf8(CStr::from_ptr(ptr).to_bytes()).is_ok() }\n}","tryCatchPattern":null,"preventionTips":["Prefer the safe validated variant (returns Option) whenever the pointer provenance is uncertain.","Guarantee producers write UTF-8 and null-terminate shared buffers.","Truncate on char boundaries when filling fixed-capacity buffers.","Keep unsafe from_c_ptr calls adjacent to their documented safety comments."],"tags":["ffi","utf-8","unsafe","panic"],"backgroundTag":"invalid-argument-format","analyzedSha":"18893faf8b356be3320add8de2f861b0b647cf06","analyzedAt":"2026-09-08T20:49:34.690Z","contentChangedAt":"2026-09-08T20:49:34.690Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}