From 3066f89a242562da657a5de27de7738b6d70a6ec Mon Sep 17 00:00:00 2001 From: "Roger A. Light" Date: Sun, 11 Feb 2018 20:47:17 +0000 Subject: [PATCH] packet__read_string() returns the string length. --- lib/handle_publish.c | 5 +++-- lib/packet_mosq.c | 17 +++++++++-------- lib/packet_mosq.h | 2 +- src/handle_connect.c | 17 ++++++++--------- src/handle_publish.c | 5 +++-- src/handle_subscribe.c | 9 +++++---- src/handle_unsubscribe.c | 7 ++++--- 7 files changed, 33 insertions(+), 29 deletions(-) diff --git a/lib/handle_publish.c b/lib/handle_publish.c index bd4953e8..f4484105 100644 --- a/lib/handle_publish.c +++ b/lib/handle_publish.c @@ -33,6 +33,7 @@ int handle__publish(struct mosquitto *mosq) struct mosquitto_message_all *message; int rc = 0; uint16_t mid; + int slen; assert(mosq); @@ -45,12 +46,12 @@ int handle__publish(struct mosquitto *mosq) message->msg.qos = (header & 0x06)>>1; message->msg.retain = (header & 0x01); - rc = packet__read_string(&mosq->in_packet, &message->msg.topic); + rc = packet__read_string(&mosq->in_packet, &message->msg.topic, &slen); if(rc){ message__cleanup(&message); return rc; } - if(!strlen(message->msg.topic)){ + if(!slen){ message__cleanup(&message); return MOSQ_ERR_PROTOCOL; } diff --git a/lib/packet_mosq.c b/lib/packet_mosq.c index 87a5ab88..09c84fa0 100644 --- a/lib/packet_mosq.c +++ b/lib/packet_mosq.c @@ -192,26 +192,27 @@ void packet__write_bytes(struct mosquitto__packet *packet, const void *bytes, ui } -int packet__read_string(struct mosquitto__packet *packet, char **str) +int packet__read_string(struct mosquitto__packet *packet, char **str, int *length) { - uint16_t len; + uint16_t slen; int rc; assert(packet); - rc = packet__read_uint16(packet, &len); + rc = packet__read_uint16(packet, &slen); if(rc) return rc; - if(packet->pos+len > packet->remaining_length) return MOSQ_ERR_PROTOCOL; + if(packet->pos+slen > packet->remaining_length) return MOSQ_ERR_PROTOCOL; - *str = mosquitto__malloc(len+1); + *str = mosquitto__malloc(slen+1); if(*str){ - memcpy(*str, &(packet->payload[packet->pos]), len); - (*str)[len] = '\0'; - packet->pos += len; + memcpy(*str, &(packet->payload[packet->pos]), slen); + (*str)[slen] = '\0'; + packet->pos += slen; }else{ return MOSQ_ERR_NOMEM; } + *length = slen; return MOSQ_ERR_SUCCESS; } diff --git a/lib/packet_mosq.h b/lib/packet_mosq.h index 44a170d7..f56b439f 100644 --- a/lib/packet_mosq.h +++ b/lib/packet_mosq.h @@ -29,7 +29,7 @@ int packet__queue(struct mosquitto *mosq, struct mosquitto__packet *packet); int packet__read_byte(struct mosquitto__packet *packet, uint8_t *byte); int packet__read_bytes(struct mosquitto__packet *packet, void *bytes, uint32_t count); -int packet__read_string(struct mosquitto__packet *packet, char **str); +int packet__read_string(struct mosquitto__packet *packet, char **str, int *length); int packet__read_uint16(struct mosquitto__packet *packet, uint16_t *word); void packet__write_byte(struct mosquitto__packet *packet, uint8_t byte); diff --git a/src/handle_connect.c b/src/handle_connect.c index b9c755a9..4fb59d2d 100644 --- a/src/handle_connect.c +++ b/src/handle_connect.c @@ -139,7 +139,7 @@ int handle__connect(struct mosquitto_db *db, struct mosquitto *context) goto handle_connect_error; } - if(packet__read_string(&context->in_packet, &protocol_name)){ + if(packet__read_string(&context->in_packet, &protocol_name, &slen)){ rc = 1; goto handle_connect_error; return 1; @@ -221,12 +221,11 @@ int handle__connect(struct mosquitto_db *db, struct mosquitto *context) goto handle_connect_error; } - if(packet__read_string(&context->in_packet, &client_id)){ + if(packet__read_string(&context->in_packet, &client_id, &slen)){ rc = 1; goto handle_connect_error; } - slen = strlen(client_id); if(slen == 0){ if(context->protocol == mosq_p_mqtt31){ send__connack(context, 0, CONNACK_REFUSED_IDENTIFIER_REJECTED); @@ -259,7 +258,7 @@ int handle__connect(struct mosquitto_db *db, struct mosquitto *context) } } - if(mosquitto_validate_utf8(client_id, strlen(client_id)) != MOSQ_ERR_SUCCESS){ + if(mosquitto_validate_utf8(client_id, slen) != MOSQ_ERR_SUCCESS){ rc = 1; goto handle_connect_error; } @@ -270,11 +269,11 @@ int handle__connect(struct mosquitto_db *db, struct mosquitto *context) rc = MOSQ_ERR_NOMEM; goto handle_connect_error; } - if(packet__read_string(&context->in_packet, &will_topic)){ + if(packet__read_string(&context->in_packet, &will_topic, &slen)){ rc = 1; goto handle_connect_error; } - if(STREMPTY(will_topic)){ + if(!slen){ rc = 1; goto handle_connect_error; } @@ -325,15 +324,15 @@ int handle__connect(struct mosquitto_db *db, struct mosquitto *context) } if(username_flag){ - rc = packet__read_string(&context->in_packet, &username); + rc = packet__read_string(&context->in_packet, &username, &slen); if(rc == MOSQ_ERR_SUCCESS){ - if(mosquitto_validate_utf8(username, strlen(username)) != MOSQ_ERR_SUCCESS){ + if(mosquitto_validate_utf8(username, slen) != MOSQ_ERR_SUCCESS){ rc = MOSQ_ERR_PROTOCOL; goto handle_connect_error; } if(password_flag){ - rc = packet__read_string(&context->in_packet, &password); + rc = packet__read_string(&context->in_packet, &password, &slen); if(rc == MOSQ_ERR_NOMEM){ rc = MOSQ_ERR_NOMEM; goto handle_connect_error; diff --git a/src/handle_publish.c b/src/handle_publish.c index 9b9386a0..07b00914 100644 --- a/src/handle_publish.c +++ b/src/handle_publish.c @@ -42,6 +42,7 @@ int handle__publish(struct mosquitto_db *db, struct mosquitto *context) int res = 0; struct mosquitto_msg_store *stored = NULL; int len; + int slen; char *topic_mount; #ifdef WITH_BRIDGE char *topic_temp; @@ -61,8 +62,8 @@ int handle__publish(struct mosquitto_db *db, struct mosquitto *context) } retain = (header & 0x01); - if(packet__read_string(&context->in_packet, &topic)) return 1; - if(STREMPTY(topic)){ + if(packet__read_string(&context->in_packet, &topic, &slen)) return 1; + if(!slen){ /* Invalid publish topic, disconnect client. */ mosquitto__free(topic); return 1; diff --git a/src/handle_subscribe.c b/src/handle_subscribe.c index 52cfa2b7..3a96ae2b 100644 --- a/src/handle_subscribe.c +++ b/src/handle_subscribe.c @@ -35,6 +35,7 @@ int handle__subscribe(struct mosquitto_db *db, struct mosquitto *context) uint8_t *payload = NULL, *tmp_payload; uint32_t payloadlen = 0; int len; + int slen; char *sub_mount; if(!context) return MOSQ_ERR_INVAL; @@ -50,13 +51,13 @@ int handle__subscribe(struct mosquitto_db *db, struct mosquitto *context) while(context->in_packet.pos < context->in_packet.remaining_length){ sub = NULL; - if(packet__read_string(&context->in_packet, &sub)){ + if(packet__read_string(&context->in_packet, &sub, &slen)){ mosquitto__free(payload); return 1; } if(sub){ - if(STREMPTY(sub)){ + if(!slen){ log__printf(NULL, MOSQ_LOG_INFO, "Empty subscription string from %s, disconnecting.", context->address); @@ -72,7 +73,7 @@ int handle__subscribe(struct mosquitto_db *db, struct mosquitto *context) mosquitto__free(payload); return 1; } - if(mosquitto_validate_utf8(sub, strlen(sub))){ + if(mosquitto_validate_utf8(sub, slen)){ log__printf(NULL, MOSQ_LOG_INFO, "Malformed UTF-8 in subscription string from %s, disconnecting.", context->id); @@ -94,7 +95,7 @@ int handle__subscribe(struct mosquitto_db *db, struct mosquitto *context) return 1; } if(context->listener && context->listener->mount_point){ - len = strlen(context->listener->mount_point) + strlen(sub) + 1; + len = strlen(context->listener->mount_point) + slen + 1; sub_mount = mosquitto__malloc(len+1); if(!sub_mount){ mosquitto__free(sub); diff --git a/src/handle_unsubscribe.c b/src/handle_unsubscribe.c index be381c98..5995b421 100644 --- a/src/handle_unsubscribe.c +++ b/src/handle_unsubscribe.c @@ -35,6 +35,7 @@ int handle__unsubscribe(struct mosquitto_db *db, struct mosquitto *context) { uint16_t mid; char *sub; + int slen; if(!context) return MOSQ_ERR_INVAL; log__printf(NULL, MOSQ_LOG_DEBUG, "Received UNSUBSCRIBE from %s", context->id); @@ -54,12 +55,12 @@ int handle__unsubscribe(struct mosquitto_db *db, struct mosquitto *context) } while(context->in_packet.pos < context->in_packet.remaining_length){ sub = NULL; - if(packet__read_string(&context->in_packet, &sub)){ + if(packet__read_string(&context->in_packet, &sub, &slen)){ return 1; } if(sub){ - if(STREMPTY(sub)){ + if(!slen){ log__printf(NULL, MOSQ_LOG_INFO, "Empty unsubscription string from %s, disconnecting.", context->id); @@ -73,7 +74,7 @@ int handle__unsubscribe(struct mosquitto_db *db, struct mosquitto *context) mosquitto__free(sub); return 1; } - if(mosquitto_validate_utf8(sub, strlen(sub))){ + if(mosquitto_validate_utf8(sub, slen)){ log__printf(NULL, MOSQ_LOG_INFO, "Malformed UTF-8 in unsubscription string from %s, disconnecting.", context->id);