8218227: StringBuilder/StringBuffer constructor throws confusing NegativeArraySizeException

Reviewed-by: rriggs
This commit is contained in:
Ivan Gerasimov 2019-02-05 17:05:40 -08:00
parent 621bf58687
commit 076d2267b6
5 changed files with 136 additions and 10 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -91,6 +91,19 @@ abstract class AbstractStringBuilder implements Appendable, CharSequence {
}
}
/**
* Creates an AbstractStringBuilder with the specified coder and with
* the initial capacity equal to the smaller of (capacity + addition)
* and Integer.MAX_VALUE.
*/
AbstractStringBuilder(byte coder, int capacity, int addition) {
this.coder = coder;
capacity = (capacity < Integer.MAX_VALUE - addition)
? capacity + addition : Integer.MAX_VALUE;
value = (coder == LATIN1)
? new byte[capacity] : StringUTF16.newBytesFor(capacity);
}
/**
* Compares the objects of two AbstractStringBuilder implementations lexicographically.
*

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 1994, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1994, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -148,7 +148,7 @@ import jdk.internal.HotSpotIntrinsicCandidate;
*/
@HotSpotIntrinsicCandidate
public StringBuffer(String str) {
super(str.length() + 16);
super(str.coder(), str.length(), 16);
append(str);
}
@ -166,7 +166,7 @@ import jdk.internal.HotSpotIntrinsicCandidate;
* @since 1.5
*/
public StringBuffer(CharSequence seq) {
this(seq.length() + 16);
super(String.LATIN1, seq.length(), 16);
append(seq);
}

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -121,7 +121,7 @@ public final class StringBuilder
*/
@HotSpotIntrinsicCandidate
public StringBuilder(String str) {
super(str.length() + 16);
super(str.coder(), str.length(), 16);
append(str);
}
@ -134,7 +134,7 @@ public final class StringBuilder
* @param seq the sequence to copy.
*/
public StringBuilder(CharSequence seq) {
this(seq.length() + 16);
super(String.LATIN1, seq.length(), 16);
append(seq);
}