Skip to content

Commit

Permalink
Fix issue spotbugs#79
Browse files Browse the repository at this point in the history
  • Loading branch information
zhouyinn committed Oct 20, 2020
1 parent a11965a commit 37fc524
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package edu.umd.cs.findbugs.detect;

import edu.umd.cs.findbugs.AbstractIntegrationTest;
import edu.umd.cs.findbugs.BugInstance;
import edu.umd.cs.findbugs.test.matcher.BugInstanceMatcher;
import edu.umd.cs.findbugs.test.matcher.BugInstanceMatcherBuilder;
import org.junit.Test;

import static edu.umd.cs.findbugs.test.CountMatcher.containsExactly;
import static org.junit.Assert.assertThat;

/**
* SpotBugs should remove the ResultSet obligation from all set
* when one occurrence of that type of obligation is Statement
* from all states.
*
* @see <a href="https://github.com/spotbugs/spotbugs/issues/79">GitHub issue</a>
* @since 4.1.3
*/
public class Issue79Test extends AbstractIntegrationTest {

@Test
public void test(){
performAnalysis("ghIssues/Issue79.class");
BugInstanceMatcher bugMatcher = new BugInstanceMatcherBuilder().build();
assertThat(getBugCollection(), containsExactly(0, bugMatcher));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,17 @@ public void edgeTransfer(Edge edge, StateSet fact) throws DataflowAnalysisExcept
+ edge.getSource().getLastInstruction());
}
fact.deleteObligation(comparedObligation, edge.getTarget().getLabel());

// closing a Statement closes the ResultSet
Obligation statement = database.getFactory().getObligationByName("java.sql.Statement");
if (comparedObligation.equals(statement)){
Obligation resultSet = database.getFactory().getObligationByName("java.sql.ResultSet");
fact.deleteObligation(resultSet, edge.getTarget().getLabel());
if (DEBUG_NULL_CHECK) {
System.out.println("Deleting " + resultSet.toString() + " on edge from comparison "
+ edge.getSource().getLastInstruction());
}
}
}
}
}
Expand Down
30 changes: 30 additions & 0 deletions spotbugsTestCases/src/java/ghIssues/Issue79.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package ghIssues;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Issue79 {
private static final String QUERY = "";

public void f(Connection cnx) throws SQLException {
PreparedStatement st = null;
ResultSet rs = null;
try {
st = cnx.prepareStatement(QUERY);
rs = st.executeQuery();
while (rs.next()) {
System.out.println(rs.getString("ID"));
}
} finally {
/*
* The statement closes the result set, and there is no scenario where st may be null
* but not the resultset, however an unsatisfied obligation is reported on the resultset
*/
if (st != null) {
st.close();
}
}
}
}

0 comments on commit 37fc524

Please sign in to comment.