Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions bifromq-util/src/main/java/org/apache/bifromq/util/UTF8Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ public static boolean isWellFormed(String str, boolean sanityCheck) {
if (cl == '\u0000') {
return false;
}
if (Character.isLowSurrogate(cl)) {
return false;
}
if (sanityCheck && isUnacceptableChar(cl)) {
return false;
}
Expand All @@ -47,15 +50,19 @@ public static boolean isWellFormed(String str, boolean sanityCheck) {
if (cr == '\u0000') {
return false;
}
if (Character.isSurrogatePair(cl, cr)) {
if (Character.isHighSurrogate(cl)) {
if (!Character.isLowSurrogate(cr)) {
return false;
}
} else if (Character.isLowSurrogate(cr)) {
return false;
}
if (sanityCheck && isUnacceptableChar(cr)) {
return false;
}
cl = cr;
}
return true;
return !Character.isHighSurrogate(cl);
}

public static boolean isValidUTF8Payload(ByteBuffer payload) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ public void emptyClientId() {
@Test
public void mustNotChars() {
assertFalse(UTF8Util.isWellFormed("hello\u0000world", false)); // null character U+0000
assertFalse(UTF8Util.isWellFormed("hello\uD83D\uDE0Aworld", false)); // surrogate pairs
assertTrue(UTF8Util.isWellFormed("hello\uD83D\uDE0Aworld", false)); // valid surrogate pair
assertFalse(UTF8Util.isWellFormed("hello\uD83Dworld", false)); // unpaired high surrogate
assertFalse(UTF8Util.isWellFormed("hello\uDE0Aworld", false)); // unpaired low surrogate
assertFalse(UTF8Util.isWellFormed("hello\uD83D", false)); // trailing high surrogate
}

@Test
Expand Down