facebook / infer

A static analyzer for Java, C, C++, and Objective-C
http://fbinfer.com/
MIT License
14.8k stars 2k forks source link

[java] A false positive about the rule THREAD_SAFETY_VIOLATION #1793

Open LynnBroe opened 9 months ago

LynnBroe commented 9 months ago

Version. Infer version v1.1.0 OS. Ubuntu 22.04.3 LTS Command. infer run -- mvn clean compile

I found a false positive about the rule THREAD_SAFETY_VIOLATION.

According to the description of the rule, infer should not check the method annotated with @VisibleForTesting. In the example below, however, infer report a THREAD_SAFETY_VIOLATION warning at line 16

import javax.annotation.concurrent.ThreadSafe;
import android.support.annotation.VisibleForTesting;
@ThreadSafe
public class B {
    boolean mField;
    void writeUnderLockOk() {
        synchronized (this) {
            mField = true;
        }
    }
    @VisibleForTesting
    boolean readOutsideLock1Bad() {
        synchronized (this) {}
        return mField;
    }
}
warning: Thread Safety Violation Read/Write race. Non-private method `B.readOutsideLock1Bad()` reads without synchronization from `this.mField`. Potentially races with write in method `B.writeUnderLockOk()`.
Reporting because the current class is annotated `@ThreadSafe`, so we assume that this method can run in parallel with other non-private methods in the class (including itself).

Hence, I think it is a fasle positive.