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

remm pushed a commit to branch 10.1.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git


The following commit(s) were added to refs/heads/10.1.x by this push:
     new 868d28dfa8 Fix possible NPE
868d28dfa8 is described below

commit 868d28dfa8fe6508fe08f4233238559cd00a8b52
Author: remm <r...@apache.org>
AuthorDate: Mon Sep 11 11:31:27 2023 +0200

    Fix possible NPE
    
    Found by coverity.
---
 java/org/apache/catalina/realm/DataSourceRealm.java    | 9 ++++++++-
 java/org/apache/catalina/realm/LocalStrings.properties | 1 +
 2 files changed, 9 insertions(+), 1 deletion(-)

diff --git a/java/org/apache/catalina/realm/DataSourceRealm.java 
b/java/org/apache/catalina/realm/DataSourceRealm.java
index 14b690ceee..d97ae3b126 100644
--- a/java/org/apache/catalina/realm/DataSourceRealm.java
+++ b/java/org/apache/catalina/realm/DataSourceRealm.java
@@ -28,6 +28,7 @@ import javax.naming.Context;
 import javax.sql.DataSource;
 
 import org.apache.catalina.LifecycleException;
+import org.apache.catalina.Server;
 import org.apache.naming.ContextBindings;
 
 /**
@@ -366,7 +367,13 @@ public class DataSourceRealm extends RealmBase {
                 context = ContextBindings.getClassLoader();
                 context = (Context) context.lookup("comp/env");
             } else {
-                context = getServer().getGlobalNamingContext();
+                Server server = getServer();
+                if (server == null) {
+                    connectionSuccess = false;
+                    
containerLog.error(sm.getString("dataSourceRealm.noNamingContext"));
+                    return null;
+                }
+                context = server.getGlobalNamingContext();
             }
             DataSource dataSource = (DataSource) 
context.lookup(dataSourceName);
             Connection connection = dataSource.getConnection();
diff --git a/java/org/apache/catalina/realm/LocalStrings.properties 
b/java/org/apache/catalina/realm/LocalStrings.properties
index bd993e00dd..0e80360222 100644
--- a/java/org/apache/catalina/realm/LocalStrings.properties
+++ b/java/org/apache/catalina/realm/LocalStrings.properties
@@ -32,6 +32,7 @@ dataSourceRealm.commit=Exception committing connection before 
closing
 dataSourceRealm.exception=Exception performing authentication
 dataSourceRealm.getPassword.exception=Exception retrieving password for [{0}]
 dataSourceRealm.getRoles.exception=Exception retrieving roles for [{0}]
+dataSourceRealm.noNamingContext=Cannot access the server to retrieve the 
naming context
 
 jaasCallback.username=Returned username [{0}]
 


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org
For additional commands, e-mail: dev-h...@tomcat.apache.org

Reply via email to