Skip to content

Commit 92c389f

Browse files
author
digi-scrypt
committed
reject non-BMP code points in the Character type converter
1 parent 8cc73b2 commit 92c389f

2 files changed

Lines changed: 11 additions & 1 deletion

File tree

src/main/java/org/apache/commons/cli/TypeHandler.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,16 @@ public static FileInputStream openFile(final String string) throws ParseExceptio
230230
map.put(Integer.class, Integer::parseInt);
231231
map.put(Short.class, Short::parseShort);
232232
map.put(Byte.class, Byte::parseByte);
233-
map.put(Character.class, s -> s.startsWith("\\u") ? Character.toChars(Integer.parseInt(s.substring(2), HEX_RADIX))[0] : s.charAt(0));
233+
map.put(Character.class, s -> {
234+
if (s.startsWith("\\u")) {
235+
final int codePoint = Integer.parseInt(s.substring(2), HEX_RADIX);
236+
if (!Character.isBmpCodePoint(codePoint)) {
237+
throw new IllegalArgumentException("Code point U+" + Integer.toHexString(codePoint) + " does not fit in a char");
238+
}
239+
return (char) codePoint;
240+
}
241+
return s.charAt(0);
242+
});
234243
map.put(Double.class, Double::parseDouble);
235244
map.put(Float.class, Float::parseFloat);
236245
map.put(BigInteger.class, BigInteger::new);

src/test/java/org/apache/commons/cli/TypeHandlerTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ private static Stream<Arguments> createValueTestParameters() throws MalformedURL
134134
list.add(Arguments.of("5", Character.class, '5'));
135135
list.add(Arguments.of("5.5", Character.class, '5'));
136136
list.add(Arguments.of("\\u0124", Character.class, Character.toChars(0x0124)[0]));
137+
list.add(Arguments.of("\\u1F600", Character.class, ParseException.class));
137138

138139
list.add(Arguments.of("just-a-string", Double.class, ParseException.class));
139140
list.add(Arguments.of("5", Double.class, 5d));

0 commit comments

Comments
 (0)