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

Fix issues found by PVS-Studio static analyzer #7 #34

Merged
merged 18 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Common/Classes/MemorySegment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ public override bool Equals(object obj)
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
public bool Equals(MemorySegment other)
{
return Common.IDisposedExtensions.IsNullOrDisposed(other) is false && Common.IDisposedExtensions.IsNullOrDisposed(this) is false && other.GetHashCode().Equals(GetHashCode());
return Common.IDisposedExtensions.IsNullOrDisposed(other) is false && Common.IDisposedExtensions.IsNullOrDisposed(this) is false && other.GetHashCode() == GetHashCode();
}

#region Operators
Expand Down
4 changes: 3 additions & 1 deletion Common/Classes/PacketBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,9 @@ bool IPacket.IsComplete
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
get
{
return Completer is null ? StaticCompleteFrom(null, null).Equals(int.MinValue) : Completer(null, null).Equals(int.MinValue);
return Completer is null
? StaticCompleteFrom(null, null) is int.MinValue
: Completer(null, null) is int.MinValue;
}
}

Expand Down
4 changes: 2 additions & 2 deletions Common/Interfaces/IParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ void IParser.OnStateChanged(ref uint state, ref ulong left, ref ulong right)
}
default:
{
if (State.Equals(Complete))
if (State is Complete)
{
OnRealization();
}
Expand Down Expand Up @@ -158,7 +158,7 @@ bool IWriteOnly.IsWriteOnly

bool ICompletable.IsComplete
{
get { return State.Equals(Complete); }
get { return State is Complete; }
}
}
}
2 changes: 1 addition & 1 deletion Common/Utility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public static int Find(byte[] array, byte[] needle, int startIndex, int sourceLe
index = Array.IndexOf(array, needle[0], startIndex, sourceLength - needleLen + 1);
#endif
// if we did not find even the first element of the needls, then the search is failed
if (index.Equals(-1))
if (index is -1)
return -1;

int i, p;
Expand Down
2 changes: 1 addition & 1 deletion Concepts/Classes/Number.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1446,7 +1446,7 @@ public int IndexOfAny(Bits[] b, bool ignoreEndian = true)
{
int index = -1;

foreach (Bits x in b) if (false.Equals((index = IndexOf(x, ignoreEndian)).Equals(-1))) break;
foreach (Bits x in b) if (false.Equals((index = IndexOf(x, ignoreEndian)) is -1)) break;

return index;
}
Expand Down
5 changes: 3 additions & 2 deletions Http/HttpMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2560,9 +2560,10 @@ public bool Equals(HttpMessage other)
//other.Created != Created

return other is not null
&& other.MessageType == MessageType
&&
other.Version.Equals(Version)
other.MessageType == MessageType
&&
other.Version == Version
&&
other.MethodString.Equals(MethodString)
//&&
Expand Down
11 changes: 6 additions & 5 deletions Rtp/RFC3550.cs
Original file line number Diff line number Diff line change
Expand Up @@ -329,13 +329,16 @@ public static IEnumerable<RtcpPacket> FromCompoundBytes(byte[] array, int offset
//Get all packets contained in the buffer.
foreach (RtcpPacket currentPacket in RtcpPacket.GetPackets(array, offset, count, version, null, ssrc, shouldDispose))
{

//Determine who the packet is from and what type it is.
int firstPayloadType = currentPacket.PayloadType;

//The first packet in a compound packet needs to be validated
if (parsedPackets is 0 && !IsValidRtcpHeader(currentPacket.Header, currentPacket.Version)) yield break;
else if (currentPacket.Version != version || skipUnknownTypes && RtcpPacket.GetImplementationForPayloadType((byte)currentPacket.PayloadType) is null) yield break;
if (parsedPackets is 0 && IsValidRtcpHeader(currentPacket.Header, currentPacket.Version) is false)
yield break;

if (currentPacket.Version != version || (skipUnknownTypes &&
RtcpPacket.GetImplementationForPayloadType((byte)currentPacket.PayloadType) is null))
yield break;

//Count the packets parsed
++parsedPackets;
Expand Down Expand Up @@ -657,8 +660,6 @@ public static void CalculateFractionAndLoss(ref uint RtpBaseSeq, ref ushort RtpM
{
//Should be performed in the Conference level, these values here will only
//should allow a backoff to occur in reporting and possibly eventually to be turned off.
fraction = 0;

uint extended_max = RtpSeqCycles + RtpMaxSeq;

int expected = (int)(extended_max - RtpBaseSeq + 1);
Expand Down
13 changes: 8 additions & 5 deletions Rtp/Rtcp/RtcpPacket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -360,11 +360,14 @@ public RtcpPacket(int version, int payloadType, int padding, int blockCount, int

int lowerBound = payload.GetLowerBound(0), upperBound = payload.GetUpperBound(0);

if (index < lowerBound || index > upperBound) throw new ArgumentOutOfRangeException("index", "Must refer to an accessible position in the given array");
if (index < lowerBound || index > upperBound)
throw new ArgumentOutOfRangeException(nameof(index), "Must refer to an accessible position in the given array");

if (count > upperBound) throw new ArgumentOutOfRangeException("count", "Must refer to an accessible position in the given array");
if (count > upperBound)
throw new ArgumentOutOfRangeException(nameof(count), "Must refer to an accessible position in the given array");

if (count + index > upperBound) throw new ArgumentOutOfRangeException("index", "Count must refer to an accessible position in the given array when deleniated by index");
if (count + index > upperBound)
throw new ArgumentOutOfRangeException(nameof(index), "Count must refer to an accessible position in the given array when deleniated by index");

AddBytesToPayload(payload, index, count);
}
Expand Down Expand Up @@ -990,11 +993,11 @@ protected override void Dispose(bool disposing)
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
public bool Equals(RtcpPacket other)
{
return other.Length.Equals(Length)
return other.Length == Length
&&
other.Payload.Equals(Payload) //SequenceEqual...
&&
other.GetHashCode().Equals(GetHashCode());
other.GetHashCode() == GetHashCode();
}

[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
Expand Down
4 changes: 3 additions & 1 deletion Rtp/Rtcp/SourceDescriptionReport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1252,8 +1252,10 @@ public bool Remove(SourceDescriptionChunk chunk)
//Iterate the contained chunks
foreach (SourceDescriptionChunk currentChunk in GetChunkIterator())
{
contained = chunk.ChunkData == currentChunk.ChunkData;

//If the chunk yielded in the iterator matches the chunk identifier a match is present.
if (contained = (chunk.ChunkData == currentChunk.ChunkData))
if (contained)
{
break;
}
Expand Down
21 changes: 14 additions & 7 deletions Rtp/RtpClient.Methods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,8 @@ public partial class RtpClient : Media.Common.ILoggingReference
protected internal /*virtual*/ int SendReceiversReport(TransportContext context, bool force = false)
{
//Determine if the ReceiversReport can be sent.
if (Common.IDisposedExtensions.IsNullOrDisposed(this) || Common.IDisposedExtensions.IsNullOrDisposed(context) //If the context is disposed
if (Common.IDisposedExtensions.IsNullOrDisposed(this) ||
Common.IDisposedExtensions.IsNullOrDisposed(context) //If the context is disposed
&& //AND the call has not been forced AND the context IsRtcpEnabled
(force is false && context.IsRtcpEnabled)
// OR there is no RtcpSocket
Expand Down Expand Up @@ -1521,7 +1522,12 @@ private int ReadApplicationLayerFraming(ref int received, ref int sessionRequire
if (Common.IDisposedExtensions.IsNullOrDisposed(buffer)) buffer = m_Buffer;

//Ensure the socket can poll, should measure against parallel checks with OR
if (buffer.Count <= 0 | m_StopRequested | socket is null | remote is null | Common.IDisposedExtensions.IsNullOrDisposed(buffer) | Common.IDisposedExtensions.IsNullOrDisposed(this)) return 0;
if (buffer.Count <= 0 ||
m_StopRequested ||
socket is null ||
remote is null ||
Common.IDisposedExtensions.IsNullOrDisposed(buffer) ||
Common.IDisposedExtensions.IsNullOrDisposed(this)) return 0;

bool tcp = socket.ProtocolType == System.Net.Sockets.ProtocolType.Tcp;

Expand Down Expand Up @@ -1870,7 +1876,7 @@ private int ReadApplicationLayerFraming(ref int received, ref int sessionRequire
//If rtp or rtcp is expected check data
if (expectRtp || expectRtcp || frameChannel.HasValue && frameChannel.Value < TransportContexts.Count)
{
Media.Common.ILoggingExtensions.Log(Logger, InternalId + "ProcessFrameData - Large Packet of " + frameLength + " for Channel " + frameChannel.Value + " remainingInBuffer=" + remainingInBuffer);
Media.Common.ILoggingExtensions.Log(Logger, InternalId + "ProcessFrameData - Large Packet of " + frameLength + " for Channel " + frameChannel.GetValueOrDefault() + " remainingInBuffer=" + remainingInBuffer);

//Could allow for the buffer to be replaced here for the remainder of this call only.

Expand Down Expand Up @@ -2439,12 +2445,13 @@ private void SendReceieve()
int usec = (int)Media.Common.Extensions.TimeSpan.TimeSpanExtensions.TotalMicroseconds(tc.m_ReceiveInterval) >> 4;

//If receiving Rtp and the socket is able to read
if (rtpEnabled && (shouldStop = IsUndisposed is false || m_StopRequested) is false
//&& (readSockets.Contains(tc.RtpSocket) || errorSockets.Contains(tc.RtpSocket))
if (rtpEnabled &&
(shouldStop = IsUndisposed is false || m_StopRequested) is false
//Check if the socket can read data first or that data needs to be received
&&
(tc.LastRtpPacketReceived.Equals(System.TimeSpan.MinValue) | tc.LastRtpPacketReceived >= tc.m_ReceiveInterval) ||
tc.RtpSocket.Poll(usec, System.Net.Sockets.SelectMode.SelectRead))
(tc.LastRtpPacketReceived == System.TimeSpan.MinValue ||
tc.LastRtpPacketReceived >= tc.m_ReceiveInterval) ||
tc.RtpSocket.Poll(usec, System.Net.Sockets.SelectMode.SelectRead))
{
//RtpTry:
//Receive RtpData
Expand Down
45 changes: 28 additions & 17 deletions Rtp/RtpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -891,7 +891,7 @@ public bool HasReceivedRtpWithinReceiveInterval
get
{
return TotalRtpPacketsReceieved >= 0 &&
false.Equals(m_LastRtpIn.Equals(DateTime.MinValue)) &&
m_LastRtpIn != DateTime.MinValue &&
false.Equals(m_ReceiveInterval.Equals(Common.Extensions.TimeSpan.TimeSpanExtensions.InfiniteTimeSpan)) &&
LastRtpPacketReceived < m_ReceiveInterval;
}
Expand All @@ -903,7 +903,7 @@ public bool HasSentRtpWithinSendInterval
get
{
return IsActive && TotalRtpPacketsSent >= 0 &&
false.Equals(m_LastRtpOut.Equals(DateTime.MinValue)) &&
m_LastRtpOut != DateTime.MinValue &&
false.Equals(m_SendInterval.Equals(Common.Extensions.TimeSpan.TimeSpanExtensions.InfiniteTimeSpan)) &&
LastRtpPacketSent < m_SendInterval;
}
Expand All @@ -915,7 +915,7 @@ public bool HasReceivedRtcpWithinReceiveInterval
get
{
return TotalRtcpPacketsReceieved >= 0 &&
false.Equals(m_LastRtcpIn.Equals(DateTime.MinValue)) &&
m_LastRtcpIn != DateTime.MinValue &&
false.Equals(m_ReceiveInterval.Equals(Common.Extensions.TimeSpan.TimeSpanExtensions.InfiniteTimeSpan)) &&
LastRtcpReportReceived < m_ReceiveInterval;
}
Expand Down Expand Up @@ -1065,10 +1065,10 @@ public TimeSpan TimeSending
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
get
{
return IDisposedExtensions.IsNullOrDisposed(this) || m_FirstPacketSent.Equals(DateTime.MinValue) ?
Media.Common.Extensions.TimeSpan.TimeSpanExtensions.InfiniteTimeSpan
:
DateTime.UtcNow - m_FirstPacketSent;
return IDisposedExtensions.IsNullOrDisposed(this) ||
m_FirstPacketSent == DateTime.MinValue
? Media.Common.Extensions.TimeSpan.TimeSpanExtensions.InfiniteTimeSpan
: DateTime.UtcNow - m_FirstPacketSent;
}
}

Expand All @@ -1080,10 +1080,10 @@ public TimeSpan TimeReceiving
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
get
{
return IDisposedExtensions.IsNullOrDisposed(this) || m_FirstPacketReceived.Equals(DateTime.MinValue) ?
Media.Common.Extensions.TimeSpan.TimeSpanExtensions.InfiniteTimeSpan
:
DateTime.UtcNow - m_FirstPacketReceived;
return IDisposedExtensions.IsNullOrDisposed(this) ||
m_FirstPacketReceived == DateTime.MinValue
? Media.Common.Extensions.TimeSpan.TimeSpanExtensions.InfiniteTimeSpan
: DateTime.UtcNow - m_FirstPacketReceived;
}
}

Expand Down Expand Up @@ -1185,7 +1185,9 @@ public TimeSpan LastRtcpReportSent
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
get
{
return m_LastRtcpOut.Equals(DateTime.MinValue) ? TimeSpan.MinValue : DateTime.UtcNow - m_LastRtcpOut;
return m_LastRtcpOut == DateTime.MinValue
? TimeSpan.MinValue
: DateTime.UtcNow - m_LastRtcpOut;
}
}

Expand All @@ -1197,7 +1199,9 @@ public TimeSpan LastRtcpReportReceived
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
get
{
return m_LastRtcpIn.Equals(DateTime.MinValue) ? TimeSpan.MinValue : DateTime.UtcNow - m_LastRtcpIn;
return m_LastRtcpIn == DateTime.MinValue
? TimeSpan.MinValue
: DateTime.UtcNow - m_LastRtcpIn;
}
}

Expand All @@ -1209,7 +1213,9 @@ public TimeSpan LastRtpPacketReceived
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
get
{
return m_LastRtpIn.Equals(DateTime.MinValue) ? TimeSpan.MinValue : DateTime.UtcNow - m_LastRtpIn;
return m_LastRtpIn == DateTime.MinValue
? TimeSpan.MinValue
: DateTime.UtcNow - m_LastRtpIn;
}
}

Expand All @@ -1221,7 +1227,9 @@ public TimeSpan LastRtpPacketSent
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
get
{
return m_LastRtpOut.Equals(DateTime.MinValue) ? TimeSpan.MinValue : DateTime.UtcNow - m_LastRtpOut;
return m_LastRtpOut == DateTime.MinValue
? TimeSpan.MinValue
: DateTime.UtcNow - m_LastRtpOut;
}
}

Expand All @@ -1233,7 +1241,9 @@ public TimeSpan TimeActive
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
get
{
return m_Initialized.Equals(DateTime.MinValue) ? TimeSpan.MinValue : DateTime.UtcNow - m_Initialized;
return m_Initialized == DateTime.MinValue
? TimeSpan.MinValue
: DateTime.UtcNow - m_Initialized;
}
}

Expand Down Expand Up @@ -2291,7 +2301,8 @@ public void DisconnectSockets()
MulticastGroups.Clear();

//For Udp the RtcpSocket may be the same socket as the RtpSocket if the sender/reciever is duplexing
if (RtcpSocket is not null && RtpSocket.Handle.Equals(RtcpSocket.Handle) is false) RtcpSocket.Close();
if (RtcpSocket is not null && RtcpSocket.Handle.Equals(RtpSocket?.Handle) is false)
dimhotepus marked this conversation as resolved.
Show resolved Hide resolved
RtcpSocket.Close();

//Close the RtpSocket
RtpSocket?.Close();
Expand Down
Loading