Skip to content
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

ClassLoader.getResource can throw IllegalArgumentException #26574

Merged
merged 1 commit into from
Feb 25, 2021

Conversation

candrews
Copy link
Contributor

@candrews candrews commented Feb 20, 2021

ClassLoader.getResource can throw IllegalArgumentException if a malformed URL is provided to it.

According to its javadoc, resolveURL should return null if not resolvable, so catch the IllegalArgumentException and return null.

Example stack trace produced by new org.springframework.core.io.DefaultResourceLoader().getResource("C:/users/candrews/example").exists()

java.lang.IllegalArgumentException: name
        at java.base/jdk.internal.loader.URLClassPath$Loader.findResource(URLClassPath.java:600)
        at java.base/jdk.internal.loader.URLClassPath.findResource(URLClassPath.java:291)
        at java.base/java.net.URLClassLoader$2.run(URLClassLoader.java:655)
        at java.base/java.net.URLClassLoader$2.run(URLClassLoader.java:653)
        at java.base/java.security.AccessController.doPrivileged(Native Method)
        at java.base/java.net.URLClassLoader.findResource(URLClassLoader.java:652)
        at org.springframework.boot.loader.LaunchedURLClassLoader.findResource(LaunchedURLClassLoader.java:100)
        at java.base/java.lang.ClassLoader.getResource(ClassLoader.java:1401)
        at org.springframework.core.io.ClassPathResource.resolveURL(ClassPathResource.java:155)
        at org.springframework.core.io.ClassPathResource.exists(ClassPathResource.java:142)

Class.getResource, ClassLoader.getResource, and ClassLoader.getSystemResource will throw IllegalArgumentException if a malformed URL is provided to them.

According to its javadoc, resolveURL should return null if not resolvable, so catch the IllegalArgumentException and return null.
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Feb 20, 2021
@candrews
Copy link
Contributor Author

candrews commented Feb 20, 2021

I believe that this issue is caused by a bug in Java. Quoting the javadoc of java.lang.ClassLoader.getResource(String):

Returns:
URL object for reading the resource; null if the resource could not be found, a URL could not be constructed to locate the resource, the resource is in a package that is not opened unconditionally, or access to the resource is denied by the security manager.

In this case, "a URL could not be constructed to locate the resource" so null should be returned. But that's not what's happening.

I've reported this issue at https://bugreport.java.com/ (internal review ID : 9069151) and openjdk/jdk#2662

@jhoeller jhoeller self-assigned this Feb 22, 2021
@jhoeller jhoeller added in: core Issues in core modules (aop, beans, core, context, expression) type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Feb 22, 2021
@jhoeller jhoeller added this to the 5.3.5 milestone Feb 22, 2021
@jhoeller jhoeller changed the title getResource can throw IllegalArgumentException ClassLoader.getResource can throw IllegalArgumentException Feb 22, 2021
@spring-projects-issues spring-projects-issues added status: backported An issue that has been backported to maintenance branches and removed for: backport-to-5.2.x labels Feb 22, 2021
@jhoeller jhoeller merged commit dee12db into spring-projects:master Feb 25, 2021
jhoeller added a commit that referenced this pull request Feb 25, 2021
jhoeller added a commit that referenced this pull request Feb 25, 2021
Class.getResource, ClassLoader.getResource, and ClassLoader.getSystemResource will throw IllegalArgumentException if a malformed URL is provided to them.

According to its javadoc, resolveURL should return null if not resolvable, so catch the IllegalArgumentException and return null.

Closes gh-26574
This was referenced Mar 17, 2021
lxbzmy pushed a commit to lxbzmy/spring-framework that referenced this pull request Mar 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: backported An issue that has been backported to maintenance branches type: bug A general bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants