{"record":{"id":"7b46de2191c606dd","repo":"uutils/coreutils","slug":"os-string-cannot-be-converted-to-bytes","errorCode":null,"errorMessage":"OS string cannot be converted to bytes","messagePattern":"OS string cannot be converted to bytes","errorType":"exception","errorClass":"io::Error","httpStatus":null,"severity":"error","filePath":"src/uucore/src/lib/mods/display.rs","lineNumber":93,"sourceCode":"        {\n            self.write_all(buf.as_encoded_bytes())\n        }\n\n        #[cfg(not(any(unix, target_os = \"wasi\")))]\n        {\n            // It's possible to write a better OsWrite impl for Windows consoles (e.g. Stdout)\n            // as those are fundamentally 16-bit. If the OS string is invalid then it can be\n            // encoded to 16-bit and written using raw windows_sys calls. But this is quite involved\n            // (see `sys/pal/windows/stdio.rs` in the stdlib) and the value-add is small.\n            //\n            // There's no way to write invalid OS strings to Windows files, as those are 8-bit.\n\n            match buf.to_str() {\n                Some(text) => self.write_all(text.as_bytes()),\n                // We could output replacement characters instead, but the\n                // stdlib errors when sending invalid UTF-8 to the console,\n                // so let's follow that.\n                None => Err(io::Error::new(\n                    io::ErrorKind::InvalidData,\n                    \"OS string cannot be converted to bytes\",\n                )),\n            }\n        }\n    }\n}\n\n// We do not have a blanket impl for all Write because a smarter Windows impl should\n// be able to make use of AsRawHandle. Please keep this in mind when adding new impls.\nimpl OsWrite for File {}\nimpl OsWrite for Stdout {}\nimpl OsWrite for StdoutLock<'_> {}\n// A future smarter Windows implementation can first flush the BufWriter before\n// doing a raw write.\nimpl<W: OsWrite> OsWrite for BufWriter<W> {}\n\nimpl OsWrite for Box<dyn OsWrite> {","sourceCodeStart":75,"sourceCodeEnd":111,"githubUrl":"https://github.com/uutils/coreutils/blob/0b77ced48502a5f80c51dfa827af7e69f3687053/src/uucore/src/lib/mods/display.rs#L75-L111","documentation":"Error \"OS string cannot be converted to bytes\" thrown in uutils/coreutils.","triggerScenarios":"Occurs when converting an OS string to bytes on a platform where that conversion is unsupported.","commonSituations":"Displaying non-UTF-8 file names on platforms without OsStr byte access.","solutions":["Use Unix-style OsStrExt bytes APIs on Unix, or convert via to_string_lossy when exact bytes are not required.","Run on a platform whose OS strings support byte conversion."],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"0b77ced48502a5f80c51dfa827af7e69f3687053","analyzedAt":"2026-08-19T21:43:27.871Z","contentChangedAt":"2026-08-19T21:43:27.871Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}