lib/string.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/string.c b/lib/string.c index 76327b51e36f..a2a678e45389 100644 --- a/lib/string.c +++ b/lib/string.c @@ -137,7 +137,7 @@ ssize_t sized_strscpy(char *dest, const char *src, size_t count) if (IS_ENABLED(CONFIG_KMSAN)) max = 0; - while (max >= sizeof(unsigned long)) { + while (max > sizeof(unsigned long)) { unsigned long c, data; c = read_word_at_a_time(src+res); @@ -153,10 +153,10 @@ ssize_t sized_strscpy(char *dest, const char *src, size_t count) max -= sizeof(unsigned long); } - while (count) { + while (count > 0) { char c; - c = src[res]; + c = READ_ONCE(src[res]); dest[res] = c; if (!c) return res; @@ -164,11 +164,11 @@ ssize_t sized_strscpy(char *dest, const char *src, size_t count) count--; } - /* Hit buffer length without finding a NUL; force NUL-termination. */ - if (res) - dest[res-1] = '\0'; + /* Final byte - force NUL termination */ + dest[res] = 0; - return -E2BIG; + /* Return -E2BIG if the source continued.. */ + return READ_ONCE(src[res]) ? -E2BIG : res; } EXPORT_SYMBOL(sized_strscpy);