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® 9.2p0 | CONFIDENTIAL | CodeSecure Inc |
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 C# 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.
| Class Name | Synchronization on Interned String (C#) | |||
|---|---|---|---|---|
| Significance | reliability | |||
| Mnemonic | CSHARP.CONCURRENCY.LOCK.ISTR | |||
| Categories |
|
|||
| Availability | Available for C# only. |
|||
| Enabling | Checks for this warning class are enabled by
default. To disable them, add the following WARNING_FILTER rule to the
project configuration file.
WARNING_FILTER += discard class="Synchronization on Interned String (C#)" |
Consider the following program:
using System;
using System.Collections;
using System.Collections.Generic;
namespace Concurrency
{
public class Concurrency
{
private static Concurrency instance;
private static readonly object lock1 = new object();
private volatile string lock2 = &lock&; // Useless volatile Modifier (C#) warning issued here
// - field is only assigned in constructor
private string lock3 = new string(&lock&.ToCharArray());
private string lock4 = string.Intern(new string("lock".ToCharArray()));
private volatile Dictionary<string, int> map = new Dictionary<string, int>(); // Useless volatile Modifier (C#) warning issued here
// - field is only assigned in constructor
// - if the intent is to publish UPDATES to map, use a concurrent map instead
private Concurrency() { }
public static Concurrency GetInstance1()
{
if (instance == null)
instance = new Concurrency(); // Double-Checked Locking (C#) warning issued here
return instance;
}
public static Concurrency GetInstance2()
{
lock (lock1)
{
if (instance == null)
instance = new Concurrency();
}
return instance;
}
public static Concurrency GetInstance3()
{
if (instance == null)
lock (lock1)
{
if (instance == null)
instance = new Concurrency();
}
return instance;
}
private int counter;
private int Next()
{
map.Add((++counter).ToString(), counter);
return counter;
}
public int Step(int i)
{
lock (lock1) // Useless Synchronization (C#) warning issued here
{
i++;
}
return i;
}
public int Test1()
{
lock (lock2) // Synchronization on Interned String (C#) warning issued here
{
return Next();
}
}
public int Test2()
{
lock (lock3)
{
return Next();
}
}
public int Test3()
{
lock (lock4) // Synchronization on Interned String (C#) warning issued here
{
return Next();
}
}
public int CallTest6()
{
return Test6(lock2);
}
public int Test6(string s)
{
lock (s) // Synchronization on Interned String (C#) warning issued here
{
return Next();
}
}
}
}
Check if the warnings correspond to actual possible errors for a concurrent execution of the program.
The following configuration file parameters affect checks for this warning class.
To report problems with this documentation, please visit https://support.codesecure.com/.