diff options
author | Marc Mutz <marc.mutz@qt.io> | 2021-12-16 09:31:39 +0100 |
---|---|---|
committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2022-03-16 19:12:56 +0000 |
commit | 6596800453328a53efc6bd8a2e728493161fb649 (patch) | |
tree | 9f2aaa9249c9b55635136fe565f70317642c094f | |
parent | ba395fcd94201a6804e56986e56d4433eeeaa871 (diff) | |
download | qtbase-6596800453328a53efc6bd8a2e728493161fb649.tar.gz |
tst_QIODevice: fix UB (precondition violation) in SequentialReadBuffer::readData()
memcpy() mustn't be called with a nullptr, even if the size is zero.
Fixes ubsan error:
tst_qiodevice.cpp:561:15: runtime error: null pointer passed as argument 1, which is declared to never be null
Even though ubsan only complained about one of them, fix all three
occurrences of the pattern in the test.
Change-Id: I5c06ab4a20a9e9f8831392c46c6969c05248fdac
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Reviewed-by: MÃ¥rten Nordheim <marten.nordheim@qt.io>
(cherry picked from commit 572b55baa42787447643169811784bc64024fb5e)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
-rw-r--r-- | tests/auto/corelib/io/qiodevice/tst_qiodevice.cpp | 9 |
1 files changed, 6 insertions, 3 deletions
diff --git a/tests/auto/corelib/io/qiodevice/tst_qiodevice.cpp b/tests/auto/corelib/io/qiodevice/tst_qiodevice.cpp index da5327594c..4a04e0f7c6 100644 --- a/tests/auto/corelib/io/qiodevice/tst_qiodevice.cpp +++ b/tests/auto/corelib/io/qiodevice/tst_qiodevice.cpp @@ -561,7 +561,8 @@ protected: qint64 readData(char *data, qint64 maxSize) override { maxSize = qMin(maxSize, qint64(buf->size() - offset)); - memcpy(data, buf->constData() + offset, maxSize); + if (maxSize > 0) + memcpy(data, buf->constData() + offset, maxSize); offset += maxSize; return maxSize; } @@ -604,13 +605,15 @@ protected: qint64 readData(char *data, qint64 maxSize) override { maxSize = qMin(maxSize, qint64(buf.size() - pos())); - memcpy(data, buf.constData() + pos(), maxSize); + if (maxSize > 0) + memcpy(data, buf.constData() + pos(), maxSize); return maxSize; } qint64 writeData(const char *data, qint64 maxSize) override { maxSize = qMin(maxSize, qint64(buf.size() - pos())); - memcpy(buf.data() + pos(), data, maxSize); + if (maxSize > 0) + memcpy(buf.data() + pos(), data, maxSize); return maxSize; } |