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.
*