From f030037d24ed08b3632264b0c6791b311d4b8603 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Thu, 16 Feb 2023 22:20:11 +0100 Subject: Implement qstrncpy() in terms of std::strncat() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This has the advantage that we're only copying strlen(src) characters, like in the strncpy_s() case, not fill all of [dst,len) with NULs, like strncpy() is wont to do. [ChangeLog][Important Behavior Changes][qstrncpy()] On non-Windows platforms, no longer writes to all bytes of the target buffer, but stops after the terminating NUL. This was already the behavior on Windows. Change-Id: Ic86206f418affae6d0d88dfe79537eeb833a7daa Reviewed-by: Thiago Macieira Reviewed-by: MÃ¥rten Nordheim Reviewed-by: Edward Welbourne --- src/corelib/text/qbytearray.cpp | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) (limited to 'src/corelib/text') diff --git a/src/corelib/text/qbytearray.cpp b/src/corelib/text/qbytearray.cpp index a1ad120520..1b21c98a2f 100644 --- a/src/corelib/text/qbytearray.cpp +++ b/src/corelib/text/qbytearray.cpp @@ -133,16 +133,9 @@ char *qstrcpy(char *dst, const char *src) char *qstrncpy(char *dst, const char *src, size_t len) { if (dst && len > 0) { - if (!src) { - *dst = '\0'; - return nullptr; - } -#ifdef Q_CC_MSVC - strncpy_s(dst, len, src, len - 1); -#else - strncpy(dst, src, len); -#endif - dst[len-1] = '\0'; + *dst = '\0'; + if (src) + std::strncat(dst, src, len - 1); } return src ? dst : nullptr; } -- cgit v1.2.3