From 4ccb000638a644d23b09167cc2922f61956c7d4a Mon Sep 17 00:00:00 2001 From: amirkiarafiei Date: Thu, 24 Sep 2026 13:29:18 +0300 Subject: [PATCH 1/3] Fix crash when reading non-UTF16 files --- src/ScintillaNext.cpp | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/ScintillaNext.cpp b/src/ScintillaNext.cpp index 7ed1b67a9..49b4ffd78 100644 --- a/src/ScintillaNext.cpp +++ b/src/ScintillaNext.cpp @@ -679,6 +679,7 @@ bool ScintillaNext::readFromDisk(QFile &file) QByteArray chunk; qint64 bytesRead; + QStringDecoder decoder; bool first_read = true; do { // Try to read as much as possible @@ -694,7 +695,6 @@ bool ScintillaNext::readFromDisk(QFile &file) // - determine space vs tabs // - determine indentation size - QStringDecoder decoder; int offset = 0; if (first_read) { first_read = false; @@ -726,10 +726,7 @@ bool ScintillaNext::readFromDisk(QFile &file) QByteArrayView input(chunk.constData() + offset, chunk.size() - offset); - QString text = decoder(input); - QByteArray utf8 = text.toUtf8(); - - if (bomType == BomType::Utf16BE ||bomType == BomType::Utf16LE) { + if (bomType == BomType::Utf16BE || bomType == BomType::Utf16LE) { QString text = decoder(input); QByteArray utf8 = text.toUtf8(); From 7abda88a740bf18cdc079a6a9c59ef473e30f5c2 Mon Sep 17 00:00:00 2001 From: amirkiarafiei Date: Thu, 24 Sep 2026 20:56:32 +0300 Subject: [PATCH 2/3] Use persistent QStringEncoder to preserve surrogate pairs across chunk boundaries --- src/ScintillaNext.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/ScintillaNext.cpp b/src/ScintillaNext.cpp index 49b4ffd78..e0b4dea81 100644 --- a/src/ScintillaNext.cpp +++ b/src/ScintillaNext.cpp @@ -680,6 +680,7 @@ bool ScintillaNext::readFromDisk(QFile &file) qint64 bytesRead; QStringDecoder decoder; + QStringEncoder encoder; bool first_read = true; do { // Try to read as much as possible @@ -705,11 +706,13 @@ bool ScintillaNext::readFromDisk(QFile &file) switch (bomType) { case BomType::Utf16BE: decoder = QStringDecoder(QStringDecoder::Utf16BE); + encoder = QStringEncoder(QStringEncoder::Utf8); offset = 2; break; case BomType::Utf16LE: decoder = QStringDecoder(QStringDecoder::Utf16LE); + encoder = QStringEncoder(QStringEncoder::Utf8); offset = 2; break; @@ -728,7 +731,7 @@ bool ScintillaNext::readFromDisk(QFile &file) if (bomType == BomType::Utf16BE || bomType == BomType::Utf16LE) { QString text = decoder(input); - QByteArray utf8 = text.toUtf8(); + QByteArray utf8 = encoder(text); appendText(utf8.size(), utf8.constData()); } else { From bea8d41c30c45adfefd924d8386ebceef079eedf Mon Sep 17 00:00:00 2001 From: dail8859 Date: Sat, 26 Sep 2026 11:02:18 -0400 Subject: [PATCH 3/3] Always instantiate QStringEncoder to UTF8 --- src/ScintillaNext.cpp | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/ScintillaNext.cpp b/src/ScintillaNext.cpp index e0b4dea81..b4daf09e4 100644 --- a/src/ScintillaNext.cpp +++ b/src/ScintillaNext.cpp @@ -678,9 +678,9 @@ bool ScintillaNext::readFromDisk(QFile &file) QByteArray chunk; qint64 bytesRead; - QStringDecoder decoder; - QStringEncoder encoder; + QStringEncoder encoder = QStringEncoder(QStringEncoder::Utf8); + bool first_read = true; do { // Try to read as much as possible @@ -706,23 +706,20 @@ bool ScintillaNext::readFromDisk(QFile &file) switch (bomType) { case BomType::Utf16BE: decoder = QStringDecoder(QStringDecoder::Utf16BE); - encoder = QStringEncoder(QStringEncoder::Utf8); offset = 2; break; case BomType::Utf16LE: decoder = QStringDecoder(QStringDecoder::Utf16LE); - encoder = QStringEncoder(QStringEncoder::Utf8); offset = 2; break; case BomType::Utf8: - // No decoder needed if you're already passing UTF-8 to Scintilla. + // No decoder needed since Scintilla expects UTF-8. offset = 3; break; default: - // Whatever your handling is for no BOM. break; } }