summaryrefslogtreecommitdiff
path: root/sal
diff options
context:
space:
mode:
authorMike Kaganski <mike.kaganski@collabora.com>2022-03-05 19:26:38 +0300
committerMike Kaganski <mike.kaganski@collabora.com>2022-03-05 18:17:55 +0100
commit713c83c0fc4a0d9950cfa0b598d7c5f4bae15755 (patch)
tree36e9d8583568aa0f89a886c95ff713ca754c670b /sal
parent805e73b555df277e7fb35825a0c48d244a2691a9 (diff)
Add checks to avoid finding empty substring / zero character
... which changed in commit 281989007fd7dea997ed9a65f513f80b1aff67dd Author Noel Grandin <noel@peralex.com> Date Tue Jul 01 13:17:01 2014 +0200 Use standard library optimised routines for OUString/OString for optimized cases: strchr/wcschr/strrchr/wcsrchr find trailing zero character, and strstr/wcsstr find empty string; previous/unoptimized code does not find these. This introduced inconsistency between char and sal_Unicode functions on non-Windows, and for sal_Unicode between Windows and non-Windows (because on Windows, optimized code is used for sal_Unicode, while on other platforms, unoptimized code is used). Change-Id: I68529c91b26f4113d9bd7777fc5ac4809349864b Reviewed-on: https://gerrit.libreoffice.org/c/core/+/131064 Tested-by: Jenkins Reviewed-by: Mike Kaganski <mike.kaganski@collabora.com>
Diffstat (limited to 'sal')
-rw-r--r--sal/rtl/strtmpl.hxx9
1 files changed, 9 insertions, 0 deletions
diff --git a/sal/rtl/strtmpl.hxx b/sal/rtl/strtmpl.hxx
index 6fa400c0164f..37f274a69cf2 100644
--- a/sal/rtl/strtmpl.hxx
+++ b/sal/rtl/strtmpl.hxx
@@ -307,6 +307,9 @@ sal_Int32 indexOfChar ( const IMPL_RTL_STRCODE* pStr
IMPL_RTL_STRCODE c )
{
assert(pStr);
+ if (!c)
+ return -1; // Unifies behavior of strchr/wcschr and unoptimized algorithm wrt '\0'
+
if constexpr (sizeof(IMPL_RTL_STRCODE) == sizeof(char))
{
// take advantage of builtin optimisations
@@ -361,6 +364,9 @@ sal_Int32 lastIndexOfChar ( const IMPL_RTL_STRCODE*
IMPL_RTL_STRCODE c )
{
assert(pStr);
+ if (!c)
+ return -1; // Unifies behavior of strrchr/wcsrchr and lastIndexOfChar_WithLength wrt '\0'
+
if constexpr (sizeof(IMPL_RTL_STRCODE) == sizeof(char))
{
// take advantage of builtin optimisations
@@ -406,6 +412,9 @@ sal_Int32 indexOfStr ( const IMPL_RTL_STRCODE* pStr,
{
assert(pStr);
assert(pSubStr);
+ /* an empty SubString is always not findable */
+ if (*pSubStr == 0)
+ return -1;
if constexpr (sizeof(IMPL_RTL_STRCODE) == sizeof(char))
{
// take advantage of builtin optimisations