Skip to content

Commit 471f331

Browse files
committed
Prevent multiple allocs by mqttClientUpdate
1 parent 3d96900 commit 471f331

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

Firmware/RTK_Everywhere/MQTT_Client.ino

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ const char MQTT_TOPIC_ASSISTNOW[] = "/pp/ubx/mga"; // AssistNow (MGA) topic
112112

113113
static MqttClient *mqttClient;
114114

115-
static char *mqttClientCertificateBuffer; // Buffer for client certificate
115+
static char *mqttClientCertificateBuffer = nullptr; // Buffer for client certificate
116+
static char *mqttClientPrivateKeyBuffer = nullptr; // Buffer for client private key
116117

117118
// Throttle the time between connection attempts
118119
static int mqttClientConnectionAttempts; // Count the number of connection attempts between restarts
@@ -121,8 +122,6 @@ static int mqttClientConnectionAttemptsTotal; // Count the number of connection
121122

122123
static volatile uint32_t mqttClientLastDataReceived; // Last time data was received via MQTT
123124

124-
static char *mqttClientPrivateKeyBuffer; // Buffer for client private key
125-
126125
static NetworkSecureWiFiClient *mqttSecureClient;
127126

128127
static volatile uint8_t mqttClientState = MQTT_CLIENT_OFF;
@@ -564,15 +563,20 @@ void mqttClientUpdate()
564563
}
565564

566565
// Allocate the buffers
566+
// Freed by mqttClientShutdown / mqttClientStop
567567
if (online.psram == true)
568568
{
569-
mqttClientCertificateBuffer = (char *)ps_malloc(MQTT_CERT_SIZE);
570-
mqttClientPrivateKeyBuffer = (char *)ps_malloc(MQTT_CERT_SIZE);
569+
if (!mqttClientCertificateBuffer)
570+
mqttClientCertificateBuffer = (char *)ps_malloc(MQTT_CERT_SIZE);
571+
if (!mqttClientPrivateKeyBuffer)
572+
mqttClientPrivateKeyBuffer = (char *)ps_malloc(MQTT_CERT_SIZE);
571573
}
572574
else
573575
{
574-
mqttClientCertificateBuffer = (char *)malloc(MQTT_CERT_SIZE);
575-
mqttClientPrivateKeyBuffer = (char *)malloc(MQTT_CERT_SIZE);
576+
if (!mqttClientCertificateBuffer)
577+
mqttClientCertificateBuffer = (char *)malloc(MQTT_CERT_SIZE);
578+
if (!mqttClientPrivateKeyBuffer)
579+
mqttClientPrivateKeyBuffer = (char *)malloc(MQTT_CERT_SIZE);
576580
}
577581

578582
if ((!mqttClientCertificateBuffer) || (!mqttClientPrivateKeyBuffer))

0 commit comments

Comments
 (0)