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

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


The following commit(s) were added to refs/heads/9.0.x by this push:
     new e8820039fe Fix potential resource leaks
e8820039fe is described below

commit e8820039fecb7af5c5762ef19c1c756618bae368
Author: Mark Thomas <ma...@apache.org>
AuthorDate: Tue Jul 4 20:21:11 2023 +0100

    Fix potential resource leaks
---
 .../catalina/users/DataSourceUserDatabase.java     | 58 +++++++++++-----------
 webapps/docs/changelog.xml                         |  8 +++
 2 files changed, 36 insertions(+), 30 deletions(-)

diff --git a/java/org/apache/catalina/users/DataSourceUserDatabase.java 
b/java/org/apache/catalina/users/DataSourceUserDatabase.java
index 47a7a4a42e..d79082d758 100644
--- a/java/org/apache/catalina/users/DataSourceUserDatabase.java
+++ b/java/org/apache/catalina/users/DataSourceUserDatabase.java
@@ -465,27 +465,26 @@ public class DataSourceUserDatabase extends 
SparseUserDatabase {
                 groups.putAll(createdGroups);
                 groups.putAll(modifiedGroups);
 
-                Connection dbConnection = openConnection();
-                if (dbConnection != null && preparedAllGroups != null) {
-                    try (PreparedStatement stmt = 
dbConnection.prepareStatement(preparedAllGroups)) {
-                        try (ResultSet rs = stmt.executeQuery()) {
-                            while (rs.next()) {
-                                String groupName = rs.getString(1);
-                                if (groupName != null) {
-                                    if (!groups.containsKey(groupName) && 
!removedGroups.containsKey(groupName)) {
-                                        Group group = 
findGroupInternal(dbConnection, groupName);
-                                        if (group != null) {
-                                            groups.put(groupName, group);
+                try (Connection dbConnection = openConnection()) {
+                    if (dbConnection != null && preparedAllGroups != null) {
+                        try (PreparedStatement stmt = 
dbConnection.prepareStatement(preparedAllGroups)) {
+                            try (ResultSet rs = stmt.executeQuery()) {
+                                while (rs.next()) {
+                                    String groupName = rs.getString(1);
+                                    if (groupName != null) {
+                                        if (!groups.containsKey(groupName) && 
!removedGroups.containsKey(groupName)) {
+                                            Group group = 
findGroupInternal(dbConnection, groupName);
+                                            if (group != null) {
+                                                groups.put(groupName, group);
+                                            }
                                         }
                                     }
                                 }
                             }
                         }
-                    } catch (SQLException e) {
-                        
log.error(sm.getString("dataSourceUserDatabase.exception"), e);
-                    } finally {
-                        closeConnection(dbConnection);
                     }
+                } catch (SQLException e) {
+                    
log.error(sm.getString("dataSourceUserDatabase.exception"), e);
                 }
                 return groups.values().iterator();
             } finally {
@@ -506,27 +505,26 @@ public class DataSourceUserDatabase extends 
SparseUserDatabase {
                 roles.putAll(createdRoles);
                 roles.putAll(modifiedRoles);
 
-                Connection dbConnection = openConnection();
-                if (dbConnection != null && preparedAllRoles != null) {
-                    try (PreparedStatement stmt = 
dbConnection.prepareStatement(preparedAllRoles)) {
-                        try (ResultSet rs = stmt.executeQuery()) {
-                            while (rs.next()) {
-                                String roleName = rs.getString(1);
-                                if (roleName != null) {
-                                    if (!roles.containsKey(roleName) && 
!removedRoles.containsKey(roleName)) {
-                                        Role role = 
findRoleInternal(dbConnection, roleName);
-                                        if (role != null) {
-                                            roles.put(roleName, role);
+                try (Connection dbConnection = openConnection()) {
+                    if (dbConnection != null && preparedAllRoles != null) {
+                        try (PreparedStatement stmt = 
dbConnection.prepareStatement(preparedAllRoles)) {
+                            try (ResultSet rs = stmt.executeQuery()) {
+                                while (rs.next()) {
+                                    String roleName = rs.getString(1);
+                                    if (roleName != null) {
+                                        if (!roles.containsKey(roleName) && 
!removedRoles.containsKey(roleName)) {
+                                            Role role = 
findRoleInternal(dbConnection, roleName);
+                                            if (role != null) {
+                                                roles.put(roleName, role);
+                                            }
                                         }
                                     }
                                 }
                             }
                         }
-                    } catch (SQLException e) {
-                        
log.error(sm.getString("dataSourceUserDatabase.exception"), e);
-                    } finally {
-                        closeConnection(dbConnection);
                     }
+                } catch (SQLException e) {
+                    
log.error(sm.getString("dataSourceUserDatabase.exception"), e);
                 }
                 return roles.values().iterator();
             } finally {
diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml
index fa38ca4889..9fd25f1703 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -105,6 +105,14 @@
   issues do not "pop up" wrt. others).
 -->
 <section name="Tomcat 9.0.79 (remm)" rtext="in development">
+  <subsection name="Catalina">
+    <changelog>
+      <fix>
+        Fix potential database connection leaks in
+        <code>DataSourceUserDatabase</code> identified by Coverity Scan. 
(markt)
+      </fix>
+    </changelog>
+  </subsection>
 </section>
 <section name="Tomcat 9.0.78 (remm)" rtext="release in progress">
   <subsection name="Other">


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

Reply via email to