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 <edward.welbourne@qt.io> Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>bb10
parent
816a623876
commit
312d08b290
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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());
|
||||
|
|
|
|||
Loading…
Reference in New Issue