Conceptual
Login

C Strings in Kernel Rust

Rust strings are a length plus bytes; C strings are bytes that stop at a zero. Since the kernel is C, anything you hand it as a name, such as a module name, a device name or a device-tree compatible string, has to be the C shape, so the kernel crate gives you a borrowed C string type, an owned allocated one, and a macro that builds a constant C string at compile time. There is also a byte-string type for text that is not guaranteed to be valid UTF-8. Mixing the two shapes up in C is a classic source of reading one byte past the end of a buffer; here the types simply will not fit together unless you convert.

Questions this Concept answers

  • Why must anything you hand to the kernel as a name be a C string rather than a Rust string?