diff --git a/lib/strlcpy.c b/lib/strlcpy.c index ffb83e0c..58773b71 100644 --- a/lib/strlcpy.c +++ b/lib/strlcpy.c @@ -14,5 +14,5 @@ #include "strlcpy.h" -extern inline ssize_t strlcpy_(char *restrict dst, const char *restrict src, - size_t size); +extern inline ssize_t strtcpy(char *restrict dst, const char *restrict src, + size_t dsize); diff --git a/lib/strlcpy.h b/lib/strlcpy.h index c44819c6..36d847c4 100644 --- a/lib/strlcpy.h +++ b/lib/strlcpy.h @@ -10,6 +10,7 @@ #include +#include #include #include #include @@ -43,21 +44,31 @@ */ -#define STRLCPY(dst, src) strlcpy_(dst, src, SIZEOF_ARRAY(dst)) +#define STRLCPY(dst, src) strtcpy(dst, src, SIZEOF_ARRAY(dst)) -inline ssize_t strlcpy_(char *restrict dst, const char *restrict src, - size_t size); +inline ssize_t strtcpy(char *restrict dst, const char *restrict src, + size_t dsize); inline ssize_t -strlcpy_(char *restrict dst, const char *restrict src, size_t size) +strtcpy(char *restrict dst, const char *restrict src, size_t dsize) { - size_t len; + bool trunc; + char *p; + size_t dlen, slen; - len = strlcpy(dst, src, size); + if (dsize == 0) + return -1; - return (len >= size) ? -1 : len; + slen = strnlen(src, dsize); + trunc = (slen == dsize); + dlen = slen - trunc; + + p = mempcpy(dst, src, dlen); + *p = '\0'; + + return trunc ? -1 : slen; } diff --git a/tests/unit/Makefile.am b/tests/unit/Makefile.am index e209f041..46794d66 100644 --- a/tests/unit/Makefile.am +++ b/tests/unit/Makefile.am @@ -38,13 +38,11 @@ test_strlcpy_SOURCES = \ $(NULL) test_strlcpy_CFLAGS = \ $(AM_FLAGS) \ - $(LIBBSD_CFLAGS) \ $(NULL) test_strlcpy_LDFLAGS = \ $(NULL) test_strlcpy_LDADD = \ $(CMOCKA_LIBS) \ - $(LIBBSD_LIBS) \ $(NULL) test_xasprintf_SOURCES = \