Skip to content

Commit

Permalink
Fix FPs in LocksReleasedAllPaths.Mutex.NetFx.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
martin-strecker-sonarsource committed Mar 29, 2023
1 parent f74847a commit d382b3c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ OperationKindEx.FieldReference when operation.ToFieldReference() is var fieldRef
OperationKindEx.LocalReference => operation.ToLocalReference().Local,
OperationKindEx.ParameterReference => operation.ToParameterReference().Parameter,
OperationKindEx.Argument => operation.ToArgument().Value.TrackedSymbol(),
OperationKindEx.DeclarationExpression => IDeclarationExpressionOperationWrapper.FromOperation(operation).Expression.TrackedSymbol(),
_ => null
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,27 @@ public override ProgramState[] PostProcess(SymbolicContext context)
};
}
}
else if (context.Operation.Instance.AsObjectCreation() is { } objectCreation
&& objectCreation.Type.Is(KnownType.System_Threading_Mutex)
&& context.Operation.Parent.AsAssignment() is { } assignment
&& assignment.Target.TrackedSymbol() is { } symbol
&& objectCreation.Arguments.Length > 0
&& objectCreation.Arguments.ToDictionary(x => x.ToArgument().Parameter.Name, x => x.ToArgument().Value) is var arguments
&& arguments.TryGetValue("initiallyOwned", out var initiallyOwned)
&& context.State[initiallyOwned] is { } initiallyOwnedValue
&& initiallyOwnedValue.HasConstraint(BoolConstraint.True))
{
lastSymbolLock[symbol] = objectCreation.ToSonar();
return arguments.TryGetValue("createdNew", out var createdNew)
&& createdNew.TrackedSymbol() is { } trackedCreatedNew
? new[]
{
AddLock(context, symbol).SetSymbolConstraint(trackedCreatedNew, BoolConstraint.True),
context.State.SetSymbolConstraint(trackedCreatedNew, BoolConstraint.False),
}
: AddLock(context, objectCreation.WrappedOperation).Preserve(symbol).ToArray();
}

return base.PostProcess(context);
}

Expand Down Expand Up @@ -131,21 +152,7 @@ ProgramState ProcessCondition(ISymbol lockSymbol)

protected override ProgramState PostProcessSimple(SymbolicContext context)
{
if (context.Operation.Instance.AsObjectCreation() is { } objectCreation)
{
if (objectCreation.Type.Is(KnownType.System_Threading_Mutex)
&& context.Operation.Parent.AsAssignment() is { } assignment
&& objectCreation.Arguments.Length > 0
&& objectCreation.Arguments.First().ToArgument().Value is { } firstArgument
&& context.State[firstArgument] is { } firstArgumentValue
&& firstArgumentValue.HasConstraint(BoolConstraint.True)
&& assignment.Target.TrackedSymbol() is { } symbol)
{
lastSymbolLock[symbol] = objectCreation.ToSonar();
return AddLock(context, objectCreation.WrappedOperation).Preserve(symbol);
}
}
else if (context.Operation.Instance.AsInvocation() is { } invocation)
if (context.Operation.Instance.AsInvocation() is { } invocation)
{
// ToDo: we ignore the number of parameters for now.
if (invocation.TargetMethod.IsAny(KnownType.System_Threading_Monitor, "Enter", "TryEnter"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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); // Noncompliant
var m0 = new Mutex(true, "foo", out var mutexWasCreated, mutexSecurity);
if (cond)
{
m0.ReleaseMutex();
Expand Down Expand Up @@ -61,7 +61,7 @@ public static void CompliantFromDocs(string mutexName, MutexSecurity mutexSecuri
if (doesNotExist)
{
// The mutex does not exist, so create it.
m = new Mutex(true, mutexName, out mutexWasCreated, mutexSecurity); // Noncompliant FP, because the rule is not aware that the mutex didn't exist before and doesn't support out mutexWasCreated. Only awareness of both can fix it.
m = new Mutex(true, mutexName, out mutexWasCreated, mutexSecurity);
if (!mutexWasCreated)
{
// unable to create the mutex
Expand Down

0 comments on commit d382b3c

Please sign in to comment.