JavaScript is not currently enabled, but is required for full CodeSonar manual search and browse functionality.

If you are viewing this file in your hub's Web GUI, enable JavaScript in your browser: you will also need it for GUI functionality.

If you opened this file directly from disk, your browser may be directly suppressing JavaScript functionality: certain browsers perform this suppression on local files (but not files delivered by web servers) for security reasons.

CodeSonar® 28.0w AdaCore Inc
Java


JAVA.CONCURRENCY.LOCK.ISTR : Synchronization on Interned String (Java)

要旨

Synchronisation occurs on an interned string.

Concurrency is an important but complex aspect of modern software. As a consequence, it is often used in an incorrect way, also because the subtleties of the Java memory model are not always understood. This checker identifies a large class of common programming errors due to incorrect uses of concurrency primitives, such as incorrect implementations of the singleton pattern and incorrect uses of the volatile field modifier, whose goal is to publish a field update to all executing cores. The latter, however, has a cost in terms of execution time.

プロパティ

クラス名 Synchronization on Interned String (Java)
日本語クラス名 Synchronization on Interned String (Java)
クラス分類 信頼性 (reliability)
ニーモニック JAVA.CONCURRENCY.LOCK.ISTR
カテゴリー
CWE CWE:366 Race Condition within a Thread
  CWE:412 Unrestricted Externally Accessible Lock
CERT-Java CERT-Java:LCK00-J Use private final lock objects to synchronize classes that may interact with untrusted code
OWASP-2021 OWASP-2021:A4 Insecure Design
OWASP-2025 OWASP-2025:A06 Insecure Design
対応言語 Java で利用可能です。
有効/無効設定 このワーニングクラスのチェックはデフォルトで有効になっています。チェックを無効にするにはプロジェクト設定ファイル (configuration file)に以下の WARNING_FILTER ルールを追加してください。
WARNING_FILTER += discard class="Synchronization on Interned String (Java)"

例

In the following program, incrementBad synchronizes on internedLock, whose value is a string literal. String literals are interned, so that lock is a single object shared across the whole program: unrelated code that happens to synchronize on the same literal can block this method or deadlock with it. incrementGood instead synchronizes on a private Object that no other code can reference.

public class InternedStringLock {

    // String literals are interned, so this field refers to a single object
    // that is shared across the whole program.
    private final String internedLock = "lock";

    // A freshly created private object is safe to lock on: no other code can
    // obtain a reference to it.
    private final Object lock = new Object();

    private int counter;

    public void incrementBad() {
        synchronized (internedLock) {   // 'Synchronization on Interned String (Java)' warning issued here
            counter++;
        }
    }

    public void incrementGood() {
        synchronized (lock) {           // ok: a private object, not an interned string
            counter++;
        }
    }
}

この例を再現するには、以下の設定で解析してください。

JAVA_ANALYSIS_ENTRY_POINTS_MODE = ALL_METHODS

解決法

Check if the warnings correspond to actual possible errors for a concurrent execution of the program.

関連のある設定ファイルパラメータ

設定ファイルの以下のパラメータがこのワーニングクラスのチェックに影響します。

 

To report problems with this documentation, please visit https://support.adacore.com/csm.