fix: ArrayIndexOutOfBoundsException with multiple bytes for a character #32

This commit is contained in:
Fabio Lima 2024-01-02 18:27:49 -03:00
parent fef740668b
commit 733242f980
2 changed files with 21 additions and 7 deletions

View File

@ -793,6 +793,16 @@ public final class Ulid implements Serializable, Comparable<Ulid> {
return false; // null or wrong size!
}
for (int i = 0; i < chars.length; i++) {
try {
if (ALPHABET_VALUES[chars[i]] == -1) {
return false; // invalid character!
}
} catch (ArrayIndexOutOfBoundsException e) {
return false; // Multibyte character!
}
}
// The time component has 48 bits.
// The base32 encoded time component has 50 bits.
// The time component cannot be greater than than 2^48-1.
@ -805,12 +815,6 @@ public final class Ulid implements Serializable, Comparable<Ulid> {
return false; // time overflow!
}
for (int i = 0; i < chars.length; i++) {
if (ALPHABET_VALUES[chars[i]] == -1) {
return false; // invalid character!
}
}
return true; // It seems to be OK.
}
}

View File

@ -104,7 +104,7 @@ public class UlidTest extends UlidFactoryTest {
}
@Test
public void testFromStrings() {
public void testFromString() {
Random random = new Random();
for (int i = 0; i < DEFAULT_LOOP_MAX; i++) {
final long msb = random.nextLong();
@ -610,6 +610,16 @@ public class UlidTest extends UlidFactoryTest {
} catch (IllegalArgumentException e) {
// success
}
// https://github.com/f4b6a3/ulid-creator/issues/32
// https://www.compart.com/en/unicode/U+3617 (: whisper; to whistle)
ulid = "0123456789ABCDEFGHJKMNPQR\u3617";
try {
Ulid.toCharArray(ulid);
fail("Should throw an exception");
} catch (IllegalArgumentException e) {
// success
}
}
@Test