path: root/tests/auto/corelib/global/qgetputenv/tst_qgetputenv.cpp
diff options
authorMarc Mutz <>2022-08-03 18:52:13 +0200
committerMarc Mutz <>2022-08-05 19:42:05 +0200
commit15422d191fb03eb9cafe68b24484d59c1270244c (patch)
treea3fb5bf2647c67652f1e986fd3f381feb2b0beb0 /tests/auto/corelib/global/qgetputenv/tst_qgetputenv.cpp
parent6a5cadb9557ebac00b8ddacf623dc38baa716680 (diff)
qputenv: defend against non-NUL-terminated QByteArray values
The old code assumed that a QByteArray's data() is always NUL-terminated. Due to the conflation of owners and non-owners in QByteArray (but also in case we ever get efficient substringing), this is not always the case, e.g. QByteArray::fromRawData() does not ensure NUL-termination. From QString::utf16(), we learn that the condition to check for is QArrayData::isMutable(). After working around the fact that QByteArray::data_ptr() doesn't exist for const QBAs and that empty QBAs always refer to QByteArray::empty_, which is !isMutable(), we can detect this situation and re-allocate without introducing new API. This is the fix for Qt ≤ 6.4. For Qt 6.5, we'll port the function to QByteArrayView. Pick-to: 6.4 6.3 6.2 5.15 Fixes: QTBUG-105302 Change-Id: I3416535ab09d601e0e87b2767f2c024ba1217e64 Reviewed-by: Sona Kurazyan <> Reviewed-by: Mårten Nordheim <>
Diffstat (limited to 'tests/auto/corelib/global/qgetputenv/tst_qgetputenv.cpp')
1 files changed, 5 insertions, 1 deletions
diff --git a/tests/auto/corelib/global/qgetputenv/tst_qgetputenv.cpp b/tests/auto/corelib/global/qgetputenv/tst_qgetputenv.cpp
index 77a7fa4194..f914ee5f23 100644
--- a/tests/auto/corelib/global/qgetputenv/tst_qgetputenv.cpp
+++ b/tests/auto/corelib/global/qgetputenv/tst_qgetputenv.cpp
@@ -60,7 +60,11 @@ void tst_QGetPutEnv::getSetCheck()
QCOMPARE(sresult, QString());
- QVERIFY(qputenv(varName, QByteArray("supervalue")));
+ constexpr char varValueFullString[] = "supervalue123";
+ const auto varValueQBA = QByteArray::fromRawData(varValueFullString, sizeof varValueFullString - 4);
+ QCOMPARE_EQ(varValueQBA, "supervalue");
+ QVERIFY(qputenv(varName, varValueQBA));