mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Jean-Noel Rouvignac
17.05.2015 1d88ff472c4e058356ccd8d9336959fa6c917b78
Fixed java.lang.NegativeArraySizeException in ByteString.toHexString().

ByteStringTestCase.java:
Added a test to reproduce the NegativeArraySizeException

ByteString.java, Base64.java:
Added fast paths that return interned string which can reduce memory pressure.
3 files modified
19 ■■■■ changed files
opendj-core/src/main/java/org/forgerock/opendj/ldap/Base64.java 6 ●●●● patch | view | raw | blame | history
opendj-core/src/main/java/org/forgerock/opendj/ldap/ByteString.java 11 ●●●●● patch | view | raw | blame | history
opendj-core/src/test/java/org/forgerock/opendj/ldap/ByteStringTestCase.java 2 ●●●●● patch | view | raw | blame | history
opendj-core/src/main/java/org/forgerock/opendj/ldap/Base64.java
@@ -22,7 +22,7 @@
 *
 *
 *      Copyright 2006-2009 Sun Microsystems, Inc.
 *      Portions copyright 2012 ForgeRock AS.
 *      Portions copyright 2012-2015 ForgeRock AS.
 */
package org.forgerock.opendj.ldap;
@@ -320,6 +320,10 @@
    static String encode(final ByteSequence bytes) {
        Reject.ifNull(bytes);
        if (bytes.isEmpty()) {
            return "";
        }
        final StringBuilder buffer = new StringBuilder(4 * bytes.length() / 3);
        int pos = 0;
opendj-core/src/main/java/org/forgerock/opendj/ldap/ByteString.java
@@ -22,7 +22,7 @@
 *
 *
 *      Copyright 2009-2010 Sun Microsystems, Inc.
 *      Portions copyright 2011-2014 ForgeRock AS
 *      Portions copyright 2011-2015 ForgeRock AS
 */
package org.forgerock.opendj.ldap;
@@ -432,6 +432,9 @@
     * @return The string representation of the byte array sub-sequence.
     */
    static String toString(final byte[] b, final int offset, final int length) {
        if (length == 0) {
            return "";
        }
        try {
            return new String(b, offset, length, "UTF-8");
        } catch (final UnsupportedEncodingException e) {
@@ -756,6 +759,9 @@
     *         using hexadecimal characters.
     */
    public String toHexString() {
        if (isEmpty()) {
            return "";
        }
        StringBuilder builder = new StringBuilder((length - 1) * 3 + 2);
        builder.append(StaticUtils.byteToHex(buffer[offset]));
        for (int i = 1; i < length; i++) {
@@ -773,6 +779,9 @@
     *         using percent + hexadecimal characters.
     */
    public String toPercentHexString() {
        if (isEmpty()) {
            return "";
        }
        StringBuilder builder = new StringBuilder(length * 3);
        for (int i = 0; i < length; i++) {
            builder.append('%');
opendj-core/src/test/java/org/forgerock/opendj/ldap/ByteStringTestCase.java
@@ -267,6 +267,8 @@
    public void testToHex() throws Exception {
        ByteString byteString = new ByteStringBuilder().append("org=example").toByteString();
        assertThat(byteString.toHexString()).isEqualTo("6F 72 67 3D 65 78 61 6D 70 6C 65");
        assertThat(ByteString.empty().toHexString()).isEqualTo("");
    }
    @Test