From 8003ae79bfd72736b809805afbb7984c3431031e Mon Sep 17 00:00:00 2001 From: Sona Kurazyan Date: Fri, 31 Jul 2020 11:51:23 +0200 Subject: Document the changed semantics when passing arrays to QByteArray methods After adding QByteArrayView overloads of QByteArray methods that used to take char*, the way of determining the length of passed fixed-sized array data has changed, due to the QByteArraiyView's optimization to deduce the length at compile time for arrays. Document the behavior and add a changelog item. [ChangeLog][QtCore][QByteArray] When passing fixed size C arrays to QByteArray methods, the length of the data on which the method will operate is determined by array size, and not by scanning for the first '\0' terminator, as it was in Qt 5. Task-number: QTBUG-85815 Change-Id: I11cba17d428791e06c9bd4c7a727b7bd6b6aec3c Reviewed-by: Paul Wicking Reviewed-by: Edward Welbourne Reviewed-by: Lars Knoll --- src/corelib/text/qbytearray.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'src/corelib/text/qbytearray.cpp') diff --git a/src/corelib/text/qbytearray.cpp b/src/corelib/text/qbytearray.cpp index aee22edc6f..b5327280f9 100644 --- a/src/corelib/text/qbytearray.cpp +++ b/src/corelib/text/qbytearray.cpp @@ -847,6 +847,14 @@ QByteArray qUncompress(const uchar* data, int nbytes) such a pointer, without a length, will interpret it as this sequence of bytes. Such a sequence, by construction, cannot contain a '\\0' byte. + Take care when passing fixed size C arrays to QByteArray methods that accept + a QByteArrayView: the length of the data on which the method will operate is + determined by array size. A \c{char [N]} array will be handled as a view of + size \c{N-1}, on the expectation that the array is a string literal with a '\\0' + at index \c{N-1}. For example: + + \snippet code/src_corelib_text_qbytearray.cpp 54 + Other overloads accept a start-pointer and a byte-count; these use the given number of bytes, following the start address, regardless of whether any of them happen to be '\\0' bytes. In some cases, where there is no overload -- cgit v1.2.3