summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/corelib/tools/qbytearray.cpp7
-rw-r--r--tests/auto/corelib/tools/qbytearray/tst_qbytearray.cpp14
2 files changed, 15 insertions, 6 deletions
diff --git a/src/corelib/tools/qbytearray.cpp b/src/corelib/tools/qbytearray.cpp
index 00d15fd518..8bae505d76 100644
--- a/src/corelib/tools/qbytearray.cpp
+++ b/src/corelib/tools/qbytearray.cpp
@@ -208,13 +208,14 @@ char *qstrncpy(char *dst, const char *src, uint len)
{
if (!src || !dst)
return 0;
+ if (len > 0) {
#if defined(_MSC_VER) && _MSC_VER >= 1400
- strncpy_s(dst, len, src, len-1);
+ strncpy_s(dst, len, src, len - 1);
#else
- strncpy(dst, src, len);
+ strncpy(dst, src, len);
#endif
- if (len > 0)
dst[len-1] = '\0';
+ }
return dst;
}
diff --git a/tests/auto/corelib/tools/qbytearray/tst_qbytearray.cpp b/tests/auto/corelib/tools/qbytearray/tst_qbytearray.cpp
index f942eab800..6d1c7481a9 100644
--- a/tests/auto/corelib/tools/qbytearray/tst_qbytearray.cpp
+++ b/tests/auto/corelib/tools/qbytearray/tst_qbytearray.cpp
@@ -794,9 +794,17 @@ void tst_QByteArray::qstrncpy()
{
QByteArray src(1024, 'a'), dst(1024, 'b');
- // singularities
- QCOMPARE(::qstrncpy(0, 0,0), (char*)0);
- QCOMPARE(::qstrncpy(dst.data(), 0, 0), (char*)0);
+ // dst == nullptr
+ QCOMPARE(::qstrncpy(0, src.data(), 0), (char*)0);
+ QCOMPARE(::qstrncpy(0, src.data(), 10), (char*)0);
+
+ // src == nullptr
+ QCOMPARE(::qstrncpy(dst.data(), 0, 0), (char*)0);
+ QCOMPARE(::qstrncpy(dst.data(), 0, 10), (char*)0);
+
+ // valid pointers, but len == 0
+ QCOMPARE(::qstrncpy(dst.data(), src.data(), 0), dst.data());
+ QCOMPARE(*dst.data(), 'b'); // must not have written to dst
// normal copy
QCOMPARE(::qstrncpy(dst.data(), src.data(), src.size()), dst.data());