diff --git a/analyzers/src/SonarAnalyzer.Common/SymbolicExecution/Roslyn/RuleChecks/LocksReleasedAllPathsBase.cs b/analyzers/src/SonarAnalyzer.Common/SymbolicExecution/Roslyn/RuleChecks/LocksReleasedAllPathsBase.cs index 71f15d85600..16f210c6387 100644 --- a/analyzers/src/SonarAnalyzer.Common/SymbolicExecution/Roslyn/RuleChecks/LocksReleasedAllPathsBase.cs +++ b/analyzers/src/SonarAnalyzer.Common/SymbolicExecution/Roslyn/RuleChecks/LocksReleasedAllPathsBase.cs @@ -109,8 +109,8 @@ public override ProgramState[] PostProcess(SymbolicContext context) && createdNew.TrackedSymbol() is { } trackedCreatedNew ? new[] { - AddLock(context, symbol).SetSymbolConstraint(trackedCreatedNew, BoolConstraint.True), - context.State.SetSymbolConstraint(trackedCreatedNew, BoolConstraint.False), + AddLock(context, objectCreation.WrappedOperation).Preserve(symbol).SetSymbolConstraint(trackedCreatedNew, BoolConstraint.True), + context.SetSymbolConstraint(trackedCreatedNew, BoolConstraint.False), } : AddLock(context, objectCreation.WrappedOperation).Preserve(symbol).ToArray(); } @@ -198,7 +198,7 @@ private ProgramState AddLock(SymbolicContext context, ISymbol symbol) } lastSymbolLock[symbol] = context.Operation; - return context.SetSymbolConstraint(symbol, LockConstraint.Held).Preserve(symbol); + return context.SetSymbolConstraint(symbol, LockConstraint.Held).SetSymbolConstraint(symbol, ObjectConstraint.NotNull).Preserve(symbol); } private ProgramState RemoveLock(SymbolicContext context, ISymbol symbol) diff --git a/analyzers/tests/SonarAnalyzer.UnitTest/TestCases/SymbolicExecution/Roslyn/LocksReleasedAllPaths.Mutex.NetFx.cs b/analyzers/tests/SonarAnalyzer.UnitTest/TestCases/SymbolicExecution/Roslyn/LocksReleasedAllPaths.Mutex.NetFx.cs index 0a6e7305fa1..463861374eb 100644 --- a/analyzers/tests/SonarAnalyzer.UnitTest/TestCases/SymbolicExecution/Roslyn/LocksReleasedAllPaths.Mutex.NetFx.cs +++ b/analyzers/tests/SonarAnalyzer.UnitTest/TestCases/SymbolicExecution/Roslyn/LocksReleasedAllPaths.Mutex.NetFx.cs @@ -14,7 +14,7 @@ public class MutexTest public static void Noncompliant(MutexSecurity mutexSecurity, bool cond) { // Note that Dispose() closes the underlying WaitHandle, but does not release the mutex - var m0 = new Mutex(true, "foo", out var mutexWasCreated, mutexSecurity); + var m0 = new Mutex(true, "foo", out var mutexWasCreated, mutexSecurity); // Noncompliant if (cond) { m0.ReleaseMutex(); diff --git a/analyzers/tests/SonarAnalyzer.UnitTest/TestCases/SymbolicExecution/Roslyn/LocksReleasedAllPaths.Mutex.cs b/analyzers/tests/SonarAnalyzer.UnitTest/TestCases/SymbolicExecution/Roslyn/LocksReleasedAllPaths.Mutex.cs index c503d7fd517..ff009150faf 100644 --- a/analyzers/tests/SonarAnalyzer.UnitTest/TestCases/SymbolicExecution/Roslyn/LocksReleasedAllPaths.Mutex.cs +++ b/analyzers/tests/SonarAnalyzer.UnitTest/TestCases/SymbolicExecution/Roslyn/LocksReleasedAllPaths.Mutex.cs @@ -23,8 +23,6 @@ public void Noncompliant(Foo foo) var m3 = Mutex.OpenExisting("x"); m3.WaitOne(); // Noncompliant - var m4 = new Mutex(); - foo.instanceMutex.WaitOne(); // FN Foo.staticMutex.WaitOne(); // Noncompliant @@ -35,7 +33,6 @@ public void Noncompliant(Foo foo) m1.ReleaseMutex(); m2.ReleaseMutex(); m3.ReleaseMutex(); - m4.ReleaseMutex(); foo.instanceMutex.ReleaseMutex(); Foo.staticMutex.ReleaseMutex(); }