From 90fc7e18c7bf7d90bb87fc94d2fff98d7e3eb803 Mon Sep 17 00:00:00 2001 From: Koji Sekiguchi Date: Thu, 8 Oct 2009 13:34:43 +0000 Subject: [PATCH] LUCENE-1953: FastVectorHighlighter: small fragCharSize can cause StringIndexOutOfBoundsException git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@823170 13f79535-47bb-0310-9956-ffa450edef68 --- contrib/CHANGES.txt | 3 +++ .../vectorhighlight/SimpleFragListBuilder.java | 2 ++ .../SimpleFragListBuilderTest.java | 15 +++++++++++++++ 3 files changed, 20 insertions(+) diff --git a/contrib/CHANGES.txt b/contrib/CHANGES.txt index 6f6d4dcc415..ff8693e4eff 100644 --- a/contrib/CHANGES.txt +++ b/contrib/CHANGES.txt @@ -26,6 +26,9 @@ Bug fixes Iterator#hasNext method on exhausted streams. (Patrick Jungermann via Karl Wettin) + * LUCENE-1953: FastVectorHighlighter: small fragCharSize can cause + StringIndexOutOfBoundsException. (Koji Sekiguchi) + New features * LUCENE-1924: Added BalancedSegmentMergePolicy to contrib/misc, diff --git a/contrib/fast-vector-highlighter/src/java/org/apache/lucene/search/vectorhighlight/SimpleFragListBuilder.java b/contrib/fast-vector-highlighter/src/java/org/apache/lucene/search/vectorhighlight/SimpleFragListBuilder.java index d45ddb10857..089b42e739d 100644 --- a/contrib/fast-vector-highlighter/src/java/org/apache/lucene/search/vectorhighlight/SimpleFragListBuilder.java +++ b/contrib/fast-vector-highlighter/src/java/org/apache/lucene/search/vectorhighlight/SimpleFragListBuilder.java @@ -59,6 +59,8 @@ public class SimpleFragListBuilder implements FragListBuilder { int st = phraseInfo.getStartOffset() - MARGIN < startOffset ? startOffset : phraseInfo.getStartOffset() - MARGIN; int en = st + fragCharSize; + if( phraseInfo.getEndOffset() > en ) + en = phraseInfo.getEndOffset(); startOffset = en; while( true ){ diff --git a/contrib/fast-vector-highlighter/src/test/org/apache/lucene/search/vectorhighlight/SimpleFragListBuilderTest.java b/contrib/fast-vector-highlighter/src/test/org/apache/lucene/search/vectorhighlight/SimpleFragListBuilderTest.java index d5951467506..996dfed2e01 100644 --- a/contrib/fast-vector-highlighter/src/test/org/apache/lucene/search/vectorhighlight/SimpleFragListBuilderTest.java +++ b/contrib/fast-vector-highlighter/src/test/org/apache/lucene/search/vectorhighlight/SimpleFragListBuilderTest.java @@ -37,6 +37,21 @@ public class SimpleFragListBuilderTest extends AbstractTestCase { } } + public void testSmallerFragSizeThanTermQuery() throws Exception { + SimpleFragListBuilder sflb = new SimpleFragListBuilder(); + FieldFragList ffl = sflb.createFieldFragList( fpl( "abcdefghijklmnopqrs", "abcdefghijklmnopqrs" ), SimpleFragListBuilder.MIN_FRAG_CHAR_SIZE ); + assertEquals( 1, ffl.fragInfos.size() ); + assertEquals( "subInfos=(abcdefghijklmnopqrs((0,19)))/1.0(0,19)", ffl.fragInfos.get( 0 ).toString() ); + } + + public void testSmallerFragSizeThanPhraseQuery() throws Exception { + SimpleFragListBuilder sflb = new SimpleFragListBuilder(); + FieldFragList ffl = sflb.createFieldFragList( fpl( "\"abcdefgh jklmnopqrs\"", "abcdefgh jklmnopqrs" ), SimpleFragListBuilder.MIN_FRAG_CHAR_SIZE ); + assertEquals( 1, ffl.fragInfos.size() ); + System.out.println( ffl.fragInfos.get( 0 ).toString() ); + assertEquals( "subInfos=(abcdefghjklmnopqrs((0,21)))/1.0(0,21)", ffl.fragInfos.get( 0 ).toString() ); + } + public void test1TermIndex() throws Exception { SimpleFragListBuilder sflb = new SimpleFragListBuilder(); FieldFragList ffl = sflb.createFieldFragList( fpl( "a", "a" ), 100 );