Skip to content

Commit

Permalink
enh(Foundation): #4690: protect against buffer overflow caused by bug…
Browse files Browse the repository at this point in the history
…gy TextEncoding implementations
  • Loading branch information
obiltschnig committed Sep 26, 2024
1 parent ce9c08a commit 54bc0fc
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Foundation/src/TextBufferIterator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ int TextBufferIterator::operator * () const

unsigned char buffer[TextEncoding::MAX_SEQUENCE_LENGTH];
unsigned char* p = buffer;
unsigned char* pend = p + TextEncoding::MAX_SEQUENCE_LENGTH;

if (it != _end)
*p++ = *it++;
Expand All @@ -115,6 +116,7 @@ int TextBufferIterator::operator * () const
{
while (read < -n && it != _end)
{
poco_assert(p != pend);
*p++ = *it++;
read++;
}
Expand Down
2 changes: 2 additions & 0 deletions Foundation/src/TextIterator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ int TextIterator::operator * () const

unsigned char buffer[TextEncoding::MAX_SEQUENCE_LENGTH];
unsigned char* p = buffer;
unsigned char* pend = p + TextEncoding::MAX_SEQUENCE_LENGTH;

if (it != _end)
*p++ = *it++;
Expand All @@ -112,6 +113,7 @@ int TextIterator::operator * () const
{
while (read < -n && it != _end)
{
poco_assert(p != pend);
*p++ = *it++;
read++;
}
Expand Down
34 changes: 34 additions & 0 deletions Foundation/testsuite/src/TextIteratorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@
#include "Poco/Latin1Encoding.h"
#include "Poco/UTF8Encoding.h"
#include "Poco/UTF16Encoding.h"
#include "Poco/UTF32Encoding.h"


using Poco::TextIterator;
using Poco::Latin1Encoding;
using Poco::UTF8Encoding;
using Poco::UTF16Encoding;
using Poco::UTF32Encoding;


TextIteratorTest::TextIteratorTest(const std::string& name): CppUnit::TestCase(name)
Expand Down Expand Up @@ -242,6 +244,36 @@ void TextIteratorTest::testSwap()
}


void TextIteratorTest::testUTF32Invalid1()
{
UTF32Encoding encoding;
const Poco::UInt32 data[] = {0x00000041, 0xffffffef, 0x00000041, 0x00000041, 0x00000041, 0x00000041, 0x00};
std::string text((const char*) data, 24);
TextIterator it(text, encoding);
TextIterator end(text);

assertTrue (it != end);
assertTrue (*it++ == 0x41);
assertTrue (it != end);
assertTrue (*it++ == -1);
}


void TextIteratorTest::testUTF32Invalid2()
{
UTF32Encoding encoding;
const Poco::UInt32 data[] = {0x00000041, 0xfffffffe, 0xfffffffe, 0x00};
std::string text((const char*) data, 12);
TextIterator it(text, encoding);
TextIterator end(text);

assertTrue (it != end);
assertTrue (*it++ == 0x41);
assertTrue (it != end);
assertTrue (*it++ == -1);
}


void TextIteratorTest::setUp()
{
}
Expand All @@ -265,6 +297,8 @@ CppUnit::Test* TextIteratorTest::suite()
CppUnit_addTest(pSuite, TextIteratorTest, testUTF8Supplementary);
CppUnit_addTest(pSuite, TextIteratorTest, testUTF16Supplementary);
CppUnit_addTest(pSuite, TextIteratorTest, testSwap);
CppUnit_addTest(pSuite, TextIteratorTest, testUTF32Invalid1);
CppUnit_addTest(pSuite, TextIteratorTest, testUTF32Invalid2);

return pSuite;
}
2 changes: 2 additions & 0 deletions Foundation/testsuite/src/TextIteratorTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class TextIteratorTest: public CppUnit::TestCase
void testUTF8Supplementary();
void testUTF16Supplementary();
void testSwap();
void testUTF32Invalid1();
void testUTF32Invalid2();

void setUp();
void tearDown();
Expand Down

0 comments on commit 54bc0fc

Please sign in to comment.