diff --git a/src/common/encoding.cpp b/src/common/encoding.cpp index c5579562..1df51768 100644 --- a/src/common/encoding.cpp +++ b/src/common/encoding.cpp @@ -744,13 +744,15 @@ const QString detectCharset (const QByteArray& byteArray) { qCDebug(appLog) << "Entering detectCharset"; const char* text = byteArray.constData(); - uint8_t c = *text; + const qsizetype size = byteArray.size(); + int pos = 0; + uint8_t c = 0; std::string charset; if (validateUTF8 (byteArray)) { qCDebug(appLog) << "validateUTF8 is true"; - while ((c = *text++) != '\0') + while (pos < size && (c = static_cast(text[pos++])) != '\0') { if (c > 0x7F) { @@ -761,11 +763,15 @@ const QString detectCharset (const QByteArray& byteArray) if (c == 0x1B) /* ESC */ { qCDebug(appLog) << "c == 0x1B"; - c = *text++; + if (pos >= size) + break; + c = static_cast(text[pos++]); if (c == '$') { qCDebug(appLog) << "c == '$'"; - c = *text++; + if (pos >= size) + break; + c = static_cast(text[pos++]); switch (c) { case 'B': // JIS X 0208-1983 @@ -778,7 +784,9 @@ const QString detectCharset (const QByteArray& byteArray) charset = "ISO-2022-JP-2"; break; case '(': - c = *text++; + if (pos >= size) + break; + c = static_cast(text[pos++]); switch (c) { case 'C': // KSC5601-1987 @@ -788,7 +796,9 @@ const QString detectCharset (const QByteArray& byteArray) } break; case ')': - c = *text++; + if (pos >= size) + break; + c = static_cast(text[pos++]); if (c == 'C') { qCDebug(appLog) << "c == 'C'";