From ea6d687b610debdaf312f7db137080eb39145802 Mon Sep 17 00:00:00 2001 From: Tatu Saloranta Date: Wed, 24 Sep 2014 20:18:47 -0700 Subject: [PATCH] Backport fix #152 to 2.3 branch --- release-notes/VERSION | 5 +++-- .../fasterxml/jackson/core/util/TextBuffer.java | 7 +++++-- .../jackson/core/util/TestTextBuffer.java | 15 +++++++++++++++ 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/release-notes/VERSION b/release-notes/VERSION index 7dee39fe50..ee5ee91ae1 100644 --- a/release-notes/VERSION +++ b/release-notes/VERSION @@ -1,12 +1,13 @@ Project: jackson-core -Version: 2.3.4 (17-Jul-2014) +Version: 2.3.5 (xx-xxx-2014) -No changes. +#152: Exception for property names longer than 256k ------------------------------------------------------------------------ === History: === ------------------------------------------------------------------------ +2.3.4 (17-Jul-2014) 2.3.3 (10-Apr-2014) No changes since 2.3.2. diff --git a/src/main/java/com/fasterxml/jackson/core/util/TextBuffer.java b/src/main/java/com/fasterxml/jackson/core/util/TextBuffer.java index 9ba08e5f8c..dde3a87162 100644 --- a/src/main/java/com/fasterxml/jackson/core/util/TextBuffer.java +++ b/src/main/java/com/fasterxml/jackson/core/util/TextBuffer.java @@ -575,8 +575,11 @@ public char[] expandCurrentSegment() final char[] curr = _currentSegment; // Let's grow by 50% final int len = curr.length; - // Must grow by at least 1 char, no matter what - int newLen = (len == MAX_SEGMENT_LEN) ? (MAX_SEGMENT_LEN+1) : Math.min(MAX_SEGMENT_LEN, len + (len >> 1)); + int newLen = len + (len >> 1); + // but above intended maximum, slow to increase by 25% + if (newLen > MAX_SEGMENT_LEN) { + newLen = len + (len >> 2); + } return (_currentSegment = ArraysCompat.copyOf(curr, newLen)); } diff --git a/src/test/java/com/fasterxml/jackson/core/util/TestTextBuffer.java b/src/test/java/com/fasterxml/jackson/core/util/TestTextBuffer.java index c4be4cd09f..543a38675d 100644 --- a/src/test/java/com/fasterxml/jackson/core/util/TestTextBuffer.java +++ b/src/test/java/com/fasterxml/jackson/core/util/TestTextBuffer.java @@ -62,4 +62,19 @@ public void testLongAppend() assertEquals(len+2, tb.size()); assertEquals(EXP, tb.contentsAsString()); } + + // [Core#152] + public void testExpand() + { + TextBuffer tb = new TextBuffer(new BufferRecycler()); + char[] buf = tb.getCurrentSegment(); + + while (buf.length < 500 * 1000) { + char[] old = buf; + buf = tb.expandCurrentSegment(); + if (old.length >= buf.length) { + fail("Expected buffer of "+old.length+" to expand, did not, length now "+buf.length); + } + } + } }