From e0e96d263039a5cf0e085a361eccf426db2f9d21 Mon Sep 17 00:00:00 2001 From: Alejandro Colomar Date: Fri, 15 Dec 2023 14:52:58 +0100 Subject: string_copying.7: stpecpy(), strtcpy(): Set errno on failure Signed-off-by: Alejandro Colomar --- man7/string_copying.7 | 40 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/man7/string_copying.7 b/man7/string_copying.7 index 02e379a7d..cc8904493 100644 --- a/man7/string_copying.7 +++ b/man7/string_copying.7 @@ -505,6 +505,29 @@ The .I dst pointer, which is useless. +.\" ----- ERRORS ------------------------------------------------------/ +.SH ERRORS +Most of these functions don't set +.IR errno . +.TP +.BR stpecpy () +.RS +.TP +.B E2BIG +The string has been truncated. +.RE +.TP +.BR strtcpy () +.RS +.TP +.B ENOBUFS +.I dsize +was +.BR 0 . +.TP +.B E2BIG +The string has been truncated. +.RE .\" ----- NOTES :: strscpy(9) -----------------------------------------/ .SH NOTES The Linux kernel has an internal function for copying strings, @@ -513,7 +536,9 @@ which is identical to .BR strtcpy (), except that it returns .B \-E2BIG -instead of \-1. +instead of \-1 +and it doesn't set +.IR errno . .\" ----- CAVEATS :: --------------------------------------------------/ .SH CAVEATS Don't mix chain calls to truncating and non-truncating functions. @@ -704,6 +729,7 @@ char * .IR stpecpy "(char *dst, char end[0], const char *restrict src)" { bool trunc; + char *p; size_t dsize, dlen, slen; \& if (dst == end) @@ -716,7 +742,10 @@ char * trunc = (slen == dsize); dlen = slen \- trunc; \& - return stpcpy(mempcpy(dst, src, dlen), "") + trunc; + p = stpcpy(mempcpy(dst, src, dlen), ""); + if (trunc) + errno = E2BIG; + return p + trunc; } \& .\" ----- EXAMPLES :: Implementations :: strtcpy() --------------------/ @@ -726,15 +755,18 @@ ssize_t bool trunc; size_t dlen, slen; \& - if (dsize == 0) + if (dsize == 0) { + errno = ENOBUFS; return \-1; + } \& slen = strnlen(src, dsize); trunc = (slen == dsize); dlen = slen \- trunc; \& stpcpy(mempcpy(dst, src, dlen), ""); - + if (trunc) + errno = E2BIG; return trunc ? \-1 : slen; } .\" ----- SEE ALSO :: -------------------------------------------------/ -- cgit v1.2.3