regex result offsets can be negative if a group was not matched, tdf#94810

"(abc)|(def)" matches "def" with result offset sequences
{0,-1,0},{3,-1,3}

And thus the assert() calls introduced with
4cf1d290ba and
ce91f3c129 were hit.

Change-Id: I571b6c7d47349a2cc7b1d1e34193b2865012a49f
This commit is contained in:
Eike Rathke
2015-11-25 15:58:01 +01:00
parent 4e88b2a787
commit 4dd2d40673

View File

@@ -301,14 +301,17 @@ SearchResult TextSearch::searchForward( const OUString& searchStr, sal_Int32 sta
for ( sal_Int32 k = 0; k < nGroups; k++ ) for ( sal_Int32 k = 0; k < nGroups; k++ )
{ {
const sal_Int32 nStart = sres.startOffset[k] - nExtraOffset; const sal_Int32 nStart = sres.startOffset[k] - nExtraOffset;
assert(nStart >= 0); // Result offsets are negative (-1) if a group expression was
// not matched.
if (nStart >= 0)
sres.startOffset[k] = (nStart < nOffsets ? offset[nStart] : (offset[nOffsets - 1] + 1)); sres.startOffset[k] = (nStart < nOffsets ? offset[nStart] : (offset[nOffsets - 1] + 1));
// JP 20.6.2001: end is ever exclusive and then don't return // JP 20.6.2001: end is ever exclusive and then don't return
// the position of the next character - return the // the position of the next character - return the
// next position behind the last found character! // next position behind the last found character!
// "a b c" find "b" must return 2,3 and not 2,4!!! // "a b c" find "b" must return 2,3 and not 2,4!!!
const sal_Int32 nStop = sres.endOffset[k] - nExtraOffset; const sal_Int32 nStop = sres.endOffset[k] - nExtraOffset;
assert(nStop >= 0); if (nStop >= 0)
{
if (nStop > 0) if (nStop > 0)
sres.endOffset[k] = offset[(nStop <= nOffsets ? nStop : nOffsets) - 1] + 1; sres.endOffset[k] = offset[(nStop <= nOffsets ? nStop : nOffsets) - 1] + 1;
else else
@@ -316,6 +319,7 @@ SearchResult TextSearch::searchForward( const OUString& searchStr, sal_Int32 sta
} }
} }
} }
}
else else
{ {
sres = (this->*fnForward)( in_str, startPos, endPos ); sres = (this->*fnForward)( in_str, startPos, endPos );
@@ -404,17 +408,21 @@ SearchResult TextSearch::searchBackward( const OUString& searchStr, sal_Int32 st
for ( sal_Int32 k = 0; k < nGroups; k++ ) for ( sal_Int32 k = 0; k < nGroups; k++ )
{ {
const sal_Int32 nStart = sres.startOffset[k]; const sal_Int32 nStart = sres.startOffset[k];
assert(nStart >= 0); // Result offsets are negative (-1) if a group expression was
// not matched.
if (nStart >= 0)
{
if (nStart > 0) if (nStart > 0)
sres.startOffset[k] = offset[(nStart <= nOffsets ? nStart : nOffsets) - 1] + 1; sres.startOffset[k] = offset[(nStart <= nOffsets ? nStart : nOffsets) - 1] + 1;
else else
sres.startOffset[k] = offset[0]; sres.startOffset[k] = offset[0];
}
// JP 20.6.2001: end is ever exclusive and then don't return // JP 20.6.2001: end is ever exclusive and then don't return
// the position of the next character - return the // the position of the next character - return the
// next position behind the last found character! // next position behind the last found character!
// "a b c" find "b" must return 2,3 and not 2,4!!! // "a b c" find "b" must return 2,3 and not 2,4!!!
const sal_Int32 nStop = sres.endOffset[k]; const sal_Int32 nStop = sres.endOffset[k];
assert(nStop >= 0); if (nStop >= 0)
sres.endOffset[k] = (nStop < nOffsets ? offset[nStop] : (offset[nOffsets - 1] + 1)); sres.endOffset[k] = (nStop < nOffsets ? offset[nStop] : (offset[nOffsets - 1] + 1));
} }
} }