This is an automated email from the ASF dual-hosted git repository.

jimjag pushed a commit to branch openformula
in repository https://gitbox.apache.org/repos/asf/openoffice.git

commit b3bfdbdc34a872b8ff7ee057c61cad3d6d43ee1e
Author: Jim Jagielski <[email protected]>
AuthorDate: Sun Nov 15 12:52:24 2026 -0500

    sc: allow string literals up to 32767 characters in formulas
---
 main/sc/inc/compiler.hxx              | 12 +++++++++++-
 main/sc/source/core/tool/compiler.cxx | 21 +++++++++++++--------
 main/sc/source/core/tool/token.cxx    | 15 +++++++++++++++
 test/odf-validate/calcval-cases14.tsv |  2 ++
 4 files changed, 41 insertions(+), 9 deletions(-)

diff --git a/main/sc/inc/compiler.hxx b/main/sc/inc/compiler.hxx
index 77c23d0989..75008d3be9 100644
--- a/main/sc/inc/compiler.hxx
+++ b/main/sc/inc/compiler.hxx
@@ -58,6 +58,8 @@
 
 #define MAXCODE      2048   /* maximum number of tokens in formula */
 #define MAXSTRLEN    1024   /* maximum length of input string of one symbol */
+#define MAXSTRLITLEN 32767  /* maximum length of a string literal in a formula 
*/
+#define MAXSYMLEN    (MAXSTRLITLEN+3)   /* symbol buffer: quote, literal, 
quote, 0 */
 #define MAXJUMPCOUNT 32     /* maximum number of jumps (ocChose) */
 
 // flag values of CharTable
@@ -200,6 +202,8 @@ public:
     void SetExternal(const sal_Unicode* pStr);
 
     ScRawToken* Clone() const;      // real copy!
+    // Heap token sized to the string, so it may exceed MAXSTRLEN; free with 
Delete().
+    static ScRawToken* NewString( const sal_Unicode* pStr, xub_StrLen nLen );
     formula::FormulaToken* CreateToken() const;   // create typified token
     void Load( SvStream&, sal_uInt16 nVer );
 
@@ -310,7 +314,7 @@ private:
     // For CONV_XL_OOX, may be set via API by MOOXML filter.
     ::com::sun::star::uno::Sequence< const 
::com::sun::star::sheet::ExternalLinkInfo > maExternalLinks;
 
-    sal_Unicode cSymbol[MAXSTRLEN];                 // current Symbol
+    sal_Unicode* cSymbol;                           // current Symbol, 
MAXSYMLEN characters
     String      aFormula;                           // formula source code
     xub_StrLen  nSrcPos;                            // tokenizer position 
(source code)
     ScRawTokenRef   pRawToken;
@@ -354,6 +358,12 @@ public:
 
     ScCompiler( ScDocument* pDocument, const ScAddress&,ScTokenArray& rArr);
 
+    virtual ~ScCompiler();
+
+private:
+    ScCompiler( const ScCompiler& );            // not implemented
+    ScCompiler& operator=( const ScCompiler& ); // not implemented
+
 public:
     static void DeInit();               /// all
 
diff --git a/main/sc/source/core/tool/compiler.cxx 
b/main/sc/source/core/tool/compiler.cxx
index 6429e64eaa..5382621c69 100644
--- a/main/sc/source/core/tool/compiler.cxx
+++ b/main/sc/source/core/tool/compiler.cxx
@@ -1815,6 +1815,7 @@ ScCompiler::ScCompiler( ScDocument* pDocument, const 
ScAddress& rPos,ScTokenArra
         mbExtendedErrorDetection( false ),
         mbRewind( false )
 {
+    cSymbol = new sal_Unicode[ MAXSYMLEN ];
     nMaxTab = pDoc ? pDoc->GetTableCount() - 1 : 0;
 }
 
@@ -1831,9 +1832,15 @@ ScCompiler::ScCompiler( ScDocument* pDocument, const 
ScAddress& rPos)
         mbExtendedErrorDetection( false ),
         mbRewind( false )
 {
+    cSymbol = new sal_Unicode[ MAXSYMLEN ];
     nMaxTab = pDoc ? pDoc->GetTableCount() - 1 : 0;
 }
 
+ScCompiler::~ScCompiler()
+{
+    delete [] cSymbol;
+}
+
 void ScCompiler::CheckTabQuotes( String& rString,
                                  const FormulaGrammar::AddressConvention eConv 
)
 {
@@ -1969,7 +1976,7 @@ sal_Unicode* lcl_UnicodeStrNCpy( sal_Unicode* pDst, const 
sal_Unicode* pSrc, xub
 
 xub_StrLen ScCompiler::NextSymbol(bool bInArray)
 {
-    cSymbol[MAXSTRLEN-1] = 0;       // Stopper
+    cSymbol[MAXSYMLEN-1] = 0;       // Stopper
     sal_Unicode* pSym = cSymbol;
     const sal_Unicode* const pStart = aFormula.GetBuffer();
     const sal_Unicode* pSrc = pStart + nSrcPos;
@@ -2239,7 +2246,7 @@ Label_MaskStateMachine:
                 }
                 if ( !bQuote )
                 {
-                    if( pSym == &cSymbol[ MAXSTRLEN-1 ] )
+                    if( pSym == &cSymbol[ MAXSYMLEN-1 ] )
                     {
                         SetError(errStringOverflow);
                         eState = ssSkipString;
@@ -2686,7 +2693,7 @@ sal_Bool ScCompiler::IsString()
         p++;
     xub_StrLen nLen = sal::static_int_cast<xub_StrLen>( p - cSymbol - 1 );
     sal_Bool bQuote = ((cSymbol[0] == '"') && (cSymbol[nLen] == '"'));
-    if ((bQuote ? nLen-2 : nLen) > MAXSTRLEN-1)
+    if ((bQuote ? nLen-1 > MAXSTRLITLEN : nLen >= MAXSTRLEN-1))
     {
         SetError(errStringOverflow);
         return sal_False;
@@ -2694,9 +2701,7 @@ sal_Bool ScCompiler::IsString()
     if ( bQuote )
     {
         cSymbol[nLen] = '\0';
-        ScRawToken aToken;
-        aToken.SetString( cSymbol+1 );
-        pRawToken = aToken.Clone();
+        pRawToken = ScRawToken::NewString( cSymbol+1, nLen ? nLen-1 : 0 );   
// lone quote: nLen is 0
         return sal_True;
     }
     return sal_False;
@@ -3377,8 +3382,8 @@ void ScCompiler::AutoCorrectParsedSymbol()
                     ((GetCharTableFlags( aCorrectedSymbol.GetChar(nPos) ) &
                     (SC_COMPILER_C_WORD | SC_COMPILER_C_CHAR_DONTCARE)) == 0)) 
)
                 nPos--;
-            if ( nPos == MAXSTRLEN - 2 )
-                aCorrectedSymbol.SetChar( nPos, cQuote );   // '"' the 255th 
character
+            if ( nPos == MAXSYMLEN - 2 )
+                aCorrectedSymbol.SetChar( nPos, cQuote );   // '"' the last 
character
             else
                 aCorrectedSymbol.Insert( cQuote, nPos + 1 );
             bCorrected = sal_True;
diff --git a/main/sc/source/core/tool/token.cxx 
b/main/sc/source/core/tool/token.cxx
index 9dda7fd408..3ddd04eff9 100644
--- a/main/sc/source/core/tool/token.cxx
+++ b/main/sc/source/core/tool/token.cxx
@@ -385,6 +385,21 @@ ScRawToken* ScRawToken::Clone() const
 }
 
 
+ScRawToken* ScRawToken::NewString( const sal_Unicode* pStr, xub_StrLen nLen )
+{
+    static sal_uInt16 nOffset = lcl_ScRawTokenOffset();     // offset of sbyte 
and cStr
+    size_t n = nOffset + GetStrLenBytes( nLen ) + GetStrLenBytes( 1 );
+    ScRawToken* p = (ScRawToken*) new sal_uInt8[ n ];
+    p->eOp = ocPush;
+    p->eType = svString;
+    p->nRefCnt = 0;
+    p->bRaw = sal_False;
+    memcpy( p->cStr, pStr, GetStrLenBytes( nLen ) );
+    p->cStr[ nLen ] = 0;
+    return p;
+}
+
+
 FormulaToken* ScRawToken::CreateToken() const
 {
 #ifdef DBG_UTIL
diff --git a/test/odf-validate/calcval-cases14.tsv 
b/test/odf-validate/calcval-cases14.tsv
index 3fdd4c8271..0ee7901b17 100644
--- a/test/odf-validate/calcval-cases14.tsv
+++ b/test/odf-validate/calcval-cases14.tsv
@@ -195,6 +195,8 @@ BITAND.3params      =BITAND(6;3;1)  cell    #ERR    6.6.2 
BITAND exactly 2 params enforced
 SG.BL-001-1024chars    
=1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1
 [...]
 SG.BL-002-30params     
=MAX(1;2;3;4;5;6;7;8;9;10;11;12;13;14;15;16;17;18;19;20;21;22;23;24;25;26;27;28;29;30)
  cell    30      3.7 basic limits: 30 parameters in one function call
 SG.BL-003-32767chars   =LEN(REPT("a";32767))   cell    32767   3.7 basic 
limits: a string of 32,767 ASCII characters
+SG.BL-003-literal1022  
=LEN("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 [...]
+SG.BL-003-literal32767 
=LEN("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa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