Kaydet (Commit) 4bdbea54 authored tarafından Tobias Lippert's avatar Tobias Lippert Kaydeden (comit) Caolán McNamara

tdf#89520 Make TOX creation more robust

The code is now more robust and will accept illegal arguments.

Change-Id: I43ae82b953cea845fb170aa7b6e8d42470ad4e5e
Reviewed-on: https://gerrit.libreoffice.org/14580Tested-by: 's avatarJenkins <ci@libreoffice.org>
Reviewed-by: 's avatarCaolán McNamara <caolanm@redhat.com>
Tested-by: 's avatarCaolán McNamara <caolanm@redhat.com>
üst e719183c
...@@ -32,10 +32,14 @@ class ToxWhitespaceStripperTest : public CppUnit::TestFixture ...@@ -32,10 +32,14 @@ class ToxWhitespaceStripperTest : public CppUnit::TestFixture
void void
PositionAfterStringCanBeRequested(); PositionAfterStringCanBeRequested();
void
InvalidPositionIsMappedToLastEntry();
CPPUNIT_TEST_SUITE(ToxWhitespaceStripperTest); CPPUNIT_TEST_SUITE(ToxWhitespaceStripperTest);
CPPUNIT_TEST(MappingCharactersToVariousStrippedStringsWorks); CPPUNIT_TEST(MappingCharactersToVariousStrippedStringsWorks);
CPPUNIT_TEST(StrippingWhitespacesFromVariousStringsWorks); CPPUNIT_TEST(StrippingWhitespacesFromVariousStringsWorks);
CPPUNIT_TEST(PositionAfterStringCanBeRequested); CPPUNIT_TEST(PositionAfterStringCanBeRequested);
CPPUNIT_TEST(InvalidPositionIsMappedToLastEntry);
CPPUNIT_TEST_SUITE_END(); CPPUNIT_TEST_SUITE_END();
...@@ -141,6 +145,16 @@ ToxWhitespaceStripperTest::PositionAfterStringCanBeRequested() ...@@ -141,6 +145,16 @@ ToxWhitespaceStripperTest::PositionAfterStringCanBeRequested()
CPPUNIT_ASSERT_EQUAL(expected, sut.GetPositionInStrippedString(test.getLength())); CPPUNIT_ASSERT_EQUAL(expected, sut.GetPositionInStrippedString(test.getLength()));
} }
void
ToxWhitespaceStripperTest::InvalidPositionIsMappedToLastEntry()
{
OUString test("ab c");
ToxWhitespaceStripper sut(test);
sal_Int32 expected = 4; // the length of the string after merging the two whitespaces
sal_Int32 result = sut.GetPositionInStrippedString(40); // a value past the original string length
CPPUNIT_ASSERT_EQUAL(expected, result);
}
// Put the test suite in the registry // Put the test suite in the registry
CPPUNIT_TEST_SUITE_REGISTRATION(ToxWhitespaceStripperTest); CPPUNIT_TEST_SUITE_REGISTRATION(ToxWhitespaceStripperTest);
......
...@@ -10,6 +10,8 @@ ...@@ -10,6 +10,8 @@
#include "ToxWhitespaceStripper.hxx" #include "ToxWhitespaceStripper.hxx"
#include "rtl/ustrbuf.hxx" #include "rtl/ustrbuf.hxx"
#include "sal/log.hxx"
#include <boost/numeric/conversion/cast.hpp> #include <boost/numeric/conversion/cast.hpp>
namespace sw { namespace sw {
...@@ -50,6 +52,11 @@ sal_Int32 ...@@ -50,6 +52,11 @@ sal_Int32
ToxWhitespaceStripper::GetPositionInStrippedString(sal_Int32 pos) const ToxWhitespaceStripper::GetPositionInStrippedString(sal_Int32 pos) const
{ {
size_t upos = boost::numeric_cast<size_t>(pos); size_t upos = boost::numeric_cast<size_t>(pos);
if (upos >= mNewPositions.size()) {
SAL_WARN("sw.core", "Requested position of TOX entry text which does not exist. "
"Maybe the formatting hint is corrupt?");
return mNewPositions.back();
}
return mNewPositions.at(upos); return mNewPositions.at(upos);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment