Skip to content

Commit edd6437

Browse files
toddjonkershipilev
authored andcommitted
8319817: Charset constructor should make defensive copy of aliases
Backport-of: d6d7bdc7748c10963c3e58c0287b2472646bf36f
1 parent 590715f commit edd6437

File tree

2 files changed

+75
-2
lines changed

2 files changed

+75
-2
lines changed

src/java.base/share/classes/java/nio/charset/Charset.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
import java.util.Locale;
4343
import java.util.Map;
4444
import java.util.NoSuchElementException;
45-
import java.util.Objects;
4645
import java.util.ServiceConfigurationError;
4746
import java.util.ServiceLoader;
4847
import java.util.Set;
@@ -689,7 +688,12 @@ public static Charset defaultCharset() {
689688
* If the canonical name or any of the aliases are illegal
690689
*/
691690
protected Charset(String canonicalName, String[] aliases) {
692-
String[] as = Objects.requireNonNullElse(aliases, zeroAliases);
691+
String[] as =
692+
aliases == null ?
693+
zeroAliases :
694+
VM.isSystemDomainLoader(getClass().getClassLoader()) ?
695+
aliases :
696+
Arrays.copyOf(aliases, aliases.length);
693697

694698
// Skip checks for the standard, built-in Charsets we always load
695699
// during initialization.
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/* @test
25+
* @bug 8319817
26+
* @summary Check that aliases cannot be mutated
27+
* @run junit AliasesCopy
28+
*/
29+
30+
import java.nio.charset.Charset;
31+
import java.nio.charset.CharsetDecoder;
32+
import java.nio.charset.CharsetEncoder;
33+
import java.util.Set;
34+
35+
import org.junit.jupiter.api.Test;
36+
import static org.junit.jupiter.api.Assertions.assertIterableEquals;
37+
38+
public class AliasesCopy {
39+
private static final Set<String> ALIASES_SET = Set.of("foo-alias");
40+
private static final String[] ALIASES_ARRAY = ALIASES_SET.toArray(String[]::new);
41+
42+
@Test
43+
public void aliasesCopy() {
44+
final FooCharset cs = new FooCharset(ALIASES_ARRAY);
45+
ALIASES_ARRAY[0] = "bar-alias";
46+
assertIterableEquals(ALIASES_SET, cs.aliases());
47+
}
48+
49+
private static final class FooCharset extends Charset {
50+
private FooCharset(String[] aliases) {
51+
super("foo", aliases);
52+
}
53+
54+
@Override
55+
public CharsetEncoder newEncoder() {
56+
throw new RuntimeException("not implemented");
57+
}
58+
59+
@Override
60+
public CharsetDecoder newDecoder() {
61+
throw new RuntimeException("not implemented");
62+
}
63+
64+
@Override
65+
public boolean contains(Charset cs) {
66+
throw new RuntimeException("not implemented");
67+
}
68+
}
69+
}

0 commit comments

Comments
 (0)