From 8cf9ba95f857cbbc7f5d49b631075884a98e260b Mon Sep 17 00:00:00 2001 From: gc87 Date: Fri, 29 Jul 2022 15:47:12 +0800 Subject: [PATCH] refactor(mqtt): static alloc -> dynamic alloc --- plugins/mqtt/mqtt_plugin.c | 11 ++++++++--- src/connection/mqtt_c_client.c | 26 ++++++++++++++++++++------ 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/plugins/mqtt/mqtt_plugin.c b/plugins/mqtt/mqtt_plugin.c index c22990a4..ed843d0c 100644 --- a/plugins/mqtt/mqtt_plugin.c +++ b/plugins/mqtt/mqtt_plugin.c @@ -28,6 +28,7 @@ #define INTERVAL 100000U #define TIMEOUT 3000U +#define MAX_TOPIC_LEN 256 #define TOPIC_READ_REQ "neuron/%s/read/req" #define TOPIC_WRITE_REQ "neuron/%s/write/req" @@ -64,13 +65,17 @@ struct mqtt_routine { static char *topics_format(char *format, char *name) { - char temp[256] = { '\0' }; - int rc = snprintf(temp, 256, format, name); + char *topic = calloc(1, MAX_TOPIC_LEN); + if (NULL == topic) { + return NULL; + } + + int rc = snprintf(topic, MAX_TOPIC_LEN, format, name); if (-1 != rc) { - char *topic = strdup(temp); return topic; } + free(topic); return NULL; } diff --git a/src/connection/mqtt_c_client.c b/src/connection/mqtt_c_client.c index 11fdb092..964e0ce0 100644 --- a/src/connection/mqtt_c_client.c +++ b/src/connection/mqtt_c_client.c @@ -53,8 +53,8 @@ struct reconnect_state { char * key_file; char * keypass; mqtt_c_client_t *client; - uint8_t send_buf[SEND_BUF_SIZE]; - uint8_t recv_buf[RECV_BUF_SIZE]; + uint8_t * send_buf; + uint8_t * recv_buf; size_t send_buf_sz; size_t recv_buf_sz; X509 * cert; @@ -410,6 +410,14 @@ static void reconnect_state_uninit(struct reconnect_state *state) if (NULL != state->keypass) { free(state->keypass); } + + if (NULL != state->send_buf) { + free(state->send_buf); + } + + if (NULL != state->recv_buf) { + free(state->recv_buf); + } } static void subscribe_on_reconnect(mqtt_c_client_t *client) @@ -584,6 +592,14 @@ static mqtt_c_client_t *client_create(const neu_mqtt_option_t *option, return NULL; } + struct reconnect_state *state = &client->state; + state->send_buf = calloc(1, SEND_BUF_SIZE); + state->recv_buf = calloc(1, RECV_BUF_SIZE); + if (NULL == state->send_buf || NULL == state->recv_buf) { + zlog_error(option->log, "mqtt buff alloc error"); + return NULL; + } + client->option = option; client->user_data = context; client->state_update_func = client->option->state_update_func; @@ -591,8 +607,6 @@ static mqtt_c_client_t *client_create(const neu_mqtt_option_t *option, UT_icd tuple_icd = { sizeof(struct subscribe_tuple *), NULL, NULL, NULL }; utarray_new(client->array, &tuple_icd); - struct reconnect_state *state = &client->state; - if (NULL != client->option->clientid) { state->client_id = strdup(client->option->clientid); } @@ -644,8 +658,8 @@ static mqtt_c_client_t *client_create(const neu_mqtt_option_t *option, } state->client = client; - state->send_buf_sz = sizeof(state->send_buf); - state->recv_buf_sz = sizeof(state->recv_buf); + state->send_buf_sz = SEND_BUF_SIZE; + state->recv_buf_sz = RECV_BUF_SIZE; state->cert = NULL; state->key = NULL; state->ssl_ctx = NULL;