Skip to content

Commit ff66175

Browse files
committed
Merge pull request apache#681 from DaanHoogland/coverity-regressions
Coverity regressions per 10 aug 2015Not all are in here, the db upgrade code seems to stay the main pitfall. * pr/681: coverity 1315775: proper getting of networkLabel coverity 1315774: improvement of code to negate false positive Signed-off-by: Daan Hoogland <daan@onecht.net>
2 parents aa7f8e5 + 1ab3b96 commit ff66175

2 files changed

Lines changed: 20 additions & 29 deletions

File tree

engine/schema/src/com/cloud/upgrade/dao/Upgrade30xBase.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,15 @@ public abstract class Upgrade30xBase extends LegacyDbUpgrade {
3434
protected String getNetworkLabelFromConfig(Connection conn, String name) {
3535
String sql = "SELECT value FROM `cloud`.`configuration` where name = ?";
3636
String networkLabel = null;
37-
PreparedStatement pstmt = null;
38-
ResultSet rs = null;
39-
try {
40-
pstmt = conn.prepareStatement(sql);
37+
try (PreparedStatement pstmt = conn.prepareStatement(sql);) {
4138
pstmt.setString(1,name);
42-
rs = pstmt.executeQuery();
43-
if (rs.next()) {
44-
networkLabel = rs.getString(1);
39+
try (ResultSet rs = pstmt.executeQuery();) {
40+
if (rs.next()) {
41+
networkLabel = rs.getString(1);
42+
}
4543
}
4644
} catch (SQLException e) {
4745
throw new CloudRuntimeException("Unable to fetch network label from configuration", e);
48-
} finally {
49-
closeAutoCloseable(rs);
50-
closeAutoCloseable(pstmt);
5146
}
5247
return networkLabel;
5348
}

framework/db/src/com/cloud/utils/db/DbUtil.java

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -230,30 +230,26 @@ public static Class<?> getEntityBeanType(GenericDao<?, Long> dao) {
230230
}
231231

232232
public static boolean releaseGlobalLock(String name) {
233-
Connection conn = getConnectionForGlobalLocks(name, false);
234-
if (conn == null) {
235-
s_logger.error("Unable to acquire DB connection for global lock system");
236-
assert (false);
237-
return false;
238-
}
233+
try (Connection conn = getConnectionForGlobalLocks(name, false);) {
234+
if (conn == null) {
235+
s_logger.error("Unable to acquire DB connection for global lock system");
236+
assert (false);
237+
return false;
238+
}
239239

240-
PreparedStatement pstmt = null;
241-
ResultSet rs = null;
242-
try {
243-
pstmt = conn.prepareStatement("SELECT COALESCE(RELEASE_LOCK(?), 0)");
244-
pstmt.setString(1, name);
245-
rs = pstmt.executeQuery();
246-
if (rs != null && rs.first())
247-
return rs.getInt(1) > 0;
248-
s_logger.error("releaseGlobalLock:RELEASE_LOCK() returns unexpected result");
240+
try (PreparedStatement pstmt = conn.prepareStatement("SELECT COALESCE(RELEASE_LOCK(?), 0)");) {
241+
pstmt.setString(1, name);
242+
try (ResultSet rs = pstmt.executeQuery();) {
243+
if (rs != null && rs.first()) {
244+
return rs.getInt(1) > 0;
245+
}
246+
s_logger.error("releaseGlobalLock:RELEASE_LOCK() returns unexpected result");
247+
}
248+
}
249249
} catch (SQLException e) {
250250
s_logger.error("RELEASE_LOCK() throws exception ", e);
251251
} catch (Throwable e) {
252252
s_logger.error("RELEASE_LOCK() throws exception ", e);
253-
} finally {
254-
closeResultSet(rs);
255-
closeStatement(pstmt);
256-
closeConnection(conn);
257253
}
258254
return false;
259255
}

0 commit comments

Comments
 (0)