JDK-6992121 : StringBuilder.ensureCapacity(int minCap) throws OutOfMemoryError with minCap=Integer.MIN_VALUE
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 7
  • Priority: P2
  • Status: Resolved
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2010-10-14
  • Updated: 2014-04-01
  • Resolved: 2010-11-11
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 7
7 b118Fixed
Related Reports
Duplicate :  
Relates :  
Relates :  
Description
java.lang.StringBuilder.ensureCapacity(int minCap) method throws OutOfMemoryError Exception with the minCap value near to the Integer.MIN_VALUE.

See the following simple code:

public class TestEnsureCapacity {

    public static void main(String[] args) {
        StringBuilder sb = new StringBuffer("abc");
        int cap = sb.capacity();
        sb.ensureCapacity(Integer.MIN_VALUE);
    }

}

The Exception message will be thrown:

Exception in thread "main" java.lang.OutOfMemoryError
        at java.lang.AbstractStringBuilder.expandCapacity(AbstractStringBuilder.java:125)
        at java.lang.AbstractStringBuilder.ensureCapacityInternal(AbstractStringBuilder.java:112)
        at java.lang.AbstractStringBuilder.ensureCapacity(AbstractStringBuilder.java:103)
        at java.lang.StringBuilder.ensureCapacity(StringBuilder.java:72)
        at TestEnsureCapacity.main(TestEnsureCapacity.java:6)

In that time JDK specification say nothing about possible exceptions for the java.lang.StringBuilder.ensureCapacity method: If the minimumCapacity argument is nonpositive, this method takes no action and simply returns.

This is regression since JDK 7 b94.

Comments
EVALUATION http://hg.openjdk.java.net/jdk7/build/jdk/rev/c6320457db65
04-12-2010

EVALUATION Changeset URL: http://hg.openjdk.java.net/jdk7/tl/jdk/rev/c6320457db65
19-10-2010

EVALUATION The changes for 6933217 intentionally do this check: if (minimumCapacity - value.length > 0) instead of if (minimumCapacity > value.length) to handle the overflow case (where mininumCapacity is negative value). But it fails to handle the case where ensureCapacity is a public API that effectively accepts negative numbers (i.e. the impacted classes are StringBuffer, StringBuilder, ArrayList, Vector). Fix the public ensureCapacity method to return if minimumCapacity is negative.
18-10-2010

SUGGESTED FIX diff -r 1b430727f00d src/share/classes/java/lang/AbstractStringBuilder.java --- a/src/share/classes/java/lang/AbstractStringBuilder.java Tue Oct 12 17:05:28 2010 -0700 +++ b/src/share/classes/java/lang/AbstractStringBuilder.java Thu Oct 14 18:08:03 2010 +0100 @@ -108,7 +108,7 @@ abstract class AbstractStringBuilder imp * never synchronized. */ private void ensureCapacityInternal(int minimumCapacity) { - if (minimumCapacity - value.length > 0) + if (minimumCapacity > value.length) expandCapacity(minimumCapacity); } We also need to re-examine j.u.ArrayList and j.u.Vector in case they have the same issue.
14-10-2010

EVALUATION The changes for 6933217 means that the minimumCapacity parameter is no longer checked as before.
14-10-2010