This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-email.git

commit 12353c94f1c7c370a5639db4c85593cc1999e8a3
Author: Gary Gregory <[email protected]>
AuthorDate: Sun Dec 10 14:38:26 2023 -0500

    Avoid possible NullPointerException in
    DataSourceClassPathResolver.resolve(String, boolean)
---
 src/changes/changes.xml                            |  1 +
 .../mail/resolver/DataSourceClassPathResolver.java | 35 +++++++++++-----------
 2 files changed, 19 insertions(+), 17 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 12130d8..bbef085 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -44,6 +44,7 @@
       <action type="fix" dev="ggregory" due-to="Alexander 
Lehmann">Email.setBounceAddress no longer accepts invalid email 
addresses.</action>
       <action type="fix" dev="ggregory" due-to="Amir Behnam, Michael 
Osipov">Throw more specific exceptions in MimeMessageParser #11.</action>
       <action type="fix" dev="ggregory" due-to="Gary 
Gregory">Email.setMailSession(Session) throws the more precise exception 
NullPointerException.</action>
+      <action type="fix" dev="ggregory" due-to="Gary Gregory">Avoid possible 
NullPointerException in DataSourceClassPathResolver.resolve(String, 
boolean).</action>
       <!-- ADD -->
       <action type="add" due-to="Dependabot" dev="ggregory">
         Add github/codeql-action #75.
diff --git 
a/src/main/java/org/apache/commons/mail/resolver/DataSourceClassPathResolver.java
 
b/src/main/java/org/apache/commons/mail/resolver/DataSourceClassPathResolver.java
index b85a994..7a302c2 100644
--- 
a/src/main/java/org/apache/commons/mail/resolver/DataSourceClassPathResolver.java
+++ 
b/src/main/java/org/apache/commons/mail/resolver/DataSourceClassPathResolver.java
@@ -22,6 +22,7 @@ import javax.mail.util.ByteArrayDataSource;
 
 import java.io.IOException;
 import java.io.InputStream;
+import java.net.URL;
 
 /**
  * Creates a {@code DataSource} based on an class path.
@@ -82,35 +83,35 @@ public class DataSourceClassPathResolver extends 
DataSourceBaseResolver
 
     /** {@inheritDoc} */
     @Override
-    public DataSource resolve(final String resourceLocation, final boolean 
isLenient) throws IOException
-    {
-        try
-        {
-            if (!isCid(resourceLocation) && !isHttpUrl(resourceLocation))
-            {
+    public DataSource resolve(final String resourceLocation, final boolean 
isLenient) throws IOException {
+        try {
+            if (!isCid(resourceLocation) && !isHttpUrl(resourceLocation)) {
                 final String mimeType = 
FileTypeMap.getDefaultFileTypeMap().getContentType(resourceLocation);
                 final String resourceName = getResourceName(resourceLocation);
-                try (InputStream is = 
DataSourceClassPathResolver.class.getResourceAsStream(resourceName)){
-                    if (is == null) {
-                        if (isLenient)
-                        {
+                try (InputStream inputStream = 
DataSourceClassPathResolver.class.getResourceAsStream(resourceName)) {
+                    if (inputStream == null) {
+                        if (isLenient) {
                             return null;
                         }
                         throw new IOException("The following class path 
resource was not found : " + resourceLocation);
                     }
-                    final ByteArrayDataSource ds = new ByteArrayDataSource(is, 
mimeType);
+                    final ByteArrayDataSource ds = new 
ByteArrayDataSource(inputStream, mimeType);
                     // EMAIL-125: set the name of the DataSource to the 
normalized resource URL
                     // similar to other DataSource implementations, e.g. 
FileDataSource, URLDataSource
-                    
ds.setName(DataSourceClassPathResolver.class.getResource(resourceName).toString());
+                    final URL resource = 
DataSourceClassPathResolver.class.getResource(resourceName);
+                    if (resource != null) {
+                        ds.setName(resource.toString());
+                    } else if (isLenient) {
+                        return null;
+                    } else {
+                        throw new IOException("The following class path 
resource was not found : " + resourceName);
+                    }
                     return ds;
                 }
             }
             return null;
-        }
-        catch (final IOException e)
-        {
-            if (isLenient)
-            {
+        } catch (final IOException e) {
+            if (isLenient) {
                 return null;
             }
             throw e;

Reply via email to