Fix potential out of bounds write in the JSON writer

If a small string (1 or 2 chars) would require a JSON escape sequence
when writing out the string, the code could write out of bounds of the
byte array. Fix that by always allocating at least 16 bytes of space.

Change-Id: I4d023e7ed837b25b0a5dcf6cfaaf94aa55695b9f
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
bb10
Lars Knoll 2019-11-15 12:28:56 +01:00
parent 37e054993b
commit 3d9bae304c
1 changed files with 2 additions and 1 deletions

View File

@ -60,7 +60,8 @@ static inline uchar hexdig(uint u)
static QByteArray escapedString(const QString &s)
{
QByteArray ba(s.length(), Qt::Uninitialized);
// give it a minimum size to ensure the resize() below always adds enough space
QByteArray ba(qMax(s.length(), 16), Qt::Uninitialized);
uchar *cursor = reinterpret_cast<uchar *>(const_cast<char *>(ba.constData()));
const uchar *ba_end = cursor + ba.length();