Apache Lucene open-source search software
LUCENE-7538: throw IllegalArgumentException if you attempt to store a too-massive text field
Mike McCandless2902727a1570
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex f64e9e8986c..191c103387c 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -70,6 +70,10 @@ Improvements* LUCENE-7544: UnifiedHighlighter: add extension points for handling custom queries.(Michael Braun, David Smiley)+* LUCENE-7538: Asking IndexWriter to store a too-massive text field+ now throws IllegalArgumentException instead of a cryptic exception+ that closes your IndexWriter (Steve Chen via Mike McCandless)+======================= Lucene 6.3.0 =======================API Changesdiff --git a/lucene/core/src/java/org/apache/lucene/codecs/compressing/GrowableByteArrayDataOutput.java b/lucene/core/src/java/org/apache/lucene/codecs/compressing/GrowableByteArrayDataOutput.javaindex 67cfab6c7fb..ec551d14d1f 100644--- a/lucene/core/src/java/org/apache/lucene/codecs/compressing/GrowableByteArrayDataOutput.java+++ b/lucene/core/src/java/org/apache/lucene/codecs/compressing/GrowableByteArrayDataOutput.java@@ -64,7 +64,7 @@ public final class GrowableByteArrayDataOutput extends DataOutput {@Overridepublic void writeString(String string) throws IOException {- int maxLen = string.length() * UnicodeUtil.MAX_UTF8_BYTES_PER_CHAR;+ int maxLen = UnicodeUtil.maxUTF8Length(string.length());if (maxLen <= MIN_UTF8_SIZE_TO_ENABLE_DOUBLE_PASS_ENCODING) {// string is small enough that we don't need to save memory by falling back to double-pass approach// this is just an optimized writeString() that re-uses scratchBytes.diff --git a/lucene/core/src/java/org/apache/lucene/index/DefaultIndexingChain.java b/lucene/core/src/java/org/apache/lucene/index/DefaultIndexingChain.javaindex e2ece543506..15de682dec4 100644--- a/lucene/core/src/java/org/apache/lucene/index/DefaultIndexingChain.java+++ b/lucene/core/src/java/org/apache/lucene/index/DefaultIndexingChain.java@@ -430,6 +430,10 @@ final class DefaultIndexingChain extends DocConsumer {fp = getOrAddField(fieldName, fieldType, false);}if (fieldType.stored()) {+ String value = field.stringValue();+ if (value != null && value.length() > IndexWriter.MAX_STORED_STRING_LENGTH) {+ throw new IllegalArgumentException("stored field \"" + field.name() + "\" is too large (" + value.length() + " characters) to store");+ }try {storedFieldsWriter.writeField(fp.fieldInfo, field);} catch (Throwable th) {diff --git a/lucene/core/src/java/org/apache/lucene/index/IndexWriter.java b/lucene/core/src/java/org/apache/lucene/index/IndexWriter.javaindex 4517294f4c3..68f3b3b6b2d 100644--- a/lucene/core/src/java/org/apache/lucene/index/IndexWriter.java+++ b/lucene/core/src/java/org/apache/lucene/index/IndexWriter.java@@ -62,6 +62,7 @@ import org.apache.lucene.store.MergeInfo;import org.apache.lucene.store.RateLimitedIndexOutput;import org.apache.lucene.store.TrackingDirectoryWrapper;import org.apache.lucene.util.Accountable;+import org.apache.lucene.util.ArrayUtil;import org.apache.lucene.util.Bits;import org.apache.lucene.util.BytesRef;import org.apache.lucene.util.CloseableThreadLocal;@@ -70,6 +71,7 @@ import org.apache.lucene.util.IOUtils;import org.apache.lucene.util.InfoStream;import org.apache.lucene.util.StringHelper;import org.apache.lucene.util.ThreadInterruptedException;+import org.apache.lucene.util.UnicodeUtil;import org.apache.lucene.util.Version;/**@@ -258,6 +260,12 @@ public class IndexWriter implements Closeable, TwoPhaseCommit, Accountable {* IndexWriterConfig#setInfoStream(InfoStream)}).*/public final static int MAX_TERM_LENGTH = DocumentsWriterPerThread.MAX_TERM_LENGTH_UTF8;++ /**+ * Maximum length string for a stored field.+ */+ public final static int MAX_STORED_STRING_LENGTH = ArrayUtil.MAX_ARRAY_LENGTH / UnicodeUtil.MAX_UTF8_BYTES_PER_CHAR;+// when unrecoverable disaster strikes, we populate this with the reason that we had to close IndexWritervolatile Throwable tragedy;diff --git a/lucene/core/src/java/org/apache/lucene/util/BytesRef.java b/lucene/core/src/java/org/apache/lucene/util/BytesRef.javaindex c62d6391aea..2fcf28ad99f 100644--- a/lucene/core/src/java/org/apache/lucene/util/BytesRef.java+++ b/lucene/core/src/java/org/apache/lucene/util/BytesRef.java@@ -84,7 +84,7 @@ public final class BytesRef implements Comparable<BytesRef>,Cloneable {* unicode text, with no unpaired surrogates.*/public BytesRef(CharSequence text) {- this(new byte[UnicodeUtil.MAX_UTF8_BYTES_PER_CHAR * text.length()]);+ this(new byte[UnicodeUtil.maxUTF8Length(text.length())]);length = UnicodeUtil.UTF16toUTF8(text, 0, text.length(), bytes);}diff --git a/lucene/core/src/java/org/apache/lucene/util/BytesRefBuilder.java b/lucene/core/src/java/org/apache/lucene/util/BytesRefBuilder.javaindex 2bfa2f2dc70..08fda910a55 100644--- a/lucene/core/src/java/org/apache/lucene/util/BytesRefBuilder.java+++ b/lucene/core/src/java/org/apache/lucene/util/BytesRefBuilder.java@@ -143,7 +143,7 @@ public class BytesRefBuilder {* represent the provided text.*/public void copyChars(CharSequence text, int off, int len) {- grow(len * UnicodeUtil.MAX_UTF8_BYTES_PER_CHAR);+ grow(UnicodeUtil.maxUTF8Length(len));ref.length = UnicodeUtil.UTF16toUTF8(text, off, len, ref.bytes);}@@ -152,7 +152,7 @@ public class BytesRefBuilder {* represent the provided text.*/public void copyChars(char[] text, int off, int len) {- grow(len * UnicodeUtil.MAX_UTF8_BYTES_PER_CHAR);+ grow(UnicodeUtil.maxUTF8Length(len));ref.length = UnicodeUtil.UTF16toUTF8(text, off, len, ref.bytes);}diff --git a/lucene/core/src/java/org/apache/lucene/util/UnicodeUtil.java b/lucene/core/src/java/org/apache/lucene/util/UnicodeUtil.javaindex a21281f8a0a..20e6249f2a3 100644--- a/lucene/core/src/java/org/apache/lucene/util/UnicodeUtil.java+++ b/lucene/core/src/java/org/apache/lucene/util/UnicodeUtil.java@@ -612,6 +612,11 @@ public final class UnicodeUtil {}return out_offset;}++ /** Returns the maximum number of utf8 bytes required to encode a utf16 (e.g., java char[], String) */+ public static int maxUTF8Length(int utf16Length) {+ return Math.multiplyExact(utf16Length, MAX_UTF8_BYTES_PER_CHAR);+ }/*** Utility method for {@link #UTF8toUTF16(byte[], int, int, char[])}diff --git a/lucene/core/src/test/org/apache/lucene/codecs/compressing/TestGrowableByteArrayDataOutput.java b/lucene/core/src/test/org/apache/lucene/codecs/compressing/TestGrowableByteArrayDataOutput.javaindex 3820733ecca..37a7e4c5822 100644--- a/lucene/core/src/test/org/apache/lucene/codecs/compressing/TestGrowableByteArrayDataOutput.java+++ b/lucene/core/src/test/org/apache/lucene/codecs/compressing/TestGrowableByteArrayDataOutput.java@@ -37,7 +37,7 @@ public class TestGrowableByteArrayDataOutput extends LuceneTestCase {// create a small string such that the single pass approach is usedint length = TestUtil.nextInt(random(), 1, minSizeForDoublePass - 1);String unicode = TestUtil.randomFixedByteLengthUnicodeString(random(), length);- byte[] utf8 = new byte[unicode.length() * UnicodeUtil.MAX_UTF8_BYTES_PER_CHAR];+ byte[] utf8 = new byte[UnicodeUtil.maxUTF8Length(unicode.length())];int len = UnicodeUtil.UTF16toUTF8(unicode, 0, unicode.length(), utf8);GrowableByteArrayDataOutput dataOutput = new GrowableByteArrayDataOutput(1 << 8);@@ -61,7 +61,7 @@ public class TestGrowableByteArrayDataOutput extends LuceneTestCase {int num = atLeast(100);for (int i = 0; i < num; i++) {String unicode = TestUtil.randomRealisticUnicodeString(random(), minSizeForDoublePass, 10 * minSizeForDoublePass);- byte[] utf8 = new byte[unicode.length() * UnicodeUtil.MAX_UTF8_BYTES_PER_CHAR];+ byte[] utf8 = new byte[UnicodeUtil.maxUTF8Length(unicode.length())];int len = UnicodeUtil.UTF16toUTF8(unicode, 0, unicode.length(), utf8);GrowableByteArrayDataOutput dataOutput = new GrowableByteArrayDataOutput(1 << 8);diff --git a/lucene/core/src/test/org/apache/lucene/index/TestIndexWriter.java b/lucene/core/src/test/org/apache/lucene/index/TestIndexWriter.javaindex 6f112a7f748..a99576305a1 100644--- a/lucene/core/src/test/org/apache/lucene/index/TestIndexWriter.java+++ b/lucene/core/src/test/org/apache/lucene/index/TestIndexWriter.java@@ -97,6 +97,7 @@ import org.apache.lucene.util.ThreadInterruptedException;import org.apache.lucene.util.automaton.Automata;import org.apache.lucene.util.automaton.Automaton;import org.apache.lucene.util.automaton.CharacterRunAutomaton;+import org.junit.Ignore;import org.junit.Test;public class TestIndexWriter extends LuceneTestCase {@@ -2768,5 +2769,34 @@ public class TestIndexWriter extends LuceneTestCase {dir.close();}+ @Ignore("requires running tests with biggish heap")+ public void testMassiveField() throws Exception {+ Directory dir = newDirectory();+ IndexWriterConfig iwc = new IndexWriterConfig(new MockAnalyzer(random()));+ final IndexWriter w = new IndexWriter(dir, iwc);++ StringBuilder b = new StringBuilder();+ while (b.length() <= IndexWriter.MAX_STORED_STRING_LENGTH) {+ b.append("x ");+ }++ final Document doc = new Document();+ //doc.add(new TextField("big", b.toString(), Field.Store.YES));+ doc.add(new StoredField("big", b.toString()));+ Exception e = expectThrows(IllegalArgumentException.class, () -> {w.addDocument(doc);});+ assertEquals("stored field \"big\" is too large (" + b.length() + " characters) to store", e.getMessage());++ // make sure writer is still usable:+ Document doc2 = new Document();+ doc2.add(new StringField("id", "foo", Field.Store.YES));+ w.addDocument(doc2);++ DirectoryReader r = DirectoryReader.open(w);+ assertEquals(1, r.numDocs());+ r.close();+ w.close();+ dir.close();+ }+}diff --git a/lucene/core/src/test/org/apache/lucene/util/TestUnicodeUtil.java b/lucene/core/src/test/org/apache/lucene/util/TestUnicodeUtil.javaindex 7bbd606e4e0..15251ad9b5e 100644--- a/lucene/core/src/test/org/apache/lucene/util/TestUnicodeUtil.java+++ b/lucene/core/src/test/org/apache/lucene/util/TestUnicodeUtil.java@@ -111,7 +111,7 @@ public class TestUnicodeUtil extends LuceneTestCase {int num = atLeast(50000);for (int i = 0; i < num; i++) {final String s = TestUtil.randomUnicodeString(random());- final byte[] utf8 = new byte[s.length() * UnicodeUtil.MAX_UTF8_BYTES_PER_CHAR];+ final byte[] utf8 = new byte[UnicodeUtil.maxUTF8Length(s.length())];final int utf8Len = UnicodeUtil.UTF16toUTF8(s, 0, s.length(), utf8);assertEquals(s.codePointCount(0, s.length()),UnicodeUtil.codePointCount(new BytesRef(utf8, 0, utf8Len)));@@ -137,7 +137,7 @@ public class TestUnicodeUtil extends LuceneTestCase {int num = atLeast(50000);for (int i = 0; i < num; i++) {final String s = TestUtil.randomUnicodeString(random());- final byte[] utf8 = new byte[s.length() * UnicodeUtil.MAX_UTF8_BYTES_PER_CHAR];+ final byte[] utf8 = new byte[UnicodeUtil.maxUTF8Length(s.length())];final int utf8Len = UnicodeUtil.UTF16toUTF8(s, 0, s.length(), utf8);utf32 = ArrayUtil.grow(utf32, utf8Len);final int utf32Len = UnicodeUtil.UTF8toUTF32(new BytesRef(utf8, 0, utf8Len), utf32);@@ -208,7 +208,7 @@ public class TestUnicodeUtil extends LuceneTestCase {int num = atLeast(5000);for (int i = 0; i < num; i++) {String unicode = TestUtil.randomUnicodeString(random());- byte[] utf8 = new byte[unicode.length() * UnicodeUtil.MAX_UTF8_BYTES_PER_CHAR];+ byte[] utf8 = new byte[UnicodeUtil.maxUTF8Length(unicode.length())];int len = UnicodeUtil.UTF16toUTF8(unicode, 0, unicode.length(), utf8);assertEquals(len, UnicodeUtil.calcUTF16toUTF8Length(unicode, 0, unicode.length()));}diff --git a/lucene/core/src/test/org/apache/lucene/util/automaton/TestUTF32ToUTF8.java b/lucene/core/src/test/org/apache/lucene/util/automaton/TestUTF32ToUTF8.javaindex 6434c1cde76..1c1d1d419da 100644--- a/lucene/core/src/test/org/apache/lucene/util/automaton/TestUTF32ToUTF8.java+++ b/lucene/core/src/test/org/apache/lucene/util/automaton/TestUTF32ToUTF8.java@@ -41,7 +41,7 @@ public class TestUTF32ToUTF8 extends LuceneTestCase {private boolean matches(ByteRunAutomaton a, int code) {char[] chars = Character.toChars(code);- byte[] b = new byte[UnicodeUtil.MAX_UTF8_BYTES_PER_CHAR * chars.length];+ byte[] b = new byte[UnicodeUtil.maxUTF8Length(chars.length)];final int len = UnicodeUtil.UTF16toUTF8(chars, 0, chars.length, b);return a.run(b, 0, len);}