You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2026/08/12 15:43:30 UTC

(commons-cli) branch master updated: Reject UTF-16 surrogate values in Character converter (#438)

This is an automated email from the ASF dual-hosted git repository.

garydgregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-cli.git


The following commit(s) were added to refs/heads/master by this push:
     new de0bd57b Reject UTF-16 surrogate values in Character converter (#438)
de0bd57b is described below

commit de0bd57bd92b261152af254907f1d129869a6acf
Author: Naveed Khan <16...@users.noreply.github.com>
AuthorDate: Wed Aug 12 15:43:24 2026 +0000

    Reject UTF-16 surrogate values in Character converter (#438)
---
 src/main/java/org/apache/commons/cli/TypeHandler.java     | 3 +++
 src/test/java/org/apache/commons/cli/TypeHandlerTest.java | 2 ++
 2 files changed, 5 insertions(+)

diff --git a/src/main/java/org/apache/commons/cli/TypeHandler.java b/src/main/java/org/apache/commons/cli/TypeHandler.java
index d889dfcf..4709c781 100644
--- a/src/main/java/org/apache/commons/cli/TypeHandler.java
+++ b/src/main/java/org/apache/commons/cli/TypeHandler.java
@@ -236,6 +236,9 @@ public class TypeHandler {
                 if (!Character.isBmpCodePoint(codePoint)) {
                     throw new IllegalArgumentException("Code point U+" + Integer.toHexString(codePoint) + " does not fit in a char");
                 }
+                if (Character.isSurrogate((char) codePoint)) {
+                    throw new IllegalArgumentException("Code point U+" + Integer.toHexString(codePoint) + " is a UTF-16 surrogate");
+                }
                 return (char) codePoint;
             }
             return s.charAt(0);
diff --git a/src/test/java/org/apache/commons/cli/TypeHandlerTest.java b/src/test/java/org/apache/commons/cli/TypeHandlerTest.java
index 8b6b2182..fe14877a 100644
--- a/src/test/java/org/apache/commons/cli/TypeHandlerTest.java
+++ b/src/test/java/org/apache/commons/cli/TypeHandlerTest.java
@@ -135,6 +135,8 @@ class TypeHandlerTest {
         list.add(Arguments.of("5.5", Character.class, '5'));
         list.add(Arguments.of("\\u0124", Character.class, Character.toChars(0x0124)[0]));
         list.add(Arguments.of("\\u1F600", Character.class, ParseException.class));
+        list.add(Arguments.of("\\uD800", Character.class, ParseException.class));
+        list.add(Arguments.of("\\uDFFF", Character.class, ParseException.class));
 
         list.add(Arguments.of("just-a-string", Double.class, ParseException.class));
         list.add(Arguments.of("5", Double.class, 5d));