On Mon, Oct 14, 2024 at 02:13:14PM -0700, Justin Stitt wrote: > strncpy() is deprecated for use on NUL-terminated destination strings [1] and > as such we should prefer more robust and less ambiguous string interfaces. > > String copy operations involving manual pointer offset and length > calculations followed by explicit NUL-byte assignments are best changed > to either strscpy or memcpy. > > strscpy is not a drop-in replacement as @len would need a one subtracted > from it to avoid truncating the source string. > > To not sabotage readability of the current code, use memcpy (retaining > the manual NUL assignment) as this unambiguously describes the desired > behavior.
We know the destination must have a NUL-terminated string. Is the src NUL terminated? Looking at parse_pred(), it seems like no? And we can't use memtostr_pad() here because the source buffer size isn't known at compile time. Okay then. And there are no NUL bytes in the "str + s" span, so yeah, it looks like memcpy() is best. Reviewed-by: Kees Cook <[email protected]> -- Kees Cook
