From 40aefd0ad361e715350669d6895c684587766086 Mon Sep 17 00:00:00 2001 From: SeppPenner Date: Wed, 10 Jun 2020 12:00:34 +0200 Subject: [PATCH] Added local variable to avoid that the solution is not threadsafe. --- Build/MQTTnet.nuspec | 1 + .../MQTTnet/Implementations/MqttTcpChannel.cs | 18 ++++++++++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/Build/MQTTnet.nuspec b/Build/MQTTnet.nuspec index b329846..b366be7 100644 --- a/Build/MQTTnet.nuspec +++ b/Build/MQTTnet.nuspec @@ -17,6 +17,7 @@ * [Server] Adjusted some namespaces (BREAKING CHANGE!) * [Server] Added state checks (throw if not started etc.) for most server APIs. * [Server] Exposed real X509Certificate2 (instead byte array) to TLS options (thanks to @borigas). +* [Core] Fixed a null reference exception in the MqttTcpChannel with WriteAsync and ReadAsync. Copyright Christian Kratky 2016-2020 MQTT Message Queue Telemetry Transport MQTTClient MQTTServer Server MQTTBroker Broker NETStandard IoT InternetOfThings Messaging Hardware Arduino Sensor Actuator M2M ESP Smart Home Cities Automation Xamarin Blazor diff --git a/Source/MQTTnet/Implementations/MqttTcpChannel.cs b/Source/MQTTnet/Implementations/MqttTcpChannel.cs index 19ec6d1..72c0e00 100644 --- a/Source/MQTTnet/Implementations/MqttTcpChannel.cs +++ b/Source/MQTTnet/Implementations/MqttTcpChannel.cs @@ -127,7 +127,14 @@ namespace MQTTnet.Implementations // Workaround for: https://github.com/dotnet/corefx/issues/24430 using (cancellationToken.Register(Dispose)) { - return await _stream.ReadAsync(buffer, offset, count, cancellationToken).ConfigureAwait(false); + var stream = _stream; + + if (stream == null) + { + throw new ObjectDisposedException(nameof(stream)); + } + + return await stream.ReadAsync(buffer, offset, count, cancellationToken).ConfigureAwait(false); } } catch (ObjectDisposedException) @@ -157,7 +164,14 @@ namespace MQTTnet.Implementations // Workaround for: https://github.com/dotnet/corefx/issues/24430 using (cancellationToken.Register(Dispose)) { - await _stream.WriteAsync(buffer, offset, count, cancellationToken).ConfigureAwait(false); + var stream = _stream; + + if (stream == null) + { + throw new ObjectDisposedException(nameof(stream)); + } + + await stream.WriteAsync(buffer, offset, count, cancellationToken).ConfigureAwait(false); } } catch (ObjectDisposedException)