+
Skip to content

Double check when working with multithreading. SAST #40630

@LM4O322

Description

@LM4O322

Description

Another one for you, guys

It looks like the usage of double check will not work reliably in a platform independent way when implemented in Java.

Writes that initialize the object and the write to the helper field can be done or perceived out of order.

Even if the compiler does not reorder those writes, on a multiprocessor the processor or the memory system may reorder those writes, as perceived by a thread running on another processor.

Discussion

No response

Motivation

No response

Details

I think it's worth reviewing the following double check blocks:

if (converter == null) {
synchronized (this) {
if (converter == null) {

2nd:

if (shouldBootstrap == null) {
synchronized (this) {
if (shouldBootstrap == null) {

3rd:

if (provider == null) {
synchronized (this) {
if (provider == null) {

4th:

if (converter == null) {
synchronized (this) {
if (converter == null) {

Suggest using volatile variables.

JDK5 and later extends the semantics for volatile so that the system will not allow a write of a volatile to be reordered with respect to any previous read or write, and a read of a volatile cannot be reordered with respect to any following read or write.

Took the information from the source: https://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html


Found by Linux Verification Center with SVACE

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

    点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载