@@ -80,15 +80,22 @@ namespace
8080 }
8181
8282 /* *
83- * Destructor of the CHERIoT MQTT context object. This takes
84- * care of closing the TLS link, and de-allocating all objects.
83+ * Destroy the CHERIoT MQTT context object. This takes care of closing
84+ * the TLS link, and de-allocating all objects.
8585 */
86- ~CHERIoTMqttContext ( )
86+ void destroy (SObj allocator )
8787 {
8888 Timeout t{UnlimitedTimeout};
8989 tls_connection_close (&t, tlsHandle);
90+ heap_free (allocator, networkBuffer.pBuffer );
9091 }
9192
93+ /* *
94+ * No destructor. Implicit deletion is not allowed, `destroy` must be
95+ * called explicitly.
96+ */
97+ ~CHERIoTMqttContext () = delete ;
98+
9299 /* *
93100 * Following this we allocate variable length data:
94101 * - incoming publishes (array of MQTTPubAckInfo_t)
@@ -534,7 +541,7 @@ SObj mqtt_connect(Timeout *t,
534541 // coreMQTT), we can assume that the allocator zeroes out for us.
535542 size_t handleSize =
536543 sizeof (CHERIoTMqttContext) -
537- sizeof (CHERIoTMqttContext::variableLengthData) + networkBufferSize +
544+ sizeof (CHERIoTMqttContext::variableLengthData) +
538545 sizeof (MQTTPubAckInfo_t) * (incomingPublishCount + outgoingPublishCount);
539546
540547 // Create a sealed MQTT handle.
@@ -575,8 +582,14 @@ SObj mqtt_connect(Timeout *t,
575582 reinterpret_cast <MQTTPubAckInfo_t *>(&context->variableLengthData );
576583 MQTTPubAckInfo_t *outgoingPublishes =
577584 incomingPublishes + incomingPublishCount;
578- uint8_t *networkBuffer = reinterpret_cast <uint8_t *>(outgoingPublishes) +
579- sizeof (MQTTPubAckInfo_t) * outgoingPublishCount;
585+ uint8_t *networkBuffer =
586+ static_cast <uint8_t *>(heap_allocate (t, allocator, networkBufferSize));
587+
588+ if (networkBuffer == nullptr )
589+ {
590+ token_obj_destroy (allocator, mqtt_key (), sealedMQTTHandle);
591+ return nullptr ;
592+ }
580593
581594 // Initialize context nested structures.
582595 context->networkContext .tlsHandle = tlsHandle;
@@ -594,7 +607,7 @@ SObj mqtt_connect(Timeout *t,
594607 // `token_obj_destroy` will free the `CHERIoTMqttContext`
595608 // object through `heap_free`, but not call its destructor. We
596609 // must do that manually.
597- context->~CHERIoTMqttContext ( );
610+ context->destroy (allocator );
598611 token_obj_destroy (allocator, mqtt_key (), sealedMQTTHandle);
599612 };
600613 std::unique_ptr<struct SObjStruct , decltype (cleanup)> sealedContext{
@@ -787,7 +800,7 @@ int mqtt_disconnect(Timeout *t, SObj allocator, SObj mqttHandle)
787800 t,
788801 mqttHandle,
789802 [&](CHERIoTMqttContext *connection) {
790- connection->~CHERIoTMqttContext ( );
803+ connection->destroy (allocator );
791804 token_obj_destroy (allocator, mqtt_key (), mqttHandle);
792805 return 0 ;
793806 },
0 commit comments