You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2022/07/07 12:30:05 UTC

[commons-text] 01/03: Throw OutOfMemoryError instead of NegativeArraySizeException

This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-text.git

commit 808ecca4261c8fef3d85f7f9419fb972127616eb
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Thu Jul 7 07:57:50 2022 -0400

    Throw OutOfMemoryError instead of NegativeArraySizeException
    
    A different solution to what was proposed in PR #132.
---
 src/changes/changes.xml                                       |  1 +
 src/main/java/org/apache/commons/text/TextStringBuilder.java  |  7 ++++---
 .../java/org/apache/commons/text/TextStringBuilderTest.java   | 11 +++++++++++
 3 files changed, 16 insertions(+), 3 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 7bd5a874..9196d521 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -69,6 +69,7 @@ The <action> type attribute can be add,update,fix,remove.
     <action issue="TEXT=212" type="fix" dev="kinow" due-to="Ali Ghanbari">A More Efficient Implementation for Calculating Size of Longest Common Subsequence.</action>
     <action issue="TEXT-209" type="fix" dev="kinow" due-to="fourAjeff">LookupTranslator returns count of chars consumed, not of codepoints consumed.</action>
     <action issue="TEXT-209" type="fix" dev="ggregory" due-to="Arturo Bernal">Use Math.min() call instead of doing it manually. #335.</action>
+    <action                  type="fix" dev="ggregory" due-to="ValentijnvdBeek, Gary Gregory">Throw OutOfMemoryError instead of NegativeArraySizeException.</action>
     <!-- ADD -->
     <action issue="TEXT-207" type="add" dev="mattjuntunen">Add DoubleFormat utility.</action>
     <action issue="TEXT-190" type="add" dev="kinow" due-to="Benjamin Bing">Document negative limit for WordUtils abbreviate method</action>
diff --git a/src/main/java/org/apache/commons/text/TextStringBuilder.java b/src/main/java/org/apache/commons/text/TextStringBuilder.java
index 4fafcf15..296c2262 100644
--- a/src/main/java/org/apache/commons/text/TextStringBuilder.java
+++ b/src/main/java/org/apache/commons/text/TextStringBuilder.java
@@ -325,7 +325,7 @@ public class TextStringBuilder implements CharSequence, Appendable, Serializable
     private int size;
 
     /**
-     * Constructs an empty builder initial capacity 32 characters.
+     * Constructs an empty builder with an initial capacity of 32 characters.
      */
     public TextStringBuilder() {
         this(CAPACITY);
@@ -1834,8 +1834,9 @@ public class TextStringBuilder implements CharSequence, Appendable, Serializable
      * @return this, to enable chaining
      */
     public TextStringBuilder ensureCapacity(final int capacity) {
-        if (capacity > buffer.length) {
-            reallocate(capacity * 2);
+        // checks for overflow
+        if (capacity > 0 && capacity - buffer.length > 0) {
+            reallocate(capacity);
         }
         return this;
     }
diff --git a/src/test/java/org/apache/commons/text/TextStringBuilderTest.java b/src/test/java/org/apache/commons/text/TextStringBuilderTest.java
index c6001851..6656ccf9 100644
--- a/src/test/java/org/apache/commons/text/TextStringBuilderTest.java
+++ b/src/test/java/org/apache/commons/text/TextStringBuilderTest.java
@@ -880,6 +880,17 @@ public class TextStringBuilderTest {
         assertTrue(sb.capacity() >= 40);
     }
 
+    @Test
+    public void testEnsureCapacityOutOfMemoryError() {
+        final TextStringBuilder sb = new TextStringBuilder();
+        // Should not be a NegativeArraySizeException
+        sb.ensureCapacity(Integer.MIN_VALUE);
+        sb.ensureCapacity(-1);
+        sb.ensureCapacity(0);
+        sb.ensureCapacity(Integer.MAX_VALUE / 2);
+        assertThrows(OutOfMemoryError.class, () -> sb.ensureCapacity(Integer.MAX_VALUE));
+    }
+
     @Test
     public void testEquals() {
         final TextStringBuilder sb1 = new TextStringBuilder(50);