diff options
author | Marc Mutz <marc.mutz@qt.io> | 2023-02-16 22:20:11 +0100 |
---|---|---|
committer | Marc Mutz <marc.mutz@qt.io> | 2023-03-10 20:55:17 +0000 |
commit | f030037d24ed08b3632264b0c6791b311d4b8603 (patch) | |
tree | 53e58ac0cd20b7af98ef1ac5bdd3b45bb53131d0 /src/corelib/text | |
parent | c0a732f984f407f7613805ff04b8a6d46c12f754 (diff) |
Implement qstrncpy() in terms of std::strncat()
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 <thiago.macieira@intel.com>
Reviewed-by: MÃ¥rten Nordheim <marten.nordheim@qt.io>
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Diffstat (limited to 'src/corelib/text')
-rw-r--r-- | src/corelib/text/qbytearray.cpp | 13 |
1 files changed, 3 insertions, 10 deletions
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; } |