Skip to content

Commit

Permalink
Comments to clarify, also align skipOverFieldSignature
Browse files Browse the repository at this point in the history
  • Loading branch information
liach committed Nov 1, 2024
1 parent 79d4dd1 commit 4325e94
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -174,92 +174,102 @@ public static ClassDesc parseReferenceTypeDesc(String descriptor) {
}

/**
* Validates the correctness of a binary class name. In particular checks for the presence of
* invalid characters in the name.
* Validates the correctness of a binary class name.
* In particular checks for the presence of invalid characters, empty
* name, consecutive, leading, or trailing {@code .}.
*
* @param name the class name
* @return the class name passed if valid
* @throws IllegalArgumentException if the class name is invalid
* @throws NullPointerException if class name is {@code null}
*/
public static String validateBinaryClassName(String name) {
// state variable for detection of illegal states, such as:
// empty unqualified name, consecutive, leading, or trailing separators
int afterSeparator = 0;
int len = name.length();
for (int i = 0; i < len; i++) {
char ch = name.charAt(i);
// reject ';' or '[' or other form's separator
if (ch == ';' || ch == '[' || ch == '/')
throw invalidClassName(name);
if (ch == '.') {
if (i == afterSeparator) {
// illegal state when received separator indicates consecutive
// or leading separators
if (i == afterSeparator)
throw invalidClassName(name);
} else {
afterSeparator = i + 1;
}
afterSeparator = i + 1;
}
}
// reject empty unqualified name or trailing separators
if (len == afterSeparator)
throw invalidClassName(name);
return name;
}

/**
* Validates the correctness of an internal class name.
* In particular checks for the presence of invalid characters in the name.
* In particular checks for the presence of invalid characters, empty
* name, consecutive, leading, or trailing {@code /}.
*
* @param name the class name
* @return the class name passed if valid
* @throws IllegalArgumentException if the class name is invalid
* @throws NullPointerException if class name is {@code null}
*/
public static String validateInternalClassName(String name) {
// state variable for detection of illegal states, such as:
// empty unqualified name, consecutive, leading, or trailing separators
int afterSeparator = 0;
int len = name.length();
for (int i = 0; i < len; i++) {
char ch = name.charAt(i);
// reject ';' or '[' or other form's separator
if (ch == ';' || ch == '[' || ch == '.')
throw invalidClassName(name);
if (ch == '/') {
if (i == afterSeparator) {
// illegal state when received separator indicates consecutive
// or leading separators
if (i == afterSeparator)
throw invalidClassName(name);
} else {
afterSeparator = i + 1;
}
afterSeparator = i + 1;
}
}
// reject empty unqualified name or trailing separators
if (len == afterSeparator)
throw invalidClassName(name);
return name;
}

/**
* Validates the correctness of a binary package name.
* In particular checks for the presence of invalid characters in the name.
* Empty package name is allowed.
* In particular checks for the presence of invalid characters, consecutive,
* leading, or trailing {@code .}. Allows empty strings for the unnamed package.
*
* @param name the package name
* @return the package name passed if valid
* @throws IllegalArgumentException if the package name is invalid
* @throws NullPointerException if the package name is {@code null}
*/
public static String validateBinaryPackageName(String name) {
// Empty names are explicitly allowed
// the unnamed package + null check
if (name.isEmpty())
return name;
return validateBinaryClassName(name);
}

/**
* Validates the correctness of an internal package name.
* In particular checks for the presence of invalid characters in the name.
* Empty package name is allowed.
* In particular checks for the presence of invalid characters, consecutive,
* leading, or trailing {@code /}. Allows empty strings for the unnamed package.
*
* @param name the package name
* @return the package name passed if valid
* @throws IllegalArgumentException if the package name is invalid
* @throws NullPointerException if the package name is {@code null}
*/
public static String validateInternalPackageName(String name) {
// Empty names are explicitly allowed
// the unnamed package + null check
if (name.isEmpty())
return name;
return validateInternalClassName(name);
Expand Down Expand Up @@ -441,24 +451,20 @@ static int skipOverFieldSignature(String descriptor, int start, int end) {
case JVM_SIGNATURE_CLASS:
// state variable for detection of illegal states, such as:
// empty unqualified name, '//', leading '/', or trailing '/'
boolean legal = false;
int afterSeparator = index + 1; // start of internal name
while (index < end) {
switch (descriptor.charAt(index++)) {
case ';' -> {
// illegal state on parser exit indicates empty unqualified name or trailing '/'
return legal ? index - start : 0;
}
case '.', '[' -> {
// do not permit '.' or '['
ch = descriptor.charAt(index++);
if (ch == ';')
// reject empty unqualified name or trailing '/'
return index == afterSeparator ? 0 : index - start;
// reject '.' or '['
if (ch == '[' || ch == '.')
return 0;
if (ch == '/') {
// illegal state when received '/' indicates '//' or leading '/'
if (index == afterSeparator)
return 0;
}
case '/' -> {
// illegal state when received '/' indicates '//' or leading '/'
if (!legal) return 0;
legal = false;
}
default ->
legal = true;
afterSeparator = index + 1;
}
}
break;
Expand Down
3 changes: 2 additions & 1 deletion test/jdk/java/lang/constant/ClassDescTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,8 @@ public void testArrayClassDesc() throws ReflectiveOperationException {
public void testBadClassDescs() {
List<String> badDescriptors = List.of("II", "I;", "Q", "L", "",
"java.lang.String", "[]", "Ljava/lang/String",
"Ljava.lang.String;", "java/lang/String");
"Ljava.lang.String;", "java/lang/String", "L;",
"La//b;", "L/a;", "La/;");

for (String d : badDescriptors) {
try {
Expand Down

0 comments on commit 4325e94

Please sign in to comment.