Skip to content

Commit

Permalink
check the kind fon't cast
Browse files Browse the repository at this point in the history
  • Loading branch information
mary-georgiou-sonarsource committed Jul 20, 2023
1 parent 0935a93 commit 2cd9d9c
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,6 @@ static bool IsTryDownCast(IConversionOperationWrapper conversion) =>
internal static IIsPatternOperationWrapper? AsIsPattern(this IOperation operation) =>
operation.As(OperationKindEx.IsPattern, IIsPatternOperationWrapper.FromOperation);

internal static ILiteralOperationWrapper? AsLiteral(this IOperation operation) =>
operation.As(OperationKindEx.Literal, ILiteralOperationWrapper.FromOperation);

internal static IMethodReferenceOperationWrapper? AsMethodReference(this IOperation operation) =>
operation.As(OperationKindEx.MethodReference, IMethodReferenceOperationWrapper.FromOperation);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,9 @@ private void ProcessObjectCreation(ProgramState state, IObjectCreationOperationW
protected override ProgramState ProcessInvocation(ProgramState state, IInvocationOperationWrapper invocation)
{
state = base.ProcessInvocation(state, invocation);
return invocation.TargetMethod.Is(KnownType.System_Text_Encoding, nameof(Encoding.GetBytes)) && FindInvocationArgument(state, invocation.Arguments, KnownType.System_String)?.AsLiteral() is { }
? state.SetOperationConstraint(invocation, ByteCollectionConstraint.CryptographicallyWeak)
: state;
return invocation.TargetMethod.Is(KnownType.System_Text_Encoding, nameof(Encoding.GetBytes))
&& FindInvocationArgument(state, invocation.Arguments, KnownType.System_String)?.Kind == OperationKindEx.Literal
? state.SetOperationConstraint(invocation, ByteCollectionConstraint.CryptographicallyWeak)
: state;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,6 @@ private static bool IsCreateEncryptorMethod(IInvocationOperationWrapper invocati
&& invocation.TargetMethod.ContainingType.DerivesFrom(KnownType.System_Security_Cryptography_SymmetricAlgorithm);

private static bool IsIVProperty(IPropertyReferenceOperationWrapper property, ISymbol propertyInstance) =>
property.Property.Name == "IV"
property.Property.Name == nameof(SymmetricAlgorithm.IV)
&& propertyInstance.GetSymbolType().DerivesFrom(KnownType.System_Security_Cryptography_SymmetricAlgorithm);
}

0 comments on commit 2cd9d9c

Please sign in to comment.