lib/string: Add strscpy_pad() function
authorTobin C. Harding <tobin@kernel.org>
Fri, 5 Apr 2019 01:58:58 +0000 (12:58 +1100)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Tue, 23 Feb 2021 13:00:30 +0000 (14:00 +0100)
commitfe072e50be403fc52c426c15073b8c38ce301416
tree732e87200c49ba14db91fb549589c3b5a8f2152d
parent8019d7074d061d6511ca33fcb40f0b1a67ab4279
lib/string: Add strscpy_pad() function

[ Upstream commit 458a3bf82df4fe1f951d0f52b1e0c1e9d5a88a3b ]

We have a function to copy strings safely and we have a function to copy
strings and zero the tail of the destination (if source string is
shorter than destination buffer) but we do not have a function to do
both at once.  This means developers must write this themselves if they
desire this functionality.  This is a chore, and also leaves us open to
off by one errors unnecessarily.

Add a function that calls strscpy() then memset()s the tail to zero if
the source string is shorter than the destination buffer.

Acked-by: Kees Cook <keescook@chromium.org>
Signed-off-by: Tobin C. Harding <tobin@kernel.org>
Signed-off-by: Shuah Khan <shuah@kernel.org>
Signed-off-by: Sasha Levin <sashal@kernel.org>
include/linux/string.h
lib/string.c