From 0664cc9a4630653c80086e2ef3fdb6bcc5cd3ccc Mon Sep 17 00:00:00 2001 From: Julius Friedman Date: Tue, 21 Nov 2023 18:11:32 -0500 Subject: [PATCH] RtspClient - fix possible unintentional overflow static PlatformNotSupportedException --- Common/Extensions/ThreadExtensions.cs | 6 ++++-- Rtsp/RtspClient.cs | 4 +--- RtspServer/ClientSession.cs | 2 +- UnitTests/Program.cs | 3 +-- 4 files changed, 7 insertions(+), 8 deletions(-) diff --git a/Common/Extensions/ThreadExtensions.cs b/Common/Extensions/ThreadExtensions.cs index f1ad6919..b07090c6 100644 --- a/Common/Extensions/ThreadExtensions.cs +++ b/Common/Extensions/ThreadExtensions.cs @@ -57,6 +57,8 @@ public static bool IsRunning(System.Threading.Thread thread) (thread.ThreadState & (System.Threading.ThreadState.Stopped | System.Threading.ThreadState.Unstarted)) == System.Threading.ThreadState.Running; } + static readonly System.PlatformNotSupportedException ThreadAbortNotSupported = new ("Thread.Abort is not supported. Ensure your thread has stopped."); + /// /// Calls on the given thread and indicates if the interrupt was received back. /// @@ -78,7 +80,7 @@ public static void AbortAndFree(ref System.Threading.Thread thread, System.Threa //Attempt to join if (thread.Join(timeout) is false) { - throw new System.PlatformNotSupportedException("Thread.Abort is not supported. Ensure your thread has stopped."); + throw ThreadAbortNotSupported; } //Reset the state of the thread to indicate success @@ -101,7 +103,7 @@ public static void AbortAndFree(ref System.Threading.Thread thread, System.TimeS IsRunning(thread) && thread.Join(timeout) is false) { - throw new System.PlatformNotSupportedException("Thread.Abort is not supported. Ensure your thread has stopped."); + throw ThreadAbortNotSupported; } } diff --git a/Rtsp/RtspClient.cs b/Rtsp/RtspClient.cs index b14edfa3..2b875bd9 100644 --- a/Rtsp/RtspClient.cs +++ b/Rtsp/RtspClient.cs @@ -2472,10 +2472,8 @@ SharesSocket is false && //Incrment for justReceived received += justReceived; - int contentLength = interleaved.ContentLength; - //Ensure we are not doing to much receiving - if (contentLength >= 0 && received > RtspMessage.MaximumLength + contentLength) + if (received > RtspMessage.MaximumLength) { if (Common.IDisposedExtensions.IsNullOrDisposed(interleaved) && interleaved.IsPersistent) diff --git a/RtspServer/ClientSession.cs b/RtspServer/ClientSession.cs index b2f4a3c3..6d89d800 100644 --- a/RtspServer/ClientSession.cs +++ b/RtspServer/ClientSession.cs @@ -1717,7 +1717,7 @@ internal RtspMessage ProcessPause(RtspMessage request, RtpSource source) foreach (RtpClient.TransportContext sourceContext in source.RtpClient.GetTransportContexts()) { //Adding the id will stop the packets from being enqueued into the RtpClient - PacketBuffer.Add((int)sourceContext.SynchronizationSourceIdentifier); + PacketBuffer.Add(sourceContext.SynchronizationSourceIdentifier); } //Return the response diff --git a/UnitTests/Program.cs b/UnitTests/Program.cs index aff87e09..a5bdeeb8 100644 --- a/UnitTests/Program.cs +++ b/UnitTests/Program.cs @@ -2150,7 +2150,6 @@ static void TestRtspClient(string location, System.Net.NetworkCredential cred = /// static async Task TestServerAsync() { - Console.WriteLine(nameof(TestServerAsync)); System.Runtime.GCLatencyMode oldMode = System.Runtime.GCSettings.LatencyMode; @@ -2482,7 +2481,7 @@ static async Task TestServerAsync() { //Streams lookup the packet by the ssrc, this could be changed to use the payload type etc rtp.SynchronizationSourceIdentifier = rtpDumpAudioStream.SourceId; - rtpDumpAudioStream.Frames.Enqueue(new RtpFrame { rtp }); + rtpDumpAudioStream.Frames.Enqueue([rtp]); } } }