A method is called on an object in a state where its type's protocol forbids it, such as a cipher before init or a result set after close

tx0/java:protocol-violationallerrorreliability/logicdepth fastbug

What it finds

cipher.doFinal(data) before cipher.init(...), rs.next() after rs.close(), a stream used again after a terminal operation, iterator.remove() twice in a row: the JDK throws IllegalStateException or SQLException on every such call.

The protocols of these types are written as finite automata in the model file models/protocols.txt (Cipher, Mac, Signature, the JDBC Connection, Statement, PreparedStatement and ResultSet, Stream, Iterator, file streams, readers and writers). Each object created in a method is followed through its paths; a call is reported when it is wrong in every state the object can be in there. Methods of the same file that the object is passed to are summarised first, bottom-up over the calls of the file, with the state they leave the object in or a violation every path through them runs into. An object stored in a field, passed to code nobody describes or captured by a lambda is no longer followed.

From the rule's source, protocol_violation.rs.

Example from the rule's tests
import java.sql.*;import java.util.*;import java.util.stream.*;import javax.crypto.Cipher;import org.apache.kafka.streams.kstream.KStream; class Uses {  byte[] early(byte[] data) throws Exception {    Cipher c = Cipher.getInstance("AES/GCM/NoPadding");    return c.doFinal(data);tx0 reports: doFinal is called on a Cipher in state New  }   byte[] fine(byte[] data, java.security.Key key) throws Exception {    Cipher c = Cipher.getInstance("AES/GCM/NoPadding");    c.init(Cipher.ENCRYPT_MODE, key);    return c.doFinal(data);  }   byte[] maybe(byte[] data, java.security.Key key, boolean ready) throws Exception {    Cipher c = Cipher.getInstance("AES/GCM/NoPadding");    if (ready) {      c.init(Cipher.ENCRYPT_MODE, key);    }    return c.doFinal(data);  }   int afterClose(Connection conn) throws SQLException {    Statement st = conn.createStatement();    ResultSet rs = st.executeQuery("select 1");    rs.close();    return rs.next() ? 1 : 0;tx0 reports: next is called on a ResultSet in state Closed  }   long reused(List<String> names) {    Stream<String> s = names.stream();    long n = s.count();    return n + s.filter(x -> x.isEmpty()).count();tx0 reports: filter is called on a Stream in state Used  }   void removeTwice(List<String> names) {    Iterator<String> it = names.iterator();    it.next();    it.remove();    it.remove();tx0 reports: remove is called on a Iterator in state Removed  }   void loop(List<String> names) {    Iterator<String> it = names.iterator();    while (it.hasNext()) {      if (it.next().isEmpty()) {        it.remove();      }    }  }   void closeIt(ResultSet rs) throws SQLException {    rs.close();  }   boolean throughHelper(Statement st) throws SQLException {    ResultSet rs = st.executeQuery("select 1");    closeIt(rs);    return rs.next();tx0 reports: next is called on a ResultSet in state Closed  }   void stored(Statement st) throws SQLException {    ResultSet rs = st.executeQuery("select 1");    keep(rs);    rs.close();    rs.next();tx0 reports: next is called  }   void topology(org.apache.kafka.streams.StreamsBuilder builder) {    KStream<String, String> events = builder.stream("events");    events.filter((k, v) -> v != null);    events.filter((k, v) -> v.isEmpty());  }   void keep(Object o) {  }}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs from the all tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/java:protocol-violation" = "error"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/java:protocol-violation] reason="..."

tx0 explain tx0/java:protocol-violation shows the same in the terminal. Found a false positive? Report it.