Skip to content
Tech News
← Back to articles

Linux Eliminates the Strncpy API After Six Years of Work, 360 Patches

read original get Linux Kernel Development Book → more articles
Why This Matters

The removal of the strncpy API from the Linux kernel marks a significant step towards improving code safety, performance, and reliability in the Linux ecosystem. This change addresses long-standing issues related to bugs and inefficiencies associated with the deprecated function, ultimately benefiting developers and users by promoting more robust coding practices.

Key Takeaways

Linux 7.2 has finally eliminated the strncpy API from the Linux kernel. The strncpy() function for copying up to a specified number of bytes has long been deprecated and after six years of work and hundreds of patches, no more users of the strncpy interface within the Linux kernel remained that it has now been eliminated.The strncpy function within the Linux kernel has been a "persistent source of bugs" for years due to counter-intuitive semantics and behavior around NUL termination along with performance issues due to redundant zero-filling of the destination. It took work over the last six years with around 362 commits to eliminate users of strncpy code within the kernel, but they are over the finish line for Linux 7.2.

This merge on Friday eliminated the strncpy API and the last per CPU architecture strncpy implementations.In place of strncpy, Linux kernel code should use strscpy() for NUL terminated destinations, strscpy_pad() for NUl-terminated destinations with zero-padding, strtomem_pad() for non-NUL-terminated fixed-width fields, memcpy_and_pad() for bounded copies with explicit padding, or memcpy() for known-length memory copies.