From 5291dc7dcfa489354d3f880468902debf755d9cd Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Mon, 7 Mar 2022 16:33:35 +0100 Subject: [PATCH] Avoid possible allocation by amending a condition MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit QBA::toPercentEncoding() duplicated a condition to decide whether it should include a percent-replacement character in its always-encode list (which might allocate memory), where simply pretesting each character against the percent achieves the same effect more cheaply. Change-Id: Ic87a977713207d74b07ee6b811b0ead3b6945ef6 Reviewed-by: Thiago Macieira Reviewed-by: MÃ¥rten Nordheim --- src/corelib/text/qbytearray.cpp | 31 ++++++++++--------------------- 1 file changed, 10 insertions(+), 21 deletions(-) diff --git a/src/corelib/text/qbytearray.cpp b/src/corelib/text/qbytearray.cpp index 95d0ac5516..b0f0590410 100644 --- a/src/corelib/text/qbytearray.cpp +++ b/src/corelib/text/qbytearray.cpp @@ -4593,14 +4593,15 @@ static void q_toPercentEncoding(QByteArray *ba, const char *dontEncode, const ch for (qsizetype i = 0; i < len; ++i) { unsigned char c = *inputData++; - if (((c >= 0x61 && c <= 0x7A) // ALPHA - || (c >= 0x41 && c <= 0x5A) // ALPHA - || (c >= 0x30 && c <= 0x39) // DIGIT - || c == 0x2D // - - || c == 0x2E // . - || c == 0x5F // _ - || c == 0x7E // ~ - || q_strchr(dontEncode, c)) + if (c != percent + && ((c >= 0x61 && c <= 0x7A) // ALPHA + || (c >= 0x41 && c <= 0x5A) // ALPHA + || (c >= 0x30 && c <= 0x39) // DIGIT + || c == 0x2D // - + || c == 0x2E // . + || c == 0x5F // _ + || c == 0x7E // ~ + || q_strchr(dontEncode, c)) && !q_strchr(alsoEncode, c)) { if (output) output[length] = c; @@ -4652,20 +4653,8 @@ QByteArray QByteArray::toPercentEncoding(const QByteArray &exclude, const QByteA if (isEmpty()) return QByteArray(data(), 0); - QByteArray include2 = include; - if (exclude.contains(percent) - || ((percent >= 0x61 && percent <= 0x7A) // ALPHA - || (percent >= 0x41 && percent <= 0x5A) // ALPHA - || (percent >= 0x30 && percent <= 0x39) // DIGIT - || percent == 0x2D // - - || percent == 0x2E // . - || percent == 0x5F // _ - || percent == 0x7E)) { // ~ - include2 += percent; - } - QByteArray result = *this; - q_toPercentEncoding(&result, exclude.nulTerminated().constData(), include2.nulTerminated().constData(), percent); + q_toPercentEncoding(&result, exclude.nulTerminated().constData(), include.nulTerminated().constData(), percent); return result; }