Repository: quartiq/minimq (rust)

Issue:
Publish requests erroneously sent to network stack
If the socket in the network stack is disconnected (after originally being connected), the `MqttClient::publish()` function will still publish the message even though it is not possible for the message to be transmitted (due to a disconnected socket).

This can result in odd buffer overflow behavior.

The `publish()` function should silently discard the message if the socket is no longer connected.

Pull request description:
Updating is_connected() function
This PR fixes #19 by updating the `publish()` function to check if a connection with the broker exists before sending a message.

Patch:
diff --git a/src/mqtt_client.rs b/src/mqtt_client.rs
index 82e7934..dfbd6f4 100644
--- a/src/mqtt_client.rs
+++ b/src/mqtt_client.rs
@@ -190,9 +190,9 @@ where
     /// Determine if the client has established a connection with the broker.
     ///
     /// # Returns
-    /// True if the client has sent a request to establish an MQTT connection.
-    pub fn is_connected(&self) -> bool {
-        self.connect_sent
+    /// True if the client is connected to the broker.
+    pub fn is_connected(&self) -> Result<bool, Error<N::Error>> {
+        Ok(self.socket_is_connected()? && self.connect_sent)
     }
 
     /// Publish a message over MQTT.
@@ -220,7 +220,7 @@ where
         assert!(qos == QoS::AtMostOnce);
 
         // If we are not yet connected to the broker, we can't transmit a message.
-        if self.is_connected() == false {
+        if self.is_connected()? == false {
             return Ok(());
         }
 


Test patch:
diff --git a/tests/integration_test.rs b/tests/integration_test.rs
index 1d070c0..de0be7d 100644
--- a/tests/integration_test.rs
+++ b/tests/integration_test.rs
@@ -35,7 +35,7 @@ fn main() -> std::io::Result<()> {
             .unwrap();
 
         if !subscribed {
-            if client.is_connected() {
+            if client.is_connected().unwrap() {
                 client.subscribe("response", &[]).unwrap();
                 client.subscribe("request", &[]).unwrap();
                 subscribed = true;