Apache Lucene open-source search software
SOLR-4275: Fix TrieTokenizer to no longer throw StringIndexOutOfBoundsException in admin UI / AnalysisRequestHandler when you enter no number to tokenize
Uwe Schindler981694a9100c
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex 6148df8cde9..4a1bfc715b1 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -466,6 +466,10 @@ Bug Fixes* SOLR-3829: Admin UI Logging events broken if schema.xml defines a catch-alldynamicField with type ignored (steffkes)+* SOLR-4275: Fix TrieTokenizer to no longer throw StringIndexOutOfBoundsException+ in admin UI / AnalysisRequestHandler when you enter no number to tokenize.+ (Uwe Schindler)+Other Changes----------------------diff --git a/solr/core/src/java/org/apache/solr/analysis/TrieTokenizerFactory.java b/solr/core/src/java/org/apache/solr/analysis/TrieTokenizerFactory.javaindex 04317e4b9f8..7d49f9b45a2 100644--- a/solr/core/src/java/org/apache/solr/analysis/TrieTokenizerFactory.java+++ b/solr/core/src/java/org/apache/solr/analysis/TrieTokenizerFactory.java@@ -50,39 +50,54 @@ public class TrieTokenizerFactory extends TokenizerFactory {@Overridepublic TrieTokenizer create(Reader input) {- return new TrieTokenizer(input, type, precisionStep, TrieTokenizer.getNumericTokenStream(precisionStep));+ return new TrieTokenizer(input, type, TrieTokenizer.getNumericTokenStream(precisionStep));}}final class TrieTokenizer extends Tokenizer {protected static final DateField dateField = new DateField();- protected final int precisionStep;protected final TrieTypes type;protected final NumericTokenStream ts;protected final OffsetAttribute ofsAtt = addAttribute(OffsetAttribute.class);protected int startOfs, endOfs;+ protected boolean hasValue;+ protected final char[] buf = new char[32];static NumericTokenStream getNumericTokenStream(int precisionStep) {return new NumericTokenStream(precisionStep);}- public TrieTokenizer(Reader input, TrieTypes type, int precisionStep, NumericTokenStream ts) {+ public TrieTokenizer(Reader input, TrieTypes type, NumericTokenStream ts) {// must share the attribute source with the NumericTokenStream we delegate tosuper(ts, input);this.type = type;- this.precisionStep = precisionStep;this.ts = ts;}@Overridepublic void reset() {try {- char[] buf = new char[32];- int len = input.read(buf);+ int upto = 0;+ while (upto < buf.length) {+ final int length = input.read(buf, upto, buf.length - upto);+ if (length == -1) break;+ upto += length;+ }+ // skip remaining data if buffer was too short:+ if (upto == buf.length) {+ input.skip(Long.MAX_VALUE);+ }+this.startOfs = correctOffset(0);- this.endOfs = correctOffset(len);- String v = new String(buf, 0, len);+ this.endOfs = correctOffset(upto);++ if (upto == 0) {+ hasValue = false;+ return;+ }++ final String v = new String(buf, 0, upto);try {switch (type) {case INTEGER:@@ -107,21 +122,24 @@ final class TrieTokenizer extends Tokenizer {throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,"Invalid Number: " + v);}+ hasValue = true;+ ts.reset();} catch (IOException e) {throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Unable to create TrieIndexTokenizer", e);}- ts.reset();}@Overridepublic void close() throws IOException {super.close();- ts.close();+ if (hasValue) {+ ts.close();+ }}@Overridepublic boolean incrementToken() {- if (ts.incrementToken()) {+ if (hasValue && ts.incrementToken()) {ofsAtt.setOffset(startOfs, endOfs);return true;}@@ -130,7 +148,9 @@ final class TrieTokenizer extends Tokenizer {@Overridepublic void end() throws IOException {- ts.end();+ if (hasValue) {+ ts.end();+ }ofsAtt.setOffset(endOfs, endOfs);}}