-
Notifications
You must be signed in to change notification settings - Fork 6.2k
8328874: Class::forName0 should validate the class name length early #26802
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 6 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
95db25d
8328874: Class::forName0 should validate the class name length early
hgqxjj 4265fab
Merge remote-tracking branch 'upstream/master' into 8328874
hgqxjj 79fe33e
move check into java side
hgqxjj b3237eb
Update Class.java
hgqxjj 8eb1d4c
Update Class.java
hgqxjj 27971bf
Update Class.java
hgqxjj cb3e6a7
Merge remote-tracking branch 'upstream/master' into 8328874
hgqxjj 9ed4248
add regression test
hgqxjj 9e2d763
a small fix
hgqxjj a6b669e
change copyright year
hgqxjj e7e23d8
Update Class.java
hgqxjj eb1537c
Update Class.java
hgqxjj 7d8df51
Update Class.java
hgqxjj 9c580f0
Update Class.java
hgqxjj 7810b9b
Merge remote-tracking branch 'upstream/master' into 8328874
hgqxjj edc1694
move common method into a common file.
hgqxjj a8e8415
Merge remote-tracking branch 'upstream/master' into 8328874
hgqxjj c01a6d5
Optimize implementation
hgqxjj 2ca6354
Use a different native method for testNative, since the implementatio…
hgqxjj acc85bd
Update Class.java
hgqxjj File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -92,6 +92,7 @@ | |
| import sun.reflect.generics.scope.ClassScope; | ||
| import sun.reflect.annotation.*; | ||
|
|
||
| import static jdk.internal.util.ModifiedUtf.utfLen; | ||
| /** | ||
| * Instances of the class {@code Class} represent classes and | ||
| * interfaces in a running Java application. An enum class and a record | ||
|
|
@@ -222,6 +223,7 @@ public final class Class<T> implements java.io.Serializable, | |
| private static final int ANNOTATION= 0x00002000; | ||
| private static final int ENUM = 0x00004000; | ||
| private static final int SYNTHETIC = 0x00001000; | ||
| private static final int JAVA_CLASSNAME_MAX_LEN = 65535; | ||
|
||
|
|
||
| private static native void registerNatives(); | ||
| static { | ||
|
|
@@ -467,6 +469,7 @@ public static Class<?> forName(String className) | |
| @CallerSensitiveAdapter | ||
| private static Class<?> forName(String className, Class<?> caller) | ||
| throws ClassNotFoundException { | ||
| validateClassNameLength(className); | ||
| ClassLoader loader = (caller == null) ? ClassLoader.getSystemClassLoader() | ||
| : ClassLoader.getClassLoader(caller); | ||
| return forName0(className, true, loader, caller); | ||
|
|
@@ -549,6 +552,7 @@ private static Class<?> forName(String className, Class<?> caller) | |
| public static Class<?> forName(String name, boolean initialize, ClassLoader loader) | ||
| throws ClassNotFoundException | ||
| { | ||
| validateClassNameLength(name); | ||
| return forName0(name, initialize, loader, null); | ||
| } | ||
|
|
||
|
|
@@ -597,7 +601,9 @@ private static native Class<?> forName0(String name, boolean initialize, | |
| */ | ||
| public static Class<?> forName(Module module, String name) { | ||
| Objects.requireNonNull(module); | ||
| Objects.requireNonNull(name); | ||
| if (!classNameLengthIsValid(name)) { | ||
| return null; | ||
| } | ||
|
|
||
| ClassLoader cl = module.getClassLoader(); | ||
| if (cl != null) { | ||
|
|
@@ -4148,4 +4154,25 @@ int getClassFileVersion() { | |
| int getClassFileAccessFlags() { | ||
| return classFileAccessFlags; | ||
| } | ||
|
|
||
| // Checks whether the class name exceeds the maximum allowed length. | ||
| private static boolean classNameLengthIsValid(String name) { | ||
| Objects.requireNonNull(name); | ||
hgqxjj marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // Quick approximation: each char can be at most 3 bytes in Modified UTF-8. | ||
| // If the string is short enough, it definitely fits. | ||
| if (name.length() * 3 <= JAVA_CLASSNAME_MAX_LEN) { | ||
hgqxjj marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return true; | ||
| } | ||
| // Compute exact Modified UTF-8 length. | ||
| return utfLen(name, 0) <= JAVA_CLASSNAME_MAX_LEN; | ||
| } | ||
|
|
||
| // Validates the length of the class name and throws an exception if it exceeds the maximum allowed length. | ||
| private static void validateClassNameLength(String name) throws ClassNotFoundException { | ||
| if (!classNameLengthIsValid(name)) { | ||
| throw new ClassNotFoundException( | ||
| "Class name exceeds maximum length of " + JAVA_CLASSNAME_MAX_LEN); | ||
| } | ||
| } | ||
|
|
||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.