basic/qa/basic_coverage/test_like.bas |   10 ++++++++++
 basic/source/runtime/runtime.cxx      |   10 ++++++++--
 2 files changed, 18 insertions(+), 2 deletions(-)

New commits:
commit 3ab2560526ba7b745889cf509241ff17d0638cc2
Author:     Mike Kaganski <[email protected]>
AuthorDate: Thu Oct 30 17:38:27 2025 +0500
Commit:     Mike Kaganski <[email protected]>
CommitDate: Thu Oct 30 18:18:57 2025 +0100

    tdf#169147: handle errors creating icu::RegexMatcher
    
    Regression after commit 38f731ff67142a423aa6f46dc3e92d094f86ffb1
    (tdf#160478: fix Basic LIKE operator, 2024-04-02).
    
    Change-Id: I4fae01e56cb91d54adba54c3787f576f60f8b1f0
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/193202
    Reviewed-by: Mike Kaganski <[email protected]>
    Tested-by: Jenkins

diff --git a/basic/qa/basic_coverage/test_like.bas 
b/basic/qa/basic_coverage/test_like.bas
index 2d018bf64f01..3e7d049d081f 100644
--- a/basic/qa/basic_coverage/test_like.bas
+++ b/basic/qa/basic_coverage/test_like.bas
@@ -34,8 +34,18 @@ Sub verify_testLike()
     TestUtil.AssertEqual("a*b" Like "a [*]b", False, "Like9")
     TestUtil.AssertEqual("axxxxxb" Like "a [*]b", False, "Like10")
     TestUtil.AssertEqual("a [xyz" Like "a [[]*", True, "Like11")
+    ' Invalid pattern
+    Dim caughtError As Integer
+    On Error GoTo expectedErrorHandler
+    TestUtil.AssertEqual("a [xyz" Like "a [*", 42, "") ' Like must fail, will 
not reach AssertEqual
+    TestUtil.AssertEqual(caughtError, 93, "tdf#169147") ' Invalid string 
pattern
 
     Exit Sub
+
+expectedErrorHandler:
+    caughtError = Err
+    Resume Next
+
 errorHandler:
     TestUtil.ReportErrorHandler("verify_testLike", Err, Error$, Erl)
 End Sub
diff --git a/basic/source/runtime/runtime.cxx b/basic/source/runtime/runtime.cxx
index 4dcd6952f3e9..63ef87867549 100644
--- a/basic/source/runtime/runtime.cxx
+++ b/basic/source/runtime/runtime.cxx
@@ -1524,13 +1524,13 @@ void SbiRuntime::StepLIKE()
     {
         bTextMode = IsImageFlag( SbiImageFlags::COMPARETEXT );
     }
-    sal_uInt32 searchFlags = UREGEX_UWORD | UREGEX_DOTALL; // Dot matches 
newline
+    uint32_t searchFlags = UREGEX_UWORD | UREGEX_DOTALL; // Dot matches newline
     if( bTextMode )
     {
         searchFlags |= UREGEX_CASE_INSENSITIVE;
     }
 
-    static sal_uInt32 cachedSearchFlags = 0;
+    static uint32_t cachedSearchFlags = 0;
     static OUString cachedRegex;
     static std::optional<icu::RegexMatcher> oRegexMatcher;
     UErrorCode nIcuErr = U_ZERO_ERROR;
@@ -1541,6 +1541,12 @@ void SbiRuntime::StepLIKE()
         icu::UnicodeString sRegex(false, reinterpret_cast<const 
UChar*>(cachedRegex.getStr()),
                                   cachedRegex.getLength());
         oRegexMatcher.emplace(sRegex, cachedSearchFlags, nIcuErr);
+        if (U_FAILURE(nIcuErr))
+        {
+            // Bad regex; oRegexMatcher has no pattern - would crash in 
RegexMatcher::reset
+            oRegexMatcher.reset();
+            return Error(ERRCODE_BASIC_BAD_PATTERN);
+        }
     }
 
     icu::UnicodeString sSource(false, reinterpret_cast<const 
UChar*>(value.getStr()),

Reply via email to