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 oRegexMatcher; UErrorCode nIcuErr = U_ZERO_ERROR; @@ -1541,6 +1541,12 @@ void SbiRuntime::StepLIKE() icu::UnicodeString sRegex(false, reinterpret_cast(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(value.getStr()),