Skip to content

Commit

Permalink
Fix potential resource leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
markt-asf committed Jul 4, 2023
1 parent 2eae3f7 commit 3ca1ca2
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 30 deletions.
58 changes: 28 additions & 30 deletions java/org/apache/catalina/users/DataSourceUserDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -465,27 +465,26 @@ public Iterator<Group> getGroups() {
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 {
Expand All @@ -506,27 +505,26 @@ public Iterator<Role> getRoles() {
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 {
Expand Down
8 changes: 8 additions & 0 deletions webapps/docs/changelog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@
issues do not "pop up" wrt. others).
-->
<section name="Tomcat 11.0.0-M10 (markt)" 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 11.0.0-M9 (markt)" rtext="release in progress">
<subsection name="Other">
Expand Down

0 comments on commit 3ca1ca2

Please sign in to comment.