A setter is synchronized but the matching getter is not, so readers may see stale values
What it finds
A synchronized setter guards the write, but a getter that does not synchronize on the same monitor has no happens-before edge with it: another thread may read the old value, or for long and double half of the new one, as the Java memory model allows.
Reported is a getter getX() or isX() that is not synchronized, in a class whose setX with one parameter is, unless the getter reads a volatile field.
From the rule's source, unsynchronized_getter.rs.
class Account { private long balance; private String owner; synchronized void setBalance(long balance) { this.balance = balance; } long getBalance() {tx0 reports: setBalance is synchronized but this getter is not return balance; } synchronized void setOwner(String owner) { this.owner = owner; } synchronized String getOwner() { return owner; }} Turn it on, off or suppress it
The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:
[rules]
"tx0/java:unsynchronized-getter" = "warning"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:unsynchronized-getter] reason="..."tx0 explain tx0/java:unsynchronized-getter shows the same in the terminal. Found a false positive? Report it.