lib/string/: Fortify source of strtcpy(), stpecpy(), and zustr2stp()

By writing the terminating null byte via stpcpy(3), we take advantage of
_FORTIFY_SOURCE for the last byte, which was unprotected before this
commit.

Reported-by: Serge Hallyn <serge@hallyn.com>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
This commit is contained in:
Alejandro Colomar
2023-12-03 22:24:29 -06:00
committed by Serge Hallyn
parent 72060a2b2b
commit fc8389331e
3 changed files with 3 additions and 14 deletions
+1 -3
View File
@@ -55,7 +55,6 @@ inline ssize_t
strtcpy(char *restrict dst, const char *restrict src, size_t dsize)
{
bool trunc;
char *p;
size_t dlen, slen;
if (dsize == 0)
@@ -65,8 +64,7 @@ strtcpy(char *restrict dst, const char *restrict src, size_t dsize)
trunc = (slen == dsize);
dlen = slen - trunc;
p = mempcpy(dst, src, dlen);
*p = '\0';
stpcpy(mempcpy(dst, src, dlen), "");
return trunc ? -1 : slen;
}