Skip to content

Commit d8ba908

Browse files
committed
Fix: remove variable dependency in mqtt_set_will_options
last will topic and message is now allocated to remove dependency on variable type passed to function. Signed-off-by: Ajay Bhargav <[email protected]>
1 parent 12f787f commit d8ba908

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

mqttclient/mqttclient.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1492,17 +1492,21 @@ int mqtt_set_will_options(mqtt_client_t* c, char *topic, mqtt_qos_t qos, uint8_t
14921492
RETURN_ERROR(MQTT_NULL_VALUE_ERROR);
14931493

14941494
if (NULL == c->mqtt_will_options) {
1495-
c->mqtt_will_options = (mqtt_will_options_t *)platform_memory_alloc(sizeof(mqtt_will_options_t));
1495+
c->mqtt_will_options = (mqtt_will_options_t *)platform_memory_alloc(sizeof(mqtt_will_options_t) + strlen(topic) + strlen(message) + 2);
14961496
MQTT_ROBUSTNESS_CHECK(c->mqtt_will_options, MQTT_MEM_NOT_ENOUGH_ERROR);
14971497
}
14981498

14991499
if (0 == c->mqtt_will_flag)
15001500
c->mqtt_will_flag = 1;
15011501

1502-
c->mqtt_will_options->will_topic = topic;
1502+
c->mqtt_will_options->will_topic = (char *)c->mqtt_will_options + sizeof(mqtt_will_options_t);
1503+
c->mqtt_will_options->will_message = c->mqtt_will_options->will_topic + strlen(topic) + 1;
1504+
1505+
strcpy(c->mqtt_will_options->will_topic, topic);
1506+
strcpy(c->mqtt_will_options->will_message, message);
1507+
15031508
c->mqtt_will_options->will_qos = qos;
15041509
c->mqtt_will_options->will_retained = retained;
1505-
c->mqtt_will_options->will_message = message;
15061510

15071511
RETURN_ERROR(MQTT_SUCCESS_ERROR);
15081512
}

0 commit comments

Comments
 (0)