rspec/rules/S3078/java/rule.adoc

59 lines
1.3 KiB
Plaintext

== Why is this an issue?
Using compound operators as well as increments and decrements (and toggling, in the case of ``++boolean++``s) on primitive fields are not atomic operations. That is, they don't happen in a single step. For instance, when a ``++volatile++`` primitive field is incremented or decremented you run the risk of data loss if threads interleave in the steps of the update. Instead, use a guaranteed-atomic class such as ``++AtomicInteger++``, or synchronize the access.
=== Noncompliant code example
[source,java]
----
private volatile int count = 0;
private volatile boolean boo = false;
public void incrementCount() {
count++; // Noncompliant
}
public void toggleBoo(){
boo = !boo; // Noncompliant
}
----
=== Compliant solution
[source,java]
----
private AtomicInteger count = 0;
private boolean boo = false;
public void incrementCount() {
count.incrementAndGet();
}
public synchronized void toggleBoo() {
boo = !boo;
}
----
== Resources
* https://wiki.sei.cmu.edu/confluence/x/SjdGBQ[CERT, VNA02-J.] - Ensure that compound operations on shared variables are atomic
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::message.adoc[]
'''
== Comments And Links
(visible only on this page)
include::comments-and-links.adoc[]
endif::env-github,rspecator-view[]