Skip to content

Commit

Permalink
[GR-24152] OptionsEncoder fails on a String value larger than 65535 b…
Browse files Browse the repository at this point in the history
…ytes.

PullRequest: graal/6452
  • Loading branch information
tzezula committed Jun 12, 2020
2 parents 182cfc4 + f3a86d6 commit 6f4b2b8
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright (c) 2019, 2020, 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
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package org.graalvm.util.test;

import java.lang.annotation.ElementType;
import static org.graalvm.util.OptionsEncoder.decode;
import static org.graalvm.util.OptionsEncoder.encode;
import static org.junit.Assert.assertEquals;

import java.util.Collections;
import java.util.Map;
import org.junit.Test;

public class OptionsEncoderTest {

@Test
public void testSmallString() {
Map<String, Object> options = Collections.singletonMap("key", "smallString");
assertEquals(options, decode(encode(options)));
}

@Test
public void testLargeString() {
StringBuilder fillBuilder = new StringBuilder();
for (int i = 0; i < 1 << 8; i++) {
fillBuilder.append(' ');
}
String fill = fillBuilder.toString();
StringBuilder largeString = new StringBuilder();
for (int i = 0; i <= Character.MAX_VALUE >>> 8; i++) {
largeString.append(fill);
}
Map<String, Object> options = Collections.singletonMap("key", largeString.toString());
assertEquals(options, decode(encode(options)));
}

@Test
public void testEnum() {
Map<String, Object> options = Collections.singletonMap("key", ElementType.TYPE);
assertEquals(Collections.singletonMap("key", ElementType.TYPE.name()), decode(encode(options)));
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2020, 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
Expand Down Expand Up @@ -27,6 +27,7 @@
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

/**
* A stream that can read (trivial) values using their in-band data type information, intended for
Expand Down Expand Up @@ -72,7 +73,10 @@ public Object readTypedValue() throws IOException {
value = readDouble();
break;
case 'U':
value = readUTF();
int len = readInt();
byte[] bytes = new byte[len];
readFully(bytes);
value = new String(bytes, StandardCharsets.UTF_8);
break;
default:
throw new IOException("Unsupported type: " + Integer.toHexString(type));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2020, 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
Expand Down Expand Up @@ -27,6 +27,7 @@
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;

/**
* A stream that can write (trivial) values together with their data type, for use with
Expand Down Expand Up @@ -90,13 +91,18 @@ public void writeTypedValue(Object value) throws IOException {
this.writeByte('D');
this.writeDouble((Double) value);
} else if (valueClz == String.class) {
this.writeByte('U');
this.writeUTF((String) value);
writeStringValue((String) value);
} else if (valueClz.isEnum()) {
this.writeByte('U');
this.writeUTF(((Enum<?>) value).name());
writeStringValue(((Enum<?>) value).name());
} else {
throw new IllegalArgumentException(String.format("Unsupported type: Value: %s, Value type: %s", value, valueClz));
}
}

private void writeStringValue(String value) throws IOException {
this.writeByte('U');
byte[] bytes = value.getBytes(StandardCharsets.UTF_8);
this.writeInt(bytes.length);
this.write(bytes);
}
}

0 comments on commit 6f4b2b8

Please sign in to comment.