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
2 changes: 1 addition & 1 deletion src/main/cpp/charsetdecoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ class ISOLatinCharsetDecoder : public CharsetDecoder

while (src < srcEnd)
{
auto sv = static_cast<unsigned int>(*src++);
auto sv = static_cast<unsigned int>(static_cast<unsigned char>(*src++));
Transcoder::encode(sv, out);
}
in.increment_position(availableByteCount);
Expand Down
32 changes: 32 additions & 0 deletions src/test/cpp/helpers/charsetdecodertestcase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ LOGUNIT_CLASS(CharsetDecoderTestCase)
LOGUNIT_TEST(decode2);
LOGUNIT_TEST(decode3);
LOGUNIT_TEST(decode4);
LOGUNIT_TEST(testISOLatinHighBytes);
#if LOG4CXX_LOGCHAR_IS_WCHAR && LOG4CXX_HAS_MBSRTOWCS
LOGUNIT_TEST(testMbstowcsInfiniteLoop);
#endif
Expand Down Expand Up @@ -152,6 +153,37 @@ LOGUNIT_CLASS(CharsetDecoderTestCase)
}
}

/**
* Decoding ISO-8859-1 must map every byte 0x80..0xFF to the
* code point of the same numeric value. On platforms where plain
* char is signed (default on MSVC/GCC/Clang for x86/x64), a
* static_cast<unsigned int>(*src) sign-extends bytes >= 0x80 into
* 0xFFFFFFxx, which Transcoder::encode then treats as out-of-range
* Unicode and replaces with U+FFFD (or appends garbage on wchar_t
* builds). The .properties configuration loader uses this decoder
* per the Java spec, so the bug silently corrupts any non-ASCII
* Latin-1 byte that appears in a log4cxx configuration file.
*/
void testISOLatinHighBytes()
{
char buf[1];
auto dec = CharsetDecoder::getISOLatinDecoder();
for (unsigned int b = 0x80; b <= 0xFF; ++b)
{
buf[0] = static_cast<char>(b);
ByteBuffer in(buf, 1);
LogString out;
log4cxx_status_t stat = dec->decode(in, out);
LOGUNIT_ASSERT_EQUAL(APR_SUCCESS, stat);

// Build the expected LogString by encoding code point b
// through the same Transcoder path the decoder uses.
LogString expected;
Transcoder::encode(b, expected);
LOGUNIT_ASSERT_EQUAL(expected, out);
}
}

#if LOG4CXX_LOGCHAR_IS_WCHAR && LOG4CXX_HAS_MBSRTOWCS
/**
* Tests that we don't loop infinitely when mbsrtowcs refuses to consume
Expand Down
Loading