Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions src/encodes/detectcode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -996,8 +996,26 @@ bool DetectCode::ChangeFileEncodingFormat(QByteArray &inputStr,
inbytesleft -= replaceLen;
qCDebug(appLog) << "Skipped" << replaceLen << "bytes from input, remaining inbytesleft:" << inbytesleft;
} else {
qCWarning(appLog) << "[DetectCode] iconv_open failed for conversion from" << fromCode << "to" << toCode;
break;
qCWarning(appLog) << "[DetectCode] iconv conversion error for" << fromCode << "to" << toCode << "errno:" << errorNum;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium · Valid text is lost on E2BIG

When iconv returns E2BIG because the remaining output space cannot fit the next character, the error handler treats E2BIG as malformed input, writes '?', and advances inbuf past valid data instead of retrying; callers receive corrupted conversion output as success.

Handle E2BIG by providing more output space and retrying without consuming the unconverted input.

Also at src/encodes/detectcode.cpp:1009-1017.

Prompt for AI agents
In `src/encodes/detectcode.cpp` at line 999:

**Valid text is lost on E2BIG**

When iconv returns E2BIG because the remaining output space cannot fit the next character, the error handler treats E2BIG as malformed input, writes '?', and advances `inbuf` past valid data instead of retrying; callers receive corrupted conversion output as success.

Handle E2BIG by providing more output space and retrying without consuming the unconverted input.

Also at `src/encodes/detectcode.cpp:1009-1017`.

if (outbytesleft == 0) {
qCWarning(appLog) << "Output buffer exhausted, breaking conversion";
break;
}
if (outbytesleft < 1) {
qCWarning(appLog) << "Output buffer too small for replacement char, breaking";
break;
}
outbytesleft -= 1;
*outbuf = '?';

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium · Replacement output is malformed

When a non-EILSEQ error occurs while converting to a multibyte target such as UTF-16 or UTF-32, ChangeFileEncodingFormat writes ? as a single raw byte instead of encoding it in toCode, so the result is malformed and callers can save or receive undecodable output.

Encode the replacement character in toCode before writing it to the output buffer.

Prompt for AI agents
In `src/encodes/detectcode.cpp` at line 1009:

**Replacement output is malformed**

When a non-EILSEQ error occurs while converting to a multibyte target such as UTF-16 or UTF-32, `ChangeFileEncodingFormat` writes `?` as a single raw byte instead of encoding it in `toCode`, so the result is malformed and callers can save or receive undecodable output.

Encode the replacement character in `toCode` before writing it to the output buffer.

outbuf += 1;
qCDebug(appLog) << "Appended replacement char '?' for non-EILSEQ error, remaining outbytesleft:" << outbytesleft;
if (inbytesleft <= 1) {
inbuf += inbytesleft;
inbytesleft = 0;
break;
Comment on lines +1012 to +1015

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium · Large-file boundary characters are corrupted

When a file larger than 40 MiB uses a non-UTF-8 encoding and a multibyte character straddles the 1 MiB head boundary, ChangeFileEncodingFormat converts the head and remainder independently; it replaces each byte of an incomplete multibyte character at the head’s end, while conversion of the remainder replaces its continuation bytes too, so that character appears as multiple ? characters.

Preserve incomplete trailing bytes for conversion with the remainder, or use a streaming converter that carries state across the boundary.

Prompt for AI agents
In `src/encodes/detectcode.cpp` at lines 1012-1015:

**Large-file boundary characters are corrupted**

When a file larger than 40 MiB uses a non-UTF-8 encoding and a multibyte character straddles the 1 MiB head boundary, `ChangeFileEncodingFormat` converts the head and remainder independently; it replaces each byte of an incomplete multibyte character at the head’s end, while conversion of the remainder replaces its continuation bytes too, so that character appears as multiple `?` characters.

Preserve incomplete trailing bytes for conversion with the remainder, or use a streaming converter that carries state across the boundary.

}
inbuf += 1;
inbytesleft -= 1;
Comment on lines +1017 to +1018

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium · One truncated character becomes several

When the input ends with a truncated multibyte character, the non-EILSEQ handler advances inbuf by one byte and retries, so each remaining byte of the incomplete character produces another ? and the caller sees multiple replacements.

Treat the incomplete trailing sequence as one invalid character and emit only one replacement for it.

Prompt for AI agents
In `src/encodes/detectcode.cpp` at lines 1017-1018:

**One truncated character becomes several**

When the input ends with a truncated multibyte character, the non-EILSEQ handler advances `inbuf` by one byte and retries, so each remaining byte of the incomplete character produces another `?` and the caller sees multiple replacements.

Treat the incomplete trailing sequence as one invalid character and emit only one replacement for it.

}
}
} while (static_cast<size_t>(-1) == ret);
Expand Down
8 changes: 4 additions & 4 deletions tests/common2/test_detectcode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -876,15 +876,15 @@ TEST_F(DetectCodeTest, ChangeEncoding_InvalidUtf8Byte_ReplacedWithQuestionMark)
EXPECT_EQ(out, QByteArrayLiteral("?"));
}

TEST_F(DetectCodeTest, ChangeEncoding_TruncatedUtf8_KeepsConvertedPrefix)
TEST_F(DetectCodeTest, ChangeEncoding_TruncatedUtf8_ReplacesInvalidByteAndContinues)
{
// Arrange: E6 EINVAL(不完整多字节序列在末尾)→ 保留已转换前缀
// Arrange: E6 EINVAL(不完整多字节序列在末尾)→ 'a' 已转换,尾部 C3 替换为 '?'
QByteArray in = QByteArrayLiteral("a") + QByteArray::fromHex("C3"), out;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium · Continuation is never verified

When the conversion appends ? for the incomplete trailing byte but incorrectly stops instead of resuming, the ChangeEncoding_TruncatedUtf8_ReplacesInvalidByteAndContinues test puts the invalid C3 byte at the end of its input (line 882), so an implementation that appends ? and then incorrectly stops still satisfies the a? assertion at line 887; the test does not verify that later content survives.

Add valid bytes after the incomplete sequence and assert that they appear after the replacement in the output.

Prompt for AI agents
In `tests/common2/test_detectcode.cpp` at line 882:

**Continuation is never verified**

When the conversion appends `?` for the incomplete trailing byte but incorrectly stops instead of resuming, the `ChangeEncoding_TruncatedUtf8_ReplacesInvalidByteAndContinues` test puts the invalid `C3` byte at the end of its input (line 882), so an implementation that appends `?` and then incorrectly stops still satisfies the `a?` assertion at line 887; the test does not verify that later content survives.

Add valid bytes after the incomplete sequence and assert that they appear after the replacement in the output.

// Act
const bool ok = DetectCode::ChangeFileEncodingFormat(in, out, QStringLiteral("UTF-8"), QStringLiteral("GB18030"));
// Assert: 'a' 已转换,截断尾部放弃
// Assert: 'a' 已转换,非法尾部字节替换为 '?'
EXPECT_TRUE(ok);
EXPECT_EQ(out, QByteArrayLiteral("a"));
EXPECT_EQ(out, QByteArrayLiteral("a?"));

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium · Regression test bypasses iconv

When the test suite runs this updated truncated-UTF-8 test, the updated test converts UTF-8 to GB18030, but ChangeFileEncodingFormat() routes any conversion involving GB18030 to convertEncodingTextCodec() before reaching the changed iconv branch; the test therefore passes without exercising the fix and cannot catch a regression there.

Add a truncated-input test using an encoding pair that reaches the iconv branch and assert the replacement and converted output.

Prompt for AI agents
In `tests/common2/test_detectcode.cpp` at line 887:

**Regression test bypasses iconv**

When the test suite runs this updated truncated-UTF-8 test, the updated test converts UTF-8 to GB18030, but `ChangeFileEncodingFormat()` routes any conversion involving GB18030 to `convertEncodingTextCodec()` before reaching the changed iconv branch; the test therefore passes without exercising the fix and cannot catch a regression there.

Add a truncated-input test using an encoding pair that reaches the iconv branch and assert the replacement and converted output.

}

TEST_F(DetectCodeTest, ChangeEncoding_InvalidGb18030Byte_ReplacedAndContinues)
Expand Down
Loading