[Libreoffice-commits] core.git: 2 commits - sw/inc sw/source

Michael Stahl mstahl at redhat.com
Tue Apr 29 05:32:00 PDT 2014


 sw/inc/tox.hxx             |   42 ---------
 sw/source/core/tox/tox.cxx |  190 +++++++++++++++++++++++++--------------------
 2 files changed, 111 insertions(+), 121 deletions(-)

New commits:
commit 4d1c4c609bea91141d7960f9a3dd8d779573163f
Author: Michael Stahl <mstahl at redhat.com>
Date:   Tue Apr 29 14:02:47 2014 +0200

    sw: remove SwFormTokensHelper implementation details from header
    
    Change-Id: I9102caec1fec680e55e0ca66a0e1b02f41df5828

diff --git a/sw/inc/tox.hxx b/sw/inc/tox.hxx
index c626bda..cb92306 100644
--- a/sw/inc/tox.hxx
+++ b/sw/inc/tox.hxx
@@ -30,7 +30,6 @@
 #include <calbck.hxx>
 
 #include <vector>
-#include <boost/optional.hpp>
 
 namespace com { namespace sun { namespace star {
     namespace text { class XDocumentIndexMark; }
@@ -265,43 +264,7 @@ typedef std::vector<SwFormToken> SwFormTokens;
 class SW_DLLPUBLIC SwFormTokensHelper
 {
     /// the tokens
-    SwFormTokens aTokens;
-
-    /**
-       Builds a token from its string representation.
-
-       @sPattern          the whole pattern
-       @nCurPatternPos    starting position of the token
-
-       @return the token
-     */
-    SAL_DLLPRIVATE boost::optional<SwFormToken> BuildToken( const OUString & sPattern,
-                                          sal_Int32 & nCurPatternPos ) const;
-
-    /**
-       Returns the string of a token.
-
-       @param sPattern    the whole pattern
-       @param nStt        starting position of the token
-
-       @return   the string representation of the token
-    */
-    SAL_DLLPRIVATE OUString SearchNextToken( const OUString & sPattern,
-                                            sal_Int32 nStt ) const;
-
-    /**
-       Returns the type of a token.
-
-       @param sToken     the string representation of the token
-       @param pTokenLen  return parameter the length of the head of the token
-
-       If pTokenLen is non-NULL the length of the token's head is
-       written to *pTokenLen
-
-       @return the type of the token
-    */
-    SAL_DLLPRIVATE FormTokenType GetTokenType(const OUString & sToken,
-                                             sal_Int32 * pTokenLen) const;
+    SwFormTokens m_Tokens;
 
 public:
     /**
@@ -309,7 +272,7 @@ public:
 
        @param rTokens       vector of tokens
     */
-    SwFormTokensHelper(const SwFormTokens & rTokens) : aTokens(rTokens) {}
+    SwFormTokensHelper(const SwFormTokens & rTokens) : m_Tokens(rTokens) {}
 
     /**
        constructor
@@ -323,7 +286,7 @@ public:
 
        @return vector of tokens
     */
-    const SwFormTokens & GetTokens() const { return aTokens; }
+    const SwFormTokens & GetTokens() const { return m_Tokens; }
 };
 
 class SW_DLLPUBLIC SwForm
diff --git a/sw/source/core/tox/tox.cxx b/sw/source/core/tox/tox.cxx
index 03c9815..df3bd3f 100644
--- a/sw/source/core/tox/tox.cxx
+++ b/sw/source/core/tox/tox.cxx
@@ -695,27 +695,107 @@ OUString SwFormToken::GetString() const
 }
 
 // -> #i21237#
-SwFormTokensHelper::SwFormTokensHelper(const OUString & rPattern)
+
+/**
+   Returns the type of a token.
+
+   @param sToken     the string representation of the token
+   @param pTokenLen  return parameter the length of the head of the token
+
+   If pTokenLen is non-NULL the length of the token's head is
+   written to *pTokenLen
+
+   @return the type of the token
+*/
+static FormTokenType lcl_GetTokenType(const OUString & sToken,
+                                      sal_Int32 *const pTokenLen)
 {
-    sal_Int32 nCurPatternPos = 0;
+    static struct
+    {
+        OUString sNm;
+        sal_uInt16 nOffset;
+        FormTokenType eToken;
+    } const aTokenArr[] = {
+        { SwForm::GetFormTab(),         1, TOKEN_TAB_STOP },
+        { SwForm::GetFormPageNums(),    1, TOKEN_PAGE_NUMS },
+        { SwForm::GetFormLinkStt(),     1, TOKEN_LINK_START },
+        { SwForm::GetFormLinkEnd(),     1, TOKEN_LINK_END },
+        { SwForm::GetFormEntryNum(),    1, TOKEN_ENTRY_NO },
+        { SwForm::GetFormEntryTxt(),    1, TOKEN_ENTRY_TEXT },
+        { SwForm::GetFormChapterMark(), 1, TOKEN_CHAPTER_INFO },
+        { SwForm::GetFormText(),        1, TOKEN_TEXT },
+        { SwForm::GetFormEntry(),       1, TOKEN_ENTRY },
+        { SwForm::GetFormAuth(),        3, TOKEN_AUTHORITY }
+    };
 
-    while (nCurPatternPos < rPattern.getLength())
+    for( size_t i = 0; i<SAL_N_ELEMENTS(aTokenArr); ++i )
     {
-        boost::optional<SwFormToken> const oToken(
-                BuildToken(rPattern, nCurPatternPos));
-        if (oToken)
-            aTokens.push_back(oToken.get());
+        const sal_Int32 nLen(aTokenArr[i].sNm.getLength());
+        if( sToken.startsWith( aTokenArr[i].sNm.copy(0, nLen - aTokenArr[i].nOffset) ))
+        {
+            if (pTokenLen)
+                *pTokenLen = nLen;
+            return aTokenArr[ i ].eToken;
+        }
     }
+
+    SAL_WARN("sw.core", "SwFormTokensHelper: invalid token");
+    return TOKEN_END;
 }
 
-boost::optional<SwFormToken>
-SwFormTokensHelper::BuildToken( const OUString & sPattern,
-                                            sal_Int32 & nCurPatternPos ) const
+/**
+   Returns the string of a token.
+
+   @param sPattern    the whole pattern
+   @param nStt        starting position of the token
+
+   @return   the string representation of the token
+*/
+static OUString
+lcl_SearchNextToken(const OUString & sPattern, sal_Int32 const nStt)
 {
-    OUString sToken( SearchNextToken(sPattern, nCurPatternPos) );
+    sal_Int32 nEnd = sPattern.indexOf( '>', nStt );
+    if (nEnd >= 0)
+    {
+        // apparently the TOX_STYLE_DELIMITER act as a bracketing for
+        // TOKEN_TEXT tokens so that the user can have '>' inside the text...
+        const sal_Int32 nTextSeparatorFirst = sPattern.indexOf( TOX_STYLE_DELIMITER, nStt );
+        if (    nTextSeparatorFirst >= 0
+            &&  nTextSeparatorFirst + 1 < sPattern.getLength()
+            &&  nTextSeparatorFirst < nEnd)
+        {
+            const sal_Int32 nTextSeparatorSecond = sPattern.indexOf( TOX_STYLE_DELIMITER,
+                                                                     nTextSeparatorFirst + 1 );
+            // Since nEnd>=0 we don't need to check if nTextSeparatorSecond<0!
+            if( nEnd < nTextSeparatorSecond )
+                nEnd = sPattern.indexOf( '>', nTextSeparatorSecond );
+            // FIXME: No check to verify that nEnd is still >=0?
+            assert(nEnd >= 0);
+        }
+
+        ++nEnd;
+
+        return sPattern.copy( nStt, nEnd - nStt );
+    }
+
+    return OUString();
+}
+
+/**
+   Builds a token from its string representation.
+
+   @sPattern          the whole pattern
+   @nCurPatternPos    starting position of the token
+
+   @return the token
+ */
+static boost::optional<SwFormToken>
+lcl_BuildToken(const OUString & sPattern, sal_Int32 & nCurPatternPos)
+{
+    OUString sToken( lcl_SearchNextToken(sPattern, nCurPatternPos) );
     nCurPatternPos += sToken.getLength();
     sal_Int32 nTokenLen = 0;
-    FormTokenType eTokenType = GetTokenType(sToken, &nTokenLen);
+    FormTokenType const eTokenType = lcl_GetTokenType(sToken, &nTokenLen);
     if (TOKEN_END == eTokenType) // invalid input? skip it
     {
         nCurPatternPos = sPattern.getLength();
@@ -791,70 +871,17 @@ SwFormTokensHelper::BuildToken( const OUString & sPattern,
     return eRet;
 }
 
-OUString SwFormTokensHelper::SearchNextToken( const OUString & sPattern,
-                                              sal_Int32 nStt ) const
-{
-    sal_Int32 nEnd = sPattern.indexOf( '>', nStt );
-    if (nEnd >= 0)
-    {
-        // apparently the TOX_STYLE_DELIMITER act as a bracketing for
-        // TOKEN_TEXT tokens so that the user can have '>' inside the text...
-        const sal_Int32 nTextSeparatorFirst = sPattern.indexOf( TOX_STYLE_DELIMITER, nStt );
-        if (    nTextSeparatorFirst >= 0
-            &&  nTextSeparatorFirst + 1 < sPattern.getLength()
-            &&  nTextSeparatorFirst < nEnd)
-        {
-            const sal_Int32 nTextSeparatorSecond = sPattern.indexOf( TOX_STYLE_DELIMITER,
-                                                                     nTextSeparatorFirst + 1 );
-            // Since nEnd>=0 we don't need to check if nTextSeparatorSecond<0!
-            if( nEnd < nTextSeparatorSecond )
-                nEnd = sPattern.indexOf( '>', nTextSeparatorSecond );
-            // FIXME: No check to verify that nEnd is still >=0?
-            assert(nEnd >= 0);
-        }
-
-        ++nEnd;
-
-        return sPattern.copy( nStt, nEnd - nStt );
-    }
-
-    return OUString();
-}
-
-FormTokenType SwFormTokensHelper::GetTokenType(const OUString & sToken,
-                                               sal_Int32 * pTokenLen) const
+SwFormTokensHelper::SwFormTokensHelper(const OUString & rPattern)
 {
-    static struct
-    {
-        OUString sNm;
-        sal_uInt16 nOffset;
-        FormTokenType eToken;
-    } const aTokenArr[] = {
-        { SwForm::GetFormTab(),         1, TOKEN_TAB_STOP },
-        { SwForm::GetFormPageNums(),    1, TOKEN_PAGE_NUMS },
-        { SwForm::GetFormLinkStt(),     1, TOKEN_LINK_START },
-        { SwForm::GetFormLinkEnd(),     1, TOKEN_LINK_END },
-        { SwForm::GetFormEntryNum(),    1, TOKEN_ENTRY_NO },
-        { SwForm::GetFormEntryTxt(),    1, TOKEN_ENTRY_TEXT },
-        { SwForm::GetFormChapterMark(), 1, TOKEN_CHAPTER_INFO },
-        { SwForm::GetFormText(),        1, TOKEN_TEXT },
-        { SwForm::GetFormEntry(),       1, TOKEN_ENTRY },
-        { SwForm::GetFormAuth(),        3, TOKEN_AUTHORITY }
-    };
+    sal_Int32 nCurPatternPos = 0;
 
-    for( size_t i = 0; i<SAL_N_ELEMENTS(aTokenArr); ++i )
+    while (nCurPatternPos < rPattern.getLength())
     {
-        const sal_Int32 nLen(aTokenArr[i].sNm.getLength());
-        if( sToken.startsWith( aTokenArr[i].sNm.copy(0, nLen - aTokenArr[i].nOffset) ))
-        {
-            if (pTokenLen)
-                *pTokenLen = nLen;
-            return aTokenArr[ i ].eToken;
-        }
+        boost::optional<SwFormToken> const oToken(
+                lcl_BuildToken(rPattern, nCurPatternPos));
+        if (oToken)
+            m_Tokens.push_back(oToken.get());
     }
-
-    SAL_WARN("sw.core", "SwFormTokensHelper: invalid token");
-    return TOKEN_END;
 }
 
 // <- #i21237#
commit a03c88eded807da34d0490ab4e1830e7573338e9
Author: Michael Stahl <mstahl at redhat.com>
Date:   Tue Apr 29 13:53:13 2014 +0200

    fdo#77308: SwFormTokensHelper avoid infinite loop on invalid input
    
    In 4.1 this loops because the input is truncated to 64k length; try to
    avoid looping when the input is invalid and the terminating ">" cannot
    be found.
    
    Change-Id: I9261d9350f84ab366b822addde725645a46be830

diff --git a/sw/inc/tox.hxx b/sw/inc/tox.hxx
index 859374f..c626bda 100644
--- a/sw/inc/tox.hxx
+++ b/sw/inc/tox.hxx
@@ -30,6 +30,7 @@
 #include <calbck.hxx>
 
 #include <vector>
+#include <boost/optional.hpp>
 
 namespace com { namespace sun { namespace star {
     namespace text { class XDocumentIndexMark; }
@@ -274,7 +275,7 @@ class SW_DLLPUBLIC SwFormTokensHelper
 
        @return the token
      */
-    SAL_DLLPRIVATE SwFormToken BuildToken( const OUString & sPattern,
+    SAL_DLLPRIVATE boost::optional<SwFormToken> BuildToken( const OUString & sPattern,
                                           sal_Int32 & nCurPatternPos ) const;
 
     /**
diff --git a/sw/source/core/tox/tox.cxx b/sw/source/core/tox/tox.cxx
index f0f0495..03c9815 100644
--- a/sw/source/core/tox/tox.cxx
+++ b/sw/source/core/tox/tox.cxx
@@ -31,10 +31,14 @@
 #include <editeng/tstpitem.hxx>
 #include <SwStyleNameMapper.hxx>
 #include <hints.hxx>
-#include <algorithm>
 #include <functional>
 #include <switerator.hxx>
 
+#include <boost/optional.hpp>
+
+#include <algorithm>
+
+
 using namespace std;
 
 namespace
@@ -694,25 +698,29 @@ OUString SwFormToken::GetString() const
 SwFormTokensHelper::SwFormTokensHelper(const OUString & rPattern)
 {
     sal_Int32 nCurPatternPos = 0;
-    sal_Int32 nCurPatternLen = 0;
 
     while (nCurPatternPos < rPattern.getLength())
     {
-        // FIXME: nCurPatternLen added but set to 0?
-        nCurPatternPos = nCurPatternPos + nCurPatternLen;
-
-        SwFormToken aToken = BuildToken(rPattern, nCurPatternPos);
-        aTokens.push_back(aToken);
+        boost::optional<SwFormToken> const oToken(
+                BuildToken(rPattern, nCurPatternPos));
+        if (oToken)
+            aTokens.push_back(oToken.get());
     }
 }
 
-SwFormToken SwFormTokensHelper::BuildToken( const OUString & sPattern,
+boost::optional<SwFormToken>
+SwFormTokensHelper::BuildToken( const OUString & sPattern,
                                             sal_Int32 & nCurPatternPos ) const
 {
     OUString sToken( SearchNextToken(sPattern, nCurPatternPos) );
     nCurPatternPos += sToken.getLength();
     sal_Int32 nTokenLen = 0;
     FormTokenType eTokenType = GetTokenType(sToken, &nTokenLen);
+    if (TOKEN_END == eTokenType) // invalid input? skip it
+    {
+        nCurPatternPos = sPattern.getLength();
+        return boost::optional<SwFormToken>();
+    }
 
     // at this point sPattern contains the
     // character style name, the PoolId, tab stop position, tab stop alignment, chapter info format
@@ -786,17 +794,8 @@ SwFormToken SwFormTokensHelper::BuildToken( const OUString & sPattern,
 OUString SwFormTokensHelper::SearchNextToken( const OUString & sPattern,
                                               sal_Int32 nStt ) const
 {
-    //it's not so easy - it doesn't work if the text part contains a '>'
-
     sal_Int32 nEnd = sPattern.indexOf( '>', nStt );
-    if( nEnd<0 )
-    {
-        // FIXME: why is nEnd updated?
-        //        should "aResult = sPattern.copy( nStt, nEnd - nStt );"
-        //        or something like that be returned?
-        nEnd = sPattern.getLength();
-    }
-    else
+    if (nEnd >= 0)
     {
         // apparently the TOX_STYLE_DELIMITER act as a bracketing for
         // TOKEN_TEXT tokens so that the user can have '>' inside the text...
@@ -854,7 +853,7 @@ FormTokenType SwFormTokensHelper::GetTokenType(const OUString & sToken,
         }
     }
 
-    OSL_FAIL( "wrong token" );
+    SAL_WARN("sw.core", "SwFormTokensHelper: invalid token");
     return TOKEN_END;
 }
 


More information about the Libreoffice-commits mailing list