diff options
author | Mike Kaganski <mike.kaganski@collabora.com> | 2022-03-08 17:49:03 +0300 |
---|---|---|
committer | Mike Kaganski <mike.kaganski@collabora.com> | 2022-03-09 06:05:37 +0100 |
commit | 82a1d32d3d3ac1b4b0a6d4cfaca791c77d9b3c03 (patch) | |
tree | cd88c3843beef334b00bf1cec18052249b88fef2 /sal/rtl/strtmpl.hxx | |
parent | db5207644308f304372d4d63a5f8f3f904b26869 (diff) |
Simplify getToken
It should not attempt to dereference pointers when nIndex is negative.
Properly handle too large nIndex.
Also it is not necessary to parse the string when nToken is negative.
Related to commit be281db569bafaac379feb604c39e220f51b18c4
Author Rüdiger Timm <rt@openoffice.org>
Date Mon Sep 20 07:43:20 2004 +0000
INTEGRATION: CWS ause011 (1.18.22); FILE MERGED
2004/08/18 11:47:54 sb 1.18.22.1: #i33153# Made getToken more robust.
Change-Id: I6fc77a5b70308ccca08cb2132bd78d024bd7e3e0
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/131221
Tested-by: Jenkins
Reviewed-by: Mike Kaganski <mike.kaganski@collabora.com>
Diffstat (limited to 'sal/rtl/strtmpl.hxx')
-rw-r--r-- | sal/rtl/strtmpl.hxx | 63 |
1 files changed, 26 insertions, 37 deletions
diff --git a/sal/rtl/strtmpl.hxx b/sal/rtl/strtmpl.hxx index 37f274a69cf2..0f0cc6755eb3 100644 --- a/sal/rtl/strtmpl.hxx +++ b/sal/rtl/strtmpl.hxx @@ -1256,54 +1256,43 @@ sal_Int32 getToken ( IMPL_RTL_STRINGDATA** ppThis { assert(ppThis); assert(pStr); - const auto* pCharStr = pStr->buffer; - sal_Int32 nLen = pStr->length-nIndex; - sal_Int32 nTokCount = 0; // Set ppThis to an empty string and return -1 if either nToken or nIndex is // negative: - if (nIndex < 0) - nToken = -1; - - pCharStr += nIndex; - const auto* pOrgCharStr = pCharStr; - const auto* pCharStrStart = pCharStr; - while ( nLen > 0 ) + if (nIndex >= 0 && nToken >= 0) { - if ( *pCharStr == cTok ) + const auto* pOrgCharStr = pStr->buffer; + const auto* pCharStr = pOrgCharStr + nIndex; + sal_Int32 nLen = pStr->length - nIndex; + sal_Int32 nTokCount = 0; + const auto* pCharStrStart = pCharStr; + while (nLen > 0) { - nTokCount++; - - if ( nTokCount == nToken ) - pCharStrStart = pCharStr+1; - else + if (*pCharStr == cTok) { - if ( nTokCount > nToken ) + nTokCount++; + + if (nTokCount > nToken) break; + if (nTokCount == nToken) + pCharStrStart = pCharStr + 1; } - } - pCharStr++; - nLen--; + pCharStr++; + nLen--; + } + if (nTokCount >= nToken) + { + newFromStr_WithLength(ppThis, pCharStrStart, pCharStr - pCharStrStart); + if (nLen > 0) + return pCharStr - pOrgCharStr + 1; + else + return -1; + } } - if ( (nToken < 0) || (nTokCount < nToken) || (pCharStr == pCharStrStart) ) - { - new_( ppThis ); - if( (nToken < 0) || (nTokCount < nToken) ) - return -1; - else if( nLen > 0 ) - return nIndex+(pCharStr-pOrgCharStr)+1; - else return -1; - } - else - { - newFromStr_WithLength( ppThis, pCharStrStart, pCharStr-pCharStrStart ); - if ( nLen ) - return nIndex+(pCharStr-pOrgCharStr)+1; - else - return -1; - } + new_(ppThis); + return -1; } namespace detail |