From 932d5abe5ead509bbf1478149730397319a9e66f Mon Sep 17 00:00:00 2001 From: kettenis Date: Wed, 4 Jan 2023 15:48:00 +0000 Subject: [PATCH] Implement strscpy() and implement strscpy_pad() on top of that. Fixes the return value in case of truncation. ok jsg@ --- sys/dev/pci/drm/include/linux/string.h | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/sys/dev/pci/drm/include/linux/string.h b/sys/dev/pci/drm/include/linux/string.h index b19c465e2a0..f76ddf6f680 100644 --- a/sys/dev/pci/drm/include/linux/string.h +++ b/sys/dev/pci/drm/include/linux/string.h @@ -81,17 +81,21 @@ match_string(const char * const *array, size_t n, const char *str) return -EINVAL; } -/* returns chars written excluding NUL */ static inline ssize_t -strscpy_pad(char *dst, const char *src, size_t dstsize) +strscpy(char *dst, const char *src, size_t dstsize) { ssize_t r; - memset(dst, 0, dstsize); r = strlcpy(dst, src, dstsize); - /* truncation */ - if (r >= dstsize) - r = dstsize - 1; + if (dstsize == 0 || r >= dstsize) + return -E2BIG; return r; } +static inline ssize_t +strscpy_pad(char *dst, const char *src, size_t dstsize) +{ + memset(dst, 0, dstsize); + return strscpy(dst, src, dstsize); +} + #endif -- 2.20.1