From 312d08b290d079f5d72d9afc14a47606ebdde240 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Fri, 26 Aug 2016 10:03:05 +0200 Subject: qstrncpy: don't call strncpy_s with invalid parameters According to https://msdn.microsoft.com/en-us/library/5dae5d43.aspx, strncpy_s' second argument must not be 0: > If strDest or strSource is NULL, *or numberOfElements is 0*, the > invalid parameter handler is invoked. Move the existing check for len > 0 up to protect the strncpy_s call, too. Change-Id: I70d339ea60d4b76f3038b2e4e4756f6590a9bd31 Reviewed-by: Edward Welbourne Reviewed-by: Thiago Macieira --- tests/auto/corelib/tools/qbytearray/tst_qbytearray.cpp | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) (limited to 'tests/auto/corelib/tools/qbytearray/tst_qbytearray.cpp') 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()); -- cgit v1.2.3