10000 AT_STALE_THREAD_WRITE_OF_PRIMITIVE false positive on enum switches · Issue #3316 · spotbugs/spotbugs · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
AT_STALE_THREAD_WRITE_OF_PRIMITIVE false positive on enum switches #3316
Closed
@iloveeclipse

Description

@iloveeclipse

Regression in 4.9.0.
For the code below compiled with Java 21 Eclipse compiler, SpotBugs reports false positive:
Shared primitive variable "field" in one thread may not yield the value of the most recent write from another thread
with the AT_STALE_THREAD_WRITE_OF_PRIMITIVE warning.

public class MyClass  {
    private boolean field;

    public static enum MyEnum {
    }

    void method(MyEnum myEnum) {
        field = false;
        switch (myEnum) {
        default:
        }
    }

    public void print() {
        System.out.println(field);
    }
}

The analysis is wrong.

For the switch on enum value, ecj generates this field as volatile:
private static volatile synthetic int[] $SWITCH_TABLE$MyClass$MyEnum

Here, volatile modifier that is put by compiler on synthetic SWITCH_TABLE field (generated by compiler) should never qualify the class as potentially multi-threaded.

Fix is similar to #3306.

Synthetic fields shouldn't be considered for analysis.

I have a trivial patch that fixes the problem.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions

    0