Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suspicious code fragments found by PVS-Studio #6073

Closed
VasilievSerg opened this issue Aug 24, 2022 · 1 comment · Fixed by #6497
Closed

Suspicious code fragments found by PVS-Studio #6073

VasilievSerg opened this issue Aug 24, 2022 · 1 comment · Fixed by #6497

Comments

@VasilievSerg
Copy link

Hello,
PVS-Studio (static analyzer) issued a few warnings on the sources. I've described the most suspicious below. Hope it'll be useful. :)

P.S. Please, let me know if I should open separate issues for each code fragment.


Issue 1

public override bool Equals(object obj)
{
  if (!(obj is ActorMaterializerSettings s)) return false;
    return
                s.InitialInputBufferSize == InitialInputBufferSize &&
                s.MaxInputBufferSize == MaxInputBufferSize &&
                s.Dispatcher == Dispatcher &&
                s.SupervisionDecider == SupervisionDecider &&
                s.SubscriptionTimeoutSettings == SubscriptionTimeoutSettings &&   // <=
                s.IsDebugLogging == IsDebugLogging &&
                s.OutputBurstLimit == OutputBurstLimit &&
                s.SyncProcessingLimit == SyncProcessingLimit &&
                s.IsFuzzingMode == IsFuzzingMode &&
                s.IsAutoFusing == IsAutoFusing &&
                s.SubscriptionTimeoutSettings == SubscriptionTimeoutSettings &&   // <=
                s.StreamRefSettings == StreamRefSettings;
}

Warning: There are identical sub-expressions 's.SubscriptionTimeoutSettings == SubscriptionTimeoutSettings' to the left and to the right of the '&&' operator.

Link to the sources: link

Looks suspicious that the SubscriptionTimeoutSettings field is checked twice, whereas the MaxFixedBufferSize field is not checked.


Issue 2

internal static string Base64Encode(this long value, string prefix)
{
  // 11 is the number of characters it takes to represent long.MaxValue
  // so we will never need a larger size for encoding longs
  Span<char> sb = stackalloc char[11 + prefix?.Length ?? 0];
  ....
}

Warning: Perhaps the '??' operator works in a different way than it was expected. Its priority is lower than priority of other operators in its left part.

Link to the sources: link.

If prefix is null then sb will be an empty array. Looks like it should contain 11 elements at least. If so, the correct expression should contain parentheses:

11 + (prefix?.Length ?? 0)

Issue 3

protected override void PreStart() => Console.WriteLine("Good Morning, we are awake!", ConsoleColor.Green);
protected override void PostStop() => Console.WriteLine("Good Night, going to bed!", ConsoleColor.Red);

Warning: Incorrect format. A different number of format items is expected while calling 'WriteLine' function. Arguments not used: ConsoleColor.Green.

Link to the sources: link.

'WriteLine' method doesn't contain overloads that accept arguments of the 'ConsoleColor' type. So, they'll be ignored.


Issue 4

 if (_actor == null)
 {
  ....
} 
else if (IsNormal)
{
  var failedActor = _actor;

  if (System.Settings.DebugLifecycle)
    Publish(new Debug(_self.Path.ToString(), failedActor.GetType(), "Restarting"));

    if(!(failedActor is null))
    ....
}
....

Warning: Expression '!(failedActor is null)' is always true.

Link to the sources: link.

The failedActor is always not null here. _actor is not null at the else branch, as a result failedActor too.

@Arkatufus
Copy link
Contributor

These are all good recommendations, thank you for pointing them out

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants