1
0
Fork 0
mirror of https://github.com/warmcat/libwebsockets.git synced 2025-03-09 00:00:04 +01:00

mqtt: allow indicating username and password are not on heap

Fix minimal-mqtt-client and minimal-mqtt-client-multi from crashes
by allowing indicating username and password are not on heap.
This commit is contained in:
Chunho Lee 2021-10-26 17:45:11 -07:00 committed by Andy Green
parent 00b7fa23d0
commit b843c09dc2
4 changed files with 23 additions and 3 deletions

View file

@ -74,6 +74,10 @@ typedef struct lws_mqtt_client_connect_param_s {
session */
uint8_t client_id_nofree:1;
/**< do not free the client id */
uint8_t username_nofree:1;
/**< do not free the username */
uint8_t password_nofree:1;
/**< do not free the password */
struct {
const char *topic;
const char *message;
@ -152,6 +156,8 @@ typedef enum {
/* flags from byte 8 of C_TO_S CONNECT */
typedef enum {
LMQCFT_USERNAME_NOFREE = (1 << 10),
LMQCFT_PASSWORD_NOFREE = (1 << 9),
LMQCFT_CLIENT_ID_NOFREE = (1 << 8),
/* only the low 8 are standardized and go out in the protocol */
LMQCFT_USERNAME = (1 << 7),

View file

@ -122,6 +122,10 @@ lws_create_client_mqtt_object(const struct lws_client_connect_info *i,
c->conn_flags = LMQCFT_CLEAN_START;
if (cp->client_id_nofree)
c->conn_flags |= LMQCFT_CLIENT_ID_NOFREE;
if (cp->username_nofree)
c->conn_flags |= LMQCFT_USERNAME_NOFREE;
if (cp->password_nofree)
c->conn_flags |= LMQCFT_PASSWORD_NOFREE;
if (!(c->conn_flags & LMQCFT_CLIENT_ID_NOFREE))
lws_free((void *)cp->client_id);
@ -152,14 +156,16 @@ lws_create_client_mqtt_object(const struct lws_client_connect_info *i,
if (!c->username)
goto oom3;
c->conn_flags |= LMQCFT_USERNAME;
lws_free((void *)cp->username);
if (!(c->conn_flags & LMQCFT_USERNAME_NOFREE))
lws_free((void *)cp->username);
if (cp->password) {
c->password =
lws_mqtt_str_create_cstr_dup(cp->password, 0);
if (!c->password)
goto oom4;
c->conn_flags |= LMQCFT_PASSWORD;
lws_free((void *)cp->password);
if (!(c->conn_flags & LMQCFT_PASSWORD_NOFREE))
lws_free((void *)cp->password);
}
}

View file

@ -53,6 +53,9 @@ static const lws_mqtt_client_connect_param_t client_connect_param = {
.client_id = NULL,
.keep_alive = 60,
.clean_start = 1,
.client_id_nofree = 1,
.username_nofree = 1,
.password_nofree = 1,
.will_param = {
.topic = "good/bye",
.message = "sign-off",

View file

@ -42,6 +42,8 @@ static const lws_mqtt_client_connect_param_t client_connect_param = {
.keep_alive = 60,
.clean_start = 1,
.client_id_nofree = 1,
.username_nofree = 1,
.password_nofree = 1,
.will_param = {
.topic = "good/bye",
.message = "sign-off",
@ -184,6 +186,7 @@ callback_mqtt(struct lws *wsi, enum lws_callback_reasons reason,
case LWS_CALLBACK_MQTT_SUBSCRIBED:
lwsl_user("%s: MQTT_SUBSCRIBED\n", __func__);
lws_callback_on_writable(wsi);
break;
case LWS_CALLBACK_MQTT_CLIENT_WRITEABLE:
@ -250,8 +253,10 @@ callback_mqtt(struct lws *wsi, enum lws_callback_reasons reason,
*/
pss->state++;
if (pss->state != STATE_TEST_FINISH)
if (pss->state != STATE_TEST_FINISH) {
lws_callback_on_writable(wsi);
break;
}
/* Oh we are done then */