From 4f61f6c16112eacfdb397be12b93b32c39f2e30c Mon Sep 17 00:00:00 2001 From: "Roger A. Light" Date: Sat, 17 Oct 2020 01:23:08 +0100 Subject: [PATCH] Fix conversion errors. --- apps/db_dump/db_dump.c | 8 +- apps/db_dump/db_dump.h | 8 +- apps/db_dump/print.c | 6 +- apps/db_dump/stubs.c | 6 +- apps/mosquitto_passwd/mosquitto_passwd.c | 20 +-- client/client_shared.c | 2 +- client/sub_client_output.c | 12 +- config.mk | 2 +- include/mosquitto_broker.h | 16 +- include/mqtt_protocol.h | 30 ++-- lib/actions.c | 24 +-- lib/alias_mosq.c | 6 +- lib/alias_mosq.h | 4 +- lib/connect.c | 11 +- lib/handle_publish.c | 12 +- lib/handle_suback.c | 4 +- lib/helpers.c | 2 +- lib/logging_mosq.c | 2 +- lib/logging_mosq.h | 2 +- lib/loop.c | 12 +- lib/messages_mosq.c | 14 +- lib/misc_mosq.c | 2 +- lib/mosquitto.c | 16 +- lib/mosquitto_internal.h | 4 +- lib/net_mosq.c | 10 +- lib/options.c | 20 ++- lib/packet_datatypes.c | 30 ++-- lib/packet_mosq.c | 14 +- lib/packet_mosq.h | 10 +- lib/property_mosq.c | 62 ++++---- lib/property_mosq.h | 8 +- lib/send_connect.c | 30 ++-- lib/send_mosq.h | 4 +- lib/send_publish.c | 14 +- lib/send_subscribe.c | 18 ++- lib/send_unsubscribe.c | 18 ++- lib/socks_mosq.c | 34 ++-- lib/tls_mosq.c | 4 +- lib/util_mosq.c | 2 +- lib/will_mosq.c | 6 +- src/bridge.c | 6 +- src/bridge_topic.c | 10 +- src/conf.c | 99 +++++++----- src/conf_includedir.c | 6 +- src/context.c | 8 +- src/control.c | 4 +- src/database.c | 12 +- src/handle_connect.c | 31 ++-- src/handle_publish.c | 8 +- src/handle_subscribe.c | 4 +- src/handle_unsubscribe.c | 6 +- src/logging.c | 20 +-- src/loop.c | 8 +- src/mosquitto.c | 6 +- src/mosquitto_broker_internal.h | 37 +++-- src/net.c | 20 +-- src/password_mosq.c | 16 +- src/persist.h | 12 +- src/persist_read.c | 12 +- src/persist_read_v234.c | 8 +- src/persist_read_v5.c | 8 +- src/persist_write.c | 22 +-- src/persist_write_v5.c | 10 +- src/plugin_public.c | 4 +- src/retain.c | 18 +-- src/security.c | 2 +- src/security_default.c | 24 +-- src/send_auth.c | 2 +- src/send_connack.c | 2 +- src/send_unsuback.c | 4 +- src/subs.c | 40 +++-- src/sys_tree.c | 126 +++++++-------- src/sys_tree.h | 4 +- src/topic_tok.c | 4 +- src/will_delay.c | 2 +- test/unit/Makefile | 2 +- test/unit/datatype_read.c | 80 +++++----- test/unit/datatype_write.c | 12 +- test/unit/persist_read_stubs.c | 8 +- test/unit/persist_read_test.c | 2 +- test/unit/persist_write_stubs.c | 6 +- test/unit/persist_write_test.c | 2 +- test/unit/property_add.c | 3 +- test/unit/property_read.c | 44 +++--- test/unit/property_user_read.c | 2 +- test/unit/property_write.c | 22 +-- test/unit/stubs.c | 2 +- test/unit/subs_stubs.c | 12 +- test/unit/utf8.c | 192 +++++++++++------------ 89 files changed, 779 insertions(+), 724 deletions(-) diff --git a/apps/db_dump/db_dump.c b/apps/db_dump/db_dump.c index 0d10149c..faddd441 100644 --- a/apps/db_dump/db_dump.c +++ b/apps/db_dump/db_dump.c @@ -41,8 +41,8 @@ struct client_data { UT_hash_handle hh_id; char *id; - int subscriptions; - int subscription_size; + uint32_t subscriptions; + uint32_t subscription_size; int messages; long message_size; }; @@ -387,8 +387,8 @@ int main(int argc, char *argv[]) int rc = 0; uint32_t crc; uint32_t i32temp; - int length; - int chunk; + uint32_t length; + uint32_t chunk; struct mosquitto_db db; char *filename; struct client_data *cc, *cc_tmp; diff --git a/apps/db_dump/db_dump.h b/apps/db_dump/db_dump.h index 8b233606..fec0390f 100644 --- a/apps/db_dump/db_dump.h +++ b/apps/db_dump/db_dump.h @@ -18,9 +18,9 @@ Contributors: #include -void print__client(struct P_client *chunk, int length); -void print__client_msg(struct P_client_msg *chunk, int length); -void print__msg_store(struct P_msg_store *chunk, int length); -void print__sub(struct P_sub *chunk, int length); +void print__client(struct P_client *chunk, uint32_t length); +void print__client_msg(struct P_client_msg *chunk, uint32_t length); +void print__msg_store(struct P_msg_store *chunk, uint32_t length); +void print__sub(struct P_sub *chunk, uint32_t length); #endif diff --git a/apps/db_dump/print.c b/apps/db_dump/print.c index 414f931d..c449352a 100644 --- a/apps/db_dump/print.c +++ b/apps/db_dump/print.c @@ -193,7 +193,7 @@ void print__msg_store(struct P_msg_store *chunk, int length) payload = UHPA_ACCESS(chunk->payload, chunk->F.payloadlen); if(chunk->F.payloadlen < 256){ /* Print payloads with UTF-8 data below an arbitrary limit of 256 bytes */ - if(mosquitto_validate_utf8((char *)payload, chunk->F.payloadlen) == MOSQ_ERR_SUCCESS){ + if(mosquitto_validate_utf8((char *)payload, (uint16_t)chunk->F.payloadlen) == MOSQ_ERR_SUCCESS){ printf("\tPayload: %s\n", payload); } } @@ -201,10 +201,10 @@ void print__msg_store(struct P_msg_store *chunk, int length) } -void print__sub(struct P_sub *chunk, int length) +void print__sub(struct P_sub *chunk, uint32_t length) { printf("DB_CHUNK_SUB:\n"); - printf("\tLength: %d\n", length); + printf("\tLength: %u\n", length); printf("\tClient ID: %s\n", chunk->client_id); printf("\tTopic: %s\n", chunk->topic); printf("\tQoS: %d\n", chunk->F.qos); diff --git a/apps/db_dump/stubs.c b/apps/db_dump/stubs.c index 868c1905..db5a1aa7 100644 --- a/apps/db_dump/stubs.c +++ b/apps/db_dump/stubs.c @@ -23,7 +23,7 @@ int handle__packet(struct mosquitto_db *db, struct mosquitto *context) return 0; } -int log__printf(struct mosquitto *mosq, int level, const char *fmt, ...) +int log__printf(struct mosquitto *mosq, unsigned int level, const char *fmt, ...) { return 0; } @@ -53,12 +53,12 @@ int retain__store(struct mosquitto_db *db, const char *topic, struct mosquitto_m return 0; } -int sub__add(struct mosquitto_db *db, struct mosquitto *context, const char *sub, int qos, uint32_t identifier, int options, struct mosquitto__subhier **root) +int sub__add(struct mosquitto_db *db, struct mosquitto *context, const char *sub, uint8_t qos, uint32_t identifier, int options, struct mosquitto__subhier **root) { return 0; } -int sub__messages_queue(struct mosquitto_db *db, const char *source_id, const char *topic, int qos, int retain, struct mosquitto_msg_store **stored) +int sub__messages_queue(struct mosquitto_db *db, const char *source_id, const char *topic, uint8_t qos, int retain, struct mosquitto_msg_store **stored) { return 0; } diff --git a/apps/mosquitto_passwd/mosquitto_passwd.c b/apps/mosquitto_passwd/mosquitto_passwd.c index 0164ce4e..b40d5192 100644 --- a/apps/mosquitto_passwd/mosquitto_passwd.c +++ b/apps/mosquitto_passwd/mosquitto_passwd.c @@ -69,7 +69,7 @@ static FILE *mpw_tmpfile(void) } #else -static char alphanum[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; +static char unsigned alphanum[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; static unsigned char tmpfile_path[36]; static FILE *mpw_tmpfile(void) @@ -167,13 +167,13 @@ static int pwfile_iterate(FILE *fptr, FILE *ftmp, int line = 0; char *username, *password; - buf = malloc(buflen); + buf = malloc((size_t)buflen); if(buf == NULL){ fprintf(stderr, "Error: Out of memory.\n"); return 1; } lbuflen = buflen; - lbuf = malloc(lbuflen); + lbuf = malloc((size_t)lbuflen); if(lbuf == NULL){ fprintf(stderr, "Error: Out of memory.\n"); free(buf); @@ -184,14 +184,14 @@ static int pwfile_iterate(FILE *fptr, FILE *ftmp, if(lbuflen != buflen){ free(lbuf); lbuflen = buflen; - lbuf = malloc(lbuflen); + lbuf = malloc((size_t)lbuflen); if(lbuf == NULL){ fprintf(stderr, "Error: Out of memory.\n"); free(buf); return 1; } } - memcpy(lbuf, buf, buflen); + memcpy(lbuf, buf, (size_t)buflen); line++; username = strtok(buf, ":"); password = strtok(NULL, ":"); @@ -337,10 +337,10 @@ int gets_quiet(char *s, int len) struct termios ts_quiet, ts_orig; char *rs; - memset(s, 0, len); + memset(s, 0, (size_t)len); tcgetattr(0, &ts_orig); ts_quiet = ts_orig; - ts_quiet.c_lflag &= ~(ECHO | ICANON); + ts_quiet.c_lflag &= (unsigned int)(~(ECHO | ICANON)); tcsetattr(0, TCSANOW, &ts_quiet); rs = fgets(s, len, stdin); @@ -368,7 +368,7 @@ int get_password(char *password, size_t len) printf("Password: "); fflush(stdout); - if(gets_quiet(pw1, minLen)){ + if(gets_quiet(pw1, (int)minLen)){ fprintf(stderr, "Error: Empty password.\n"); return 1; } @@ -376,7 +376,7 @@ int get_password(char *password, size_t len) printf("Reenter password: "); fflush(stdout); - if(gets_quiet(pw2, minLen)){ + if(gets_quiet(pw2, (int)minLen)){ fprintf(stderr, "Error: Empty password.\n"); return 1; } @@ -633,7 +633,7 @@ int main(int argc, char *argv[]) return 1; } - backup_file = malloc(strlen(password_file)+5); + backup_file = malloc((size_t)strlen(password_file)+5); if(!backup_file){ fprintf(stderr, "Error: Out of memory.\n"); free(password_file); diff --git a/client/client_shared.c b/client/client_shared.c index dae4e2d1..091bd799 100644 --- a/client/client_shared.c +++ b/client/client_shared.c @@ -917,7 +917,7 @@ int client_config_line_proc(struct mosq_config *cfg, int pub_or_sub, int argc, c fprintf(stderr, "Error: --random-filter argument given but no chance specified.\n\n"); return 1; }else{ - cfg->random_filter = 10.0*atof(argv[i+1]); + cfg->random_filter = (int)(10.0*atof(argv[i+1])); if(cfg->random_filter > 10000 || cfg->random_filter < 1){ fprintf(stderr, "Error: --random-filter chance must be between 0.1-100.0\n\n"); return 1; diff --git a/client/sub_client_output.c b/client/sub_client_output.c index 378f0193..f0a2cff4 100644 --- a/client/sub_client_output.c +++ b/client/sub_client_output.c @@ -474,7 +474,7 @@ static void formatted_print_percent(const struct mosq_config *lcfg, const struct case 'E': if(mosquitto_property_read_int32(properties, MQTT_PROP_MESSAGE_EXPIRY_INTERVAL, &i32value, false)){ - formatted_print_int(i32value, align, pad, field_width); + formatted_print_int((int)i32value, align, pad, field_width); }else{ formatted_print_blank(pad, field_width); } @@ -509,7 +509,7 @@ static void formatted_print_percent(const struct mosq_config *lcfg, const struct return; } } - if(json_print(message, properties, ti, ns, true, lcfg->pretty) != MOSQ_ERR_SUCCESS){ + if(json_print(message, properties, ti, (int)ns, true, lcfg->pretty) != MOSQ_ERR_SUCCESS){ err_printf(lcfg, "Error: Out of memory.\n"); return; } @@ -522,7 +522,7 @@ static void formatted_print_percent(const struct mosq_config *lcfg, const struct return; } } - rc = json_print(message, properties, ti, ns, false, lcfg->pretty); + rc = json_print(message, properties, ti, (int)ns, false, lcfg->pretty); if(rc == MOSQ_ERR_NOMEM){ err_printf(lcfg, "Error: Out of memory.\n"); return; @@ -585,7 +585,7 @@ static void formatted_print_percent(const struct mosq_config *lcfg, const struct case 'S': if(mosquitto_property_read_varint(properties, MQTT_PROP_SUBSCRIPTION_IDENTIFIER, &i32value, false)){ - formatted_print_int(i32value, align, pad, field_width); + formatted_print_int((int)i32value, align, pad, field_width); }else{ formatted_print_blank(pad, field_width); } @@ -620,7 +620,7 @@ static void formatted_print_percent(const struct mosq_config *lcfg, const struct static void formatted_print(const struct mosq_config *lcfg, const struct mosquitto_message *message, const mosquitto_property *properties) { - int len; + size_t len; int i; struct tm *ti = NULL; long ns; @@ -759,7 +759,7 @@ void rand_init(void) long ns; if(!get_time(&ti, &ns)){ - srandom(ns); + srandom((unsigned int)ns); } #endif } diff --git a/config.mk b/config.mk index 8a03b8f1..e7812f9c 100644 --- a/config.mk +++ b/config.mk @@ -139,7 +139,7 @@ ifeq ($(UNAME),SunOS) CFLAGS?=-Wall -ggdb -O2 endif else - CFLAGS?=-Wall -ggdb -O2 + CFLAGS?=-Wall -ggdb -O2 -Wconversion -Werror endif STATIC_LIB_DEPS:= diff --git a/include/mosquitto_broker.h b/include/mosquitto_broker.h index da94fb97..0f6ee3a3 100644 --- a/include/mosquitto_broker.h +++ b/include/mosquitto_broker.h @@ -69,13 +69,13 @@ struct mosquitto_evt_reload { struct mosquitto_evt_acl_check { void *future; struct mosquitto *client; - int access; const char *topic; const void *payload; - long payloadlen; - int qos; - bool retain; mosquitto_property *properties; + int access; + uint32_t payloadlen; + uint8_t qos; + bool retain; void *future2[4]; }; @@ -118,8 +118,8 @@ struct mosquitto_evt_control { const void *payload; const mosquitto_property *properties; char *reason_string; - long payloadlen; - int qos; + uint32_t payloadlen; + uint8_t qos; uint8_t reason_code; bool retain; void *future2[4]; @@ -133,8 +133,8 @@ struct mosquitto_evt_message { void *payload; mosquitto_property *properties; char *reason_string; - long payloadlen; - int qos; + uint32_t payloadlen; + uint8_t qos; uint8_t reason_code; bool retain; void *future2[4]; diff --git a/include/mqtt_protocol.h b/include/mqtt_protocol.h index e1db5ee9..8dbcac83 100644 --- a/include/mqtt_protocol.h +++ b/include/mqtt_protocol.h @@ -27,21 +27,21 @@ Contributors: /* Message types */ -#define CMD_CONNECT 0x10 -#define CMD_CONNACK 0x20 -#define CMD_PUBLISH 0x30 -#define CMD_PUBACK 0x40 -#define CMD_PUBREC 0x50 -#define CMD_PUBREL 0x60 -#define CMD_PUBCOMP 0x70 -#define CMD_SUBSCRIBE 0x80 -#define CMD_SUBACK 0x90 -#define CMD_UNSUBSCRIBE 0xA0 -#define CMD_UNSUBACK 0xB0 -#define CMD_PINGREQ 0xC0 -#define CMD_PINGRESP 0xD0 -#define CMD_DISCONNECT 0xE0 -#define CMD_AUTH 0xF0 +#define CMD_CONNECT 0x10U +#define CMD_CONNACK 0x20U +#define CMD_PUBLISH 0x30U +#define CMD_PUBACK 0x40U +#define CMD_PUBREC 0x50U +#define CMD_PUBREL 0x60U +#define CMD_PUBCOMP 0x70U +#define CMD_SUBSCRIBE 0x80U +#define CMD_SUBACK 0x90U +#define CMD_UNSUBSCRIBE 0xA0U +#define CMD_UNSUBACK 0xB0U +#define CMD_PINGREQ 0xC0U +#define CMD_PINGRESP 0xD0U +#define CMD_DISCONNECT 0xE0U +#define CMD_AUTH 0xF0U /* Mosquitto only: for distinguishing CONNECT and WILL properties */ #define CMD_WILL 0x100 diff --git a/lib/actions.c b/lib/actions.c index 6cfc6b77..e92e934c 100644 --- a/lib/actions.c +++ b/lib/actions.c @@ -44,7 +44,7 @@ int mosquitto_publish_v5(struct mosquitto *mosq, int *mid, const char *topic, in mosquitto_property local_property; bool have_topic_alias; int rc; - int tlen = 0; + size_t tlen = 0; uint32_t remaining_length; if(!mosq || qos<0 || qos>2) return MOSQ_ERR_INVAL; @@ -89,7 +89,7 @@ int mosquitto_publish_v5(struct mosquitto *mosq, int *mid, const char *topic, in } }else{ tlen = strlen(topic); - if(mosquitto_validate_utf8(topic, tlen)) return MOSQ_ERR_MALFORMED_UTF8; + if(mosquitto_validate_utf8(topic, (int)tlen)) return MOSQ_ERR_MALFORMED_UTF8; if(payloadlen < 0 || payloadlen > MQTT_MAX_PAYLOAD) return MOSQ_ERR_PAYLOAD_SIZE; if(mosquitto_pub_topic_check(topic) != MOSQ_ERR_SUCCESS){ return MOSQ_ERR_INVAL; @@ -97,7 +97,7 @@ int mosquitto_publish_v5(struct mosquitto *mosq, int *mid, const char *topic, in } if(mosq->maximum_packet_size > 0){ - remaining_length = 1 + 2+tlen + payloadlen + property__get_length_all(outgoing_properties); + remaining_length = 1 + 2+(uint32_t)tlen + (uint32_t)payloadlen + property__get_length_all(outgoing_properties); if(qos > 0){ remaining_length++; } @@ -112,7 +112,7 @@ int mosquitto_publish_v5(struct mosquitto *mosq, int *mid, const char *topic, in } if(qos == 0){ - return send__publish(mosq, local_mid, topic, payloadlen, payload, qos, retain, false, outgoing_properties, NULL, 0); + return send__publish(mosq, local_mid, topic, (uint32_t)payloadlen, payload, (uint8_t)qos, retain, false, outgoing_properties, NULL, 0); }else{ if(outgoing_properties){ rc = mosquitto_property_copy_all(&properties_copy, outgoing_properties); @@ -137,18 +137,18 @@ int mosquitto_publish_v5(struct mosquitto *mosq, int *mid, const char *topic, in } if(payloadlen){ message->msg.payloadlen = payloadlen; - message->msg.payload = mosquitto__malloc(payloadlen*sizeof(uint8_t)); + message->msg.payload = mosquitto__malloc((unsigned int)payloadlen*sizeof(uint8_t)); if(!message->msg.payload){ message__cleanup(&message); mosquitto_property_free_all(&properties_copy); return MOSQ_ERR_NOMEM; } - memcpy(message->msg.payload, payload, payloadlen*sizeof(uint8_t)); + memcpy(message->msg.payload, payload, (uint32_t)payloadlen*sizeof(uint8_t)); }else{ message->msg.payloadlen = 0; message->msg.payload = NULL; } - message->msg.qos = qos; + message->msg.qos = (uint8_t)qos; message->msg.retain = retain; message->dup = false; message->properties = properties_copy; @@ -204,9 +204,9 @@ int mosquitto_subscribe_multiple(struct mosquitto *mosq, int *mid, int sub_count for(i=0; imaximum_packet_size > 0){ @@ -261,13 +261,13 @@ int mosquitto_unsubscribe_multiple(struct mosquitto *mosq, int *mid, int sub_cou for(i=0; imaximum_packet_size > 0){ - remaining_length += 2 + property__get_length_all(outgoing_properties); + remaining_length += 2U + property__get_length_all(outgoing_properties); if(packet__check_oversize(mosq, remaining_length)){ return MOSQ_ERR_OVERSIZE_PACKET; } diff --git a/lib/alias_mosq.c b/lib/alias_mosq.c index 127cebac..befa5ddd 100644 --- a/lib/alias_mosq.c +++ b/lib/alias_mosq.c @@ -20,7 +20,7 @@ Contributors: #include "alias_mosq.h" #include "memory_mosq.h" -int alias__add(struct mosquitto *mosq, const char *topic, int alias) +int alias__add(struct mosquitto *mosq, const char *topic, uint16_t alias) { int i; struct mosquitto__alias *aliases; @@ -39,7 +39,7 @@ int alias__add(struct mosquitto *mosq, const char *topic, int alias) } /* New alias */ - aliases = mosquitto__realloc(mosq->aliases, sizeof(struct mosquitto__alias)*(mosq->alias_count+1)); + aliases = mosquitto__realloc(mosq->aliases, sizeof(struct mosquitto__alias)*(size_t)(mosq->alias_count+1)); if(!aliases) return MOSQ_ERR_NOMEM; mosq->aliases = aliases; @@ -54,7 +54,7 @@ int alias__add(struct mosquitto *mosq, const char *topic, int alias) } -int alias__find(struct mosquitto *mosq, char **topic, int alias) +int alias__find(struct mosquitto *mosq, char **topic, uint16_t alias) { int i; diff --git a/lib/alias_mosq.h b/lib/alias_mosq.h index b290be4c..74bb4cde 100644 --- a/lib/alias_mosq.h +++ b/lib/alias_mosq.h @@ -19,8 +19,8 @@ Contributors: #include "mosquitto_internal.h" -int alias__add(struct mosquitto *mosq, const char *topic, int alias); -int alias__find(struct mosquitto *mosq, char **topic, int alias); +int alias__add(struct mosquitto *mosq, const char *topic, uint16_t alias); +int alias__find(struct mosquitto *mosq, char **topic, uint16_t alias); void alias__free_all(struct mosquitto *mosq); #endif diff --git a/lib/connect.c b/lib/connect.c index eae1fb0f..efe82907 100644 --- a/lib/connect.c +++ b/lib/connect.c @@ -42,8 +42,8 @@ static int mosquitto__connect_init(struct mosquitto *mosq, const char *host, int int rc; if(!mosq) return MOSQ_ERR_INVAL; - if(!host || port < 0) return MOSQ_ERR_INVAL; - if(keepalive < 5) return MOSQ_ERR_INVAL; + if(!host || port < 0 || port > UINT16_MAX) return MOSQ_ERR_INVAL; + if(keepalive < 5 || keepalive > UINT16_MAX) return MOSQ_ERR_INVAL; /* Only MQTT v3.1 requires a client id to be sent */ if(mosq->id == NULL && (mosq->protocol == mosq_p_mqtt31)){ @@ -68,9 +68,9 @@ static int mosquitto__connect_init(struct mosquitto *mosq, const char *host, int mosquitto__free(mosq->host); mosq->host = mosquitto__strdup(host); if(!mosq->host) return MOSQ_ERR_NOMEM; - mosq->port = port; + mosq->port = (uint16_t)port; - mosq->keepalive = keepalive; + mosq->keepalive = (uint16_t)keepalive; mosq->msgs_in.inflight_quota = mosq->msgs_in.inflight_maximum; mosq->msgs_out.inflight_quota = mosq->msgs_out.inflight_maximum; mosq->retain_available = 1; @@ -233,6 +233,7 @@ int mosquitto_disconnect_v5(struct mosquitto *mosq, int reason_code, const mosqu int rc; if(!mosq) return MOSQ_ERR_INVAL; if(mosq->protocol != mosq_p_mqtt5 && properties) return MOSQ_ERR_NOT_SUPPORTED; + if(reason_code < 0 || reason_code > UINT8_MAX) return MOSQ_ERR_INVAL; if(properties){ if(properties->client_generated){ @@ -251,7 +252,7 @@ int mosquitto_disconnect_v5(struct mosquitto *mosq, int reason_code, const mosqu if(mosq->sock == INVALID_SOCKET){ return MOSQ_ERR_NO_CONN; }else{ - return send__disconnect(mosq, reason_code, outgoing_properties); + return send__disconnect(mosq, (uint8_t)reason_code, outgoing_properties); } } diff --git a/lib/handle_publish.c b/lib/handle_publish.c index 18d1d360..d37aae74 100644 --- a/lib/handle_publish.c +++ b/lib/handle_publish.c @@ -38,7 +38,7 @@ int handle__publish(struct mosquitto *mosq) struct mosquitto_message_all *message; int rc = 0; uint16_t mid; - int slen; + uint16_t slen; mosquitto_property *properties = NULL; int state; @@ -94,15 +94,15 @@ int handle__publish(struct mosquitto *mosq) if(rc) return rc; } - message->msg.payloadlen = mosq->in_packet.remaining_length - mosq->in_packet.pos; + message->msg.payloadlen = (int)(mosq->in_packet.remaining_length - mosq->in_packet.pos); if(message->msg.payloadlen){ - message->msg.payload = mosquitto__calloc(message->msg.payloadlen+1, sizeof(uint8_t)); + message->msg.payload = mosquitto__calloc((size_t)message->msg.payloadlen+1, sizeof(uint8_t)); if(!message->msg.payload){ message__cleanup(&message); mosquitto_property_free_all(&properties); return MOSQ_ERR_NOMEM; } - rc = packet__read_bytes(&mosq->in_packet, message->msg.payload, message->msg.payloadlen); + rc = packet__read_bytes(&mosq->in_packet, message->msg.payload, (uint32_t)message->msg.payloadlen); if(rc){ message__cleanup(&message); mosquitto_property_free_all(&properties); @@ -135,7 +135,7 @@ int handle__publish(struct mosquitto *mosq) return MOSQ_ERR_SUCCESS; case 1: util__decrement_receive_quota(mosq); - rc = send__puback(mosq, message->msg.mid, 0, NULL); + rc = send__puback(mosq, mid, 0, NULL); pthread_mutex_lock(&mosq->callback_mutex); if(mosq->on_message){ mosq->in_callback = true; @@ -154,7 +154,7 @@ int handle__publish(struct mosquitto *mosq) case 2: message->properties = properties; util__decrement_receive_quota(mosq); - rc = send__pubrec(mosq, message->msg.mid, 0, NULL); + rc = send__pubrec(mosq, mid, 0, NULL); pthread_mutex_lock(&mosq->msgs_in.mutex); message->state = mosq_ms_wait_for_pubrel; message__queue(mosq, message, mosq_md_in); diff --git a/lib/handle_suback.c b/lib/handle_suback.c index 47a3e529..2d5a6c87 100644 --- a/lib/handle_suback.c +++ b/lib/handle_suback.c @@ -64,8 +64,8 @@ int handle__suback(struct mosquitto *mosq) if(rc) return rc; } - qos_count = mosq->in_packet.remaining_length - mosq->in_packet.pos; - granted_qos = mosquitto__malloc(qos_count*sizeof(int)); + qos_count = (int)(mosq->in_packet.remaining_length - mosq->in_packet.pos); + granted_qos = mosquitto__malloc((size_t)qos_count*sizeof(int)); if(!granted_qos) return MOSQ_ERR_NOMEM; while(mosq->in_packet.pos < mosq->in_packet.remaining_length){ rc = packet__read_byte(&mosq->in_packet, &qos); diff --git a/lib/helpers.c b/lib/helpers.c index 930eb365..c9daa996 100644 --- a/lib/helpers.c +++ b/lib/helpers.c @@ -112,7 +112,7 @@ libmosq_EXPORT int mosquitto_subscribe_simple( *messages = NULL; - userdata.messages = calloc(sizeof(struct mosquitto_message), msg_count); + userdata.messages = calloc(sizeof(struct mosquitto_message), (size_t)msg_count); if(!userdata.messages){ return MOSQ_ERR_NOMEM; } diff --git a/lib/logging_mosq.c b/lib/logging_mosq.c index 398139cb..80798318 100644 --- a/lib/logging_mosq.c +++ b/lib/logging_mosq.c @@ -29,7 +29,7 @@ int log__printf(struct mosquitto *mosq, int priority, const char *fmt, ...) { va_list va; char *s; - int len; + size_t len; assert(mosq); assert(fmt); diff --git a/lib/logging_mosq.h b/lib/logging_mosq.h index 7738ce41..3dd7e071 100644 --- a/lib/logging_mosq.h +++ b/lib/logging_mosq.h @@ -18,6 +18,6 @@ Contributors: #include "mosquitto.h" -int log__printf(struct mosquitto *mosq, int priority, const char *fmt, ...); +int log__printf(struct mosquitto *mosq, unsigned int priority, const char *fmt, ...); #endif diff --git a/lib/loop.c b/lib/loop.c index a3773b0a..249bbce8 100644 --- a/lib/loop.c +++ b/lib/loop.c @@ -34,7 +34,7 @@ Contributors: #define HAVE_PSELECT #endif -int mosquitto_loop(struct mosquitto *mosq, int timeout, int max_packets) +int mosquitto_loop(struct mosquitto *mosq, int timeout_input, int max_packets) { #ifdef HAVE_PSELECT struct timespec local_timeout; @@ -50,6 +50,10 @@ int mosquitto_loop(struct mosquitto *mosq, int timeout, int max_packets) #ifdef WITH_SRV int state; #endif + time_t timeout; + + UNUSED(timeout_input); + if(!mosq || max_packets < 1) return MOSQ_ERR_INVAL; #ifndef WIN32 @@ -126,7 +130,7 @@ int mosquitto_loop(struct mosquitto *mosq, int timeout, int max_packets) local_timeout.tv_sec = timeout/1000; #ifdef HAVE_PSELECT - local_timeout.tv_nsec = (timeout-local_timeout.tv_sec*1000)*1e6; + local_timeout.tv_nsec = (timeout-local_timeout.tv_sec*1000)*1000000; #else local_timeout.tv_usec = (timeout-local_timeout.tv_sec*1000)*1000; #endif @@ -191,7 +195,7 @@ int mosquitto_loop(struct mosquitto *mosq, int timeout, int max_packets) } -static int interruptible_sleep(struct mosquitto *mosq, unsigned long reconnect_delay) +static int interruptible_sleep(struct mosquitto *mosq, time_t reconnect_delay) { #ifdef HAVE_PSELECT struct timespec local_timeout; @@ -307,7 +311,7 @@ int mosquitto_loop_forever(struct mosquitto *mosq, int timeout, int max_packets) mosq->reconnects++; } - rc = interruptible_sleep(mosq, reconnect_delay); + rc = interruptible_sleep(mosq, (time_t)reconnect_delay); if(rc) return rc; state = mosquitto__get_state(mosq); diff --git a/lib/messages_mosq.c b/lib/messages_mosq.c index a2b7b75c..44bce636 100644 --- a/lib/messages_mosq.c +++ b/lib/messages_mosq.c @@ -69,12 +69,12 @@ int mosquitto_message_copy(struct mosquitto_message *dst, const struct mosquitto dst->qos = src->qos; dst->retain = src->retain; if(src->payloadlen){ - dst->payload = mosquitto__calloc(src->payloadlen+1, sizeof(uint8_t)); + dst->payload = mosquitto__calloc((unsigned int)src->payloadlen+1, sizeof(uint8_t)); if(!dst->payload){ mosquitto__free(dst->topic); return MOSQ_ERR_NOMEM; } - memcpy(dst->payload, src->payload, src->payloadlen); + memcpy(dst->payload, src->payload, (unsigned int)src->payloadlen); dst->payloadlen = src->payloadlen; }else{ dst->payloadlen = 0; @@ -200,7 +200,7 @@ int message__release_to_inflight(struct mosquitto *mosq, enum mosquitto_msg_dire }else if(cur->msg.qos == 2){ cur->state = mosq_ms_wait_for_pubrec; } - rc = send__publish(mosq, cur->msg.mid, cur->msg.topic, cur->msg.payloadlen, cur->msg.payload, cur->msg.qos, cur->msg.retain, cur->dup, cur->properties, NULL, 0); + rc = send__publish(mosq, (uint16_t)cur->msg.mid, cur->msg.topic, (uint32_t)cur->msg.payloadlen, cur->msg.payload, (uint8_t)cur->msg.qos, cur->msg.retain, cur->dup, cur->properties, NULL, 0); if(rc){ return rc; } @@ -287,18 +287,18 @@ void message__retry_check(struct mosquitto *mosq) case mosq_ms_publish_qos2: msg->timestamp = now; msg->dup = true; - send__publish(mosq, msg->msg.mid, msg->msg.topic, msg->msg.payloadlen, msg->msg.payload, msg->msg.qos, msg->msg.retain, msg->dup, msg->properties, NULL, 0); + send__publish(mosq, (uint16_t)msg->msg.mid, msg->msg.topic, (uint32_t)msg->msg.payloadlen, msg->msg.payload, (uint8_t)msg->msg.qos, msg->msg.retain, msg->dup, msg->properties, NULL, 0); break; case mosq_ms_wait_for_pubrel: msg->timestamp = now; msg->dup = true; - send__pubrec(mosq, msg->msg.mid, 0, NULL); + send__pubrec(mosq, (uint16_t)msg->msg.mid, 0, NULL); break; case mosq_ms_resend_pubrel: case mosq_ms_wait_for_pubcomp: msg->timestamp = now; msg->dup = true; - send__pubrel(mosq, msg->msg.mid, NULL); + send__pubrel(mosq, (uint16_t)msg->msg.mid, NULL); break; default: break; @@ -340,6 +340,6 @@ int message__out_update(struct mosquitto *mosq, uint16_t mid, enum mosquitto_msg int mosquitto_max_inflight_messages_set(struct mosquitto *mosq, unsigned int max_inflight_messages) { - return mosquitto_int_option(mosq, MOSQ_OPT_SEND_MAXIMUM, max_inflight_messages); + return mosquitto_int_option(mosq, MOSQ_OPT_SEND_MAXIMUM, (int)max_inflight_messages); } diff --git a/lib/misc_mosq.c b/lib/misc_mosq.c index c12e45b3..9343c908 100644 --- a/lib/misc_mosq.c +++ b/lib/misc_mosq.c @@ -168,7 +168,7 @@ char *fgets_extending(char **buf, int *buflen, FILE *stream) /* No EOL char found, so extend buffer */ offset = (*buflen)-1; *buflen += 1000; - newbuf = realloc(*buf, *buflen); + newbuf = realloc(*buf, (size_t)*buflen); if(!newbuf){ return NULL; } diff --git a/lib/mosquitto.c b/lib/mosquitto.c index 46ddd756..9a8fd66c 100644 --- a/lib/mosquitto.c +++ b/lib/mosquitto.c @@ -61,7 +61,7 @@ int mosquitto_lib_init(void) struct timespec tp; clock_gettime(CLOCK_MONOTONIC, &tp); - srand(tp.tv_nsec); + srand((unsigned int)tp.tv_nsec); #elif defined(__APPLE__) uint64_t ticks; @@ -159,7 +159,7 @@ int mosquitto_reinitialise(struct mosquitto *mosq, const char *id, bool clean_st if(STREMPTY(id)){ return MOSQ_ERR_INVAL; } - if(mosquitto_validate_utf8(id, strlen(id))){ + if(mosquitto_validate_utf8(id, (int)strlen(id))){ return MOSQ_ERR_MALFORMED_UTF8; } mosq->id = mosquitto__strdup(id); @@ -570,12 +570,12 @@ int mosquitto_string_to_command(const char *str, int *cmd) int mosquitto_sub_topic_tokenise(const char *subtopic, char ***topics, int *count) { - int len; - int hier_count = 1; - int start, stop; + size_t len; + size_t hier_count = 1; + size_t start, stop; int hier; - int tlen; - int i, j; + size_t tlen; + size_t i, j; if(!subtopic || !topics || !count) return MOSQ_ERR_INVAL; @@ -619,7 +619,7 @@ int mosquitto_sub_topic_tokenise(const char *subtopic, char ***topics, int *coun } } - *count = hier_count; + *count = (int)hier_count; return MOSQ_ERR_SUCCESS; } diff --git a/lib/mosquitto_internal.h b/lib/mosquitto_internal.h index 47b19a37..f1a5bb1f 100644 --- a/lib/mosquitto_internal.h +++ b/lib/mosquitto_internal.h @@ -298,7 +298,7 @@ struct mosquitto { #else # ifdef WITH_SOCKS char *socks5_host; - int socks5_port; + uint16_t socks5_port; char *socks5_username; char *socks5_password; # endif @@ -322,7 +322,7 @@ struct mosquitto { void (*on_log)(struct mosquitto *, void *userdata, int level, const char *str); /*void (*on_error)();*/ char *host; - int port; + uint16_t port; char *bind_address; unsigned int reconnects; unsigned int reconnect_delay; diff --git a/lib/net_mosq.c b/lib/net_mosq.c index b5efec2e..6743d768 100644 --- a/lib/net_mosq.c +++ b/lib/net_mosq.c @@ -268,9 +268,9 @@ static unsigned int psk_client_callback(SSL *ssl, const char *hint, snprintf(identity, max_identity_len, "%s", mosq->tls_psk_identity); - len = mosquitto__hex2bin(mosq->tls_psk, psk, max_psk_len); + len = mosquitto__hex2bin(mosq->tls_psk, psk, (int)max_psk_len); if (len < 0) return 0; - return len; + return (unsigned int)len; } #endif @@ -721,7 +721,7 @@ static int net__init_ssl_ctx(struct mosquitto *mosq) tls_alpn_len = (uint8_t) strnlen(mosq->tls_alpn, 254); tls_alpn_wire[0] = tls_alpn_len; /* first byte is length of string */ memcpy(tls_alpn_wire + 1, mosq->tls_alpn, tls_alpn_len); - SSL_CTX_set_alpn_protos(mosq->ssl_ctx, tls_alpn_wire, tls_alpn_len + 1); + SSL_CTX_set_alpn_protos(mosq->ssl_ctx, tls_alpn_wire, tls_alpn_len + 1U); } #ifdef SSL_MODE_RELEASE_BUFFERS @@ -974,7 +974,7 @@ ssize_t net__read(struct mosquitto *mosq, void *buf, size_t count) errno = 0; #ifdef WITH_TLS if(mosq->ssl){ - ret = SSL_read(mosq->ssl, buf, count); + ret = SSL_read(mosq->ssl, buf, (int)count); if(ret <= 0){ err = SSL_get_error(mosq->ssl, ret); if(err == SSL_ERROR_WANT_READ){ @@ -1022,7 +1022,7 @@ ssize_t net__write(struct mosquitto *mosq, void *buf, size_t count) #ifdef WITH_TLS if(mosq->ssl){ mosq->want_write = false; - ret = SSL_write(mosq->ssl, buf, count); + ret = SSL_write(mosq->ssl, buf, (int)count); if(ret < 0){ err = SSL_get_error(mosq->ssl, ret); if(err == SSL_ERROR_WANT_READ){ diff --git a/lib/options.c b/lib/options.c index 7cdbf978..5e934153 100644 --- a/lib/options.c +++ b/lib/options.c @@ -68,6 +68,8 @@ int mosquitto_will_clear(struct mosquitto *mosq) int mosquitto_username_pw_set(struct mosquitto *mosq, const char *username, const char *password) { + size_t slen; + if(!mosq) return MOSQ_ERR_INVAL; if(mosq->protocol == mosq_p_mqtt311 || mosq->protocol == mosq_p_mqtt31){ @@ -83,7 +85,11 @@ int mosquitto_username_pw_set(struct mosquitto *mosq, const char *username, cons mosq->password = NULL; if(username){ - if(mosquitto_validate_utf8(username, strlen(username))){ + slen = strlen(username); + if(slen > UINT16_MAX){ + return MOSQ_ERR_INVAL; + } + if(mosquitto_validate_utf8(username, (int)slen)){ return MOSQ_ERR_MALFORMED_UTF8; } mosq->username = mosquitto__strdup(username); @@ -421,24 +427,24 @@ int mosquitto_int_option(struct mosquitto *mosq, enum mosq_opt_t option, int val break; case MOSQ_OPT_RECEIVE_MAXIMUM: - if(value < 0 || value > 65535){ + if(value < 0 || value > UINT16_MAX){ return MOSQ_ERR_INVAL; } if(value == 0){ - mosq->msgs_in.inflight_maximum = 65535; + mosq->msgs_in.inflight_maximum = UINT16_MAX; }else{ - mosq->msgs_in.inflight_maximum = value; + mosq->msgs_in.inflight_maximum = (uint16_t)value; } break; case MOSQ_OPT_SEND_MAXIMUM: - if(value < 0 || value > 65535){ + if(value < 0 || value > UINT16_MAX){ return MOSQ_ERR_INVAL; } if(value == 0){ - mosq->msgs_out.inflight_maximum = 65535; + mosq->msgs_out.inflight_maximum = UINT16_MAX; }else{ - mosq->msgs_out.inflight_maximum = value; + mosq->msgs_out.inflight_maximum = (uint16_t)value; } break; diff --git a/lib/packet_datatypes.c b/lib/packet_datatypes.c index 724f8f97..4bdea671 100644 --- a/lib/packet_datatypes.c +++ b/lib/packet_datatypes.c @@ -88,7 +88,7 @@ void packet__write_bytes(struct mosquitto__packet *packet, const void *bytes, ui } -int packet__read_binary(struct mosquitto__packet *packet, uint8_t **data, int *length) +int packet__read_binary(struct mosquitto__packet *packet, uint8_t **data, uint16_t *length) { uint16_t slen; int rc; @@ -105,7 +105,7 @@ int packet__read_binary(struct mosquitto__packet *packet, uint8_t **data, int *l if(packet->pos+slen > packet->remaining_length) return MOSQ_ERR_PROTOCOL; - *data = mosquitto__malloc(slen+1); + *data = mosquitto__malloc(slen+1U); if(*data){ memcpy(*data, &(packet->payload[packet->pos]), slen); ((uint8_t *)(*data))[slen] = '\0'; @@ -119,7 +119,7 @@ int packet__read_binary(struct mosquitto__packet *packet, uint8_t **data, int *l } -int packet__read_string(struct mosquitto__packet *packet, char **str, int *length) +int packet__read_string(struct mosquitto__packet *packet, char **str, uint16_t *length) { int rc; @@ -130,7 +130,7 @@ int packet__read_string(struct mosquitto__packet *packet, char **str, int *lengt if(mosquitto_validate_utf8(*str, *length)){ mosquitto__free(*str); *str = NULL; - *length = -1; + *length = 0; return MOSQ_ERR_MALFORMED_UTF8; } @@ -158,7 +158,7 @@ int packet__read_uint16(struct mosquitto__packet *packet, uint16_t *word) lsb = packet->payload[packet->pos]; packet->pos++; - *word = (msb<<8) + lsb; + *word = (uint16_t)((msb<<8) + lsb); return MOSQ_ERR_SUCCESS; } @@ -192,19 +192,19 @@ int packet__read_uint32(struct mosquitto__packet *packet, uint32_t *word) void packet__write_uint32(struct mosquitto__packet *packet, uint32_t word) { - packet__write_byte(packet, (word & 0xFF000000) >> 24); - packet__write_byte(packet, (word & 0x00FF0000) >> 16); - packet__write_byte(packet, (word & 0x0000FF00) >> 8); - packet__write_byte(packet, (word & 0x000000FF)); + packet__write_byte(packet, (uint8_t)((word & 0xFF000000) >> 24)); + packet__write_byte(packet, (uint8_t)((word & 0x00FF0000) >> 16)); + packet__write_byte(packet, (uint8_t)((word & 0x0000FF00) >> 8)); + packet__write_byte(packet, (uint8_t)((word & 0x000000FF))); } -int packet__read_varint(struct mosquitto__packet *packet, int32_t *word, int8_t *bytes) +int packet__read_varint(struct mosquitto__packet *packet, uint32_t *word, uint8_t *bytes) { int i; uint8_t byte; - int remaining_mult = 1; - int32_t lword = 0; + unsigned int remaining_mult = 1; + uint32_t lword = 0; uint8_t lbytes = 0; for(i=0; i<4; i++){ @@ -232,13 +232,13 @@ int packet__read_varint(struct mosquitto__packet *packet, int32_t *word, int8_t } -int packet__write_varint(struct mosquitto__packet *packet, int32_t word) +int packet__write_varint(struct mosquitto__packet *packet, uint32_t word) { uint8_t byte; int count = 0; do{ - byte = word % 128; + byte = (uint8_t)(word % 128); word = word / 128; /* If there are more digits to encode, set the top bit of this digit */ if(word > 0){ @@ -255,7 +255,7 @@ int packet__write_varint(struct mosquitto__packet *packet, int32_t word) } -int packet__varint_bytes(int32_t word) +unsigned int packet__varint_bytes(uint32_t word) { if(word < 128){ return 1; diff --git a/lib/packet_mosq.c b/lib/packet_mosq.c index 9b0120cc..38f58e5b 100644 --- a/lib/packet_mosq.c +++ b/lib/packet_mosq.c @@ -67,7 +67,7 @@ int packet__alloc(struct mosquitto__packet *packet) packet->remaining_count++; }while(remaining_length > 0 && packet->remaining_count < 5); if(packet->remaining_count == 5) return MOSQ_ERR_PAYLOAD_SIZE; - packet->packet_length = packet->remaining_length + 1 + packet->remaining_count; + packet->packet_length = packet->remaining_length + 1 + (uint8_t)packet->remaining_count; #ifdef WITH_WEBSOCKETS packet->payload = mosquitto__malloc(sizeof(uint8_t)*packet->packet_length + LWS_SEND_BUFFER_PRE_PADDING + LWS_SEND_BUFFER_POST_PADDING); #else @@ -79,7 +79,7 @@ int packet__alloc(struct mosquitto__packet *packet) for(i=0; iremaining_count; i++){ packet->payload[i+1] = remaining_bytes[i]; } - packet->pos = 1 + packet->remaining_count; + packet->pos = 1U + (uint8_t)packet->remaining_count; return MOSQ_ERR_SUCCESS; } @@ -236,8 +236,8 @@ int packet__write(struct mosquitto *mosq) write_length = net__write(mosq, &(packet->payload[packet->pos]), packet->to_process); if(write_length > 0){ G_BYTES_SENT_INC(write_length); - packet->to_process -= write_length; - packet->pos += write_length; + packet->to_process -= (uint32_t)write_length; + packet->pos += (uint32_t)write_length; }else{ #ifdef WIN32 errno = WSAGetLastError(); @@ -422,7 +422,7 @@ int packet__read(struct mosquitto *mosq) }while((byte & 128) != 0); /* We have finished reading remaining_length, so make remaining_count * positive. */ - mosq->in_packet.remaining_count *= -1; + mosq->in_packet.remaining_count = (int8_t)(mosq->in_packet.remaining_count * -1); #ifdef WITH_BROKER if(db->config->max_packet_size > 0 && mosq->in_packet.remaining_length+1 > db->config->max_packet_size){ @@ -447,8 +447,8 @@ int packet__read(struct mosquitto *mosq) read_length = net__read(mosq, &(mosq->in_packet.payload[mosq->in_packet.pos]), mosq->in_packet.to_process); if(read_length > 0){ G_BYTES_RECEIVED_INC(read_length); - mosq->in_packet.to_process -= read_length; - mosq->in_packet.pos += read_length; + mosq->in_packet.to_process -= (uint32_t)read_length; + mosq->in_packet.pos += (uint32_t)read_length; }else{ #ifdef WIN32 errno = WSAGetLastError(); diff --git a/lib/packet_mosq.h b/lib/packet_mosq.h index 8a6dba88..1f0db825 100644 --- a/lib/packet_mosq.h +++ b/lib/packet_mosq.h @@ -32,20 +32,20 @@ int packet__check_oversize(struct mosquitto *mosq, uint32_t remaining_length); 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_binary(struct mosquitto__packet *packet, uint8_t **data, int *length); -int packet__read_string(struct mosquitto__packet *packet, char **str, int *length); +int packet__read_binary(struct mosquitto__packet *packet, uint8_t **data, uint16_t *length); +int packet__read_string(struct mosquitto__packet *packet, char **str, uint16_t *length); int packet__read_uint16(struct mosquitto__packet *packet, uint16_t *word); int packet__read_uint32(struct mosquitto__packet *packet, uint32_t *word); -int packet__read_varint(struct mosquitto__packet *packet, int32_t *word, int8_t *bytes); +int packet__read_varint(struct mosquitto__packet *packet, uint32_t *word, uint8_t *bytes); void packet__write_byte(struct mosquitto__packet *packet, uint8_t byte); void packet__write_bytes(struct mosquitto__packet *packet, const void *bytes, uint32_t count); void packet__write_string(struct mosquitto__packet *packet, const char *str, uint16_t length); void packet__write_uint16(struct mosquitto__packet *packet, uint16_t word); void packet__write_uint32(struct mosquitto__packet *packet, uint32_t word); -int packet__write_varint(struct mosquitto__packet *packet, int32_t word); +int packet__write_varint(struct mosquitto__packet *packet, uint32_t word); -int packet__varint_bytes(int32_t word); +unsigned int packet__varint_bytes(uint32_t word); int packet__write(struct mosquitto *mosq); #ifdef WITH_BROKER diff --git a/lib/property_mosq.c b/lib/property_mosq.c index 0ace4472..baf563ad 100644 --- a/lib/property_mosq.c +++ b/lib/property_mosq.c @@ -31,17 +31,17 @@ Contributors: #include "property_mosq.h" -int property__read(struct mosquitto__packet *packet, int32_t *len, mosquitto_property *property) +int property__read(struct mosquitto__packet *packet, uint32_t *len, mosquitto_property *property) { int rc; - int32_t property_identifier; + uint32_t property_identifier; uint8_t byte; - int8_t byte_count; + uint8_t byte_count; uint16_t uint16; uint32_t uint32; - int32_t varint; + uint32_t varint; char *str1, *str2; - int slen1, slen2; + uint16_t slen1, slen2; if(!property) return MOSQ_ERR_INVAL; @@ -51,7 +51,7 @@ int property__read(struct mosquitto__packet *packet, int32_t *len, mosquitto_pro memset(property, 0, sizeof(mosquitto_property)); - property->identifier = property_identifier; + property->identifier = (int32_t)property_identifier; switch(property_identifier){ case MQTT_PROP_PAYLOAD_FORMAT_INDICATOR: @@ -148,7 +148,7 @@ int property__read(struct mosquitto__packet *packet, int32_t *len, mosquitto_pro int property__read_all(int command, struct mosquitto__packet *packet, mosquitto_property **properties) { int rc; - int32_t proplen; + uint32_t proplen; mosquitto_property *p, *tail = NULL; rc = packet__read_varint(packet, &proplen, NULL); @@ -257,7 +257,7 @@ void mosquitto_property_free_all(mosquitto_property **property) } -int property__get_length(const mosquitto_property *property) +unsigned int property__get_length(const mosquitto_property *property) { if(!property) return 0; @@ -304,7 +304,7 @@ int property__get_length(const mosquitto_property *property) /* binary */ case MQTT_PROP_CORRELATION_DATA: case MQTT_PROP_AUTHENTICATION_DATA: - return 3 + property->value.bin.len; /* 1 + 2 bytes (len) + X bytes (payload) */ + return 3U + property->value.bin.len; /* 1 + 2 bytes (len) + X bytes (payload) */ /* string */ case MQTT_PROP_CONTENT_TYPE: @@ -314,11 +314,11 @@ int property__get_length(const mosquitto_property *property) case MQTT_PROP_RESPONSE_INFORMATION: case MQTT_PROP_SERVER_REFERENCE: case MQTT_PROP_REASON_STRING: - return 3 + property->value.s.len; /* 1 + 2 bytes (len) + X bytes (string) */ + return 3U + property->value.s.len; /* 1 + 2 bytes (len) + X bytes (string) */ /* string pair */ case MQTT_PROP_USER_PROPERTY: - return 5 + property->value.s.len + property->name.len; /* 1 + 2*(2 bytes (len) + X bytes (string))*/ + return 5U + property->value.s.len + property->name.len; /* 1 + 2*(2 bytes (len) + X bytes (string))*/ default: return 0; @@ -327,10 +327,10 @@ int property__get_length(const mosquitto_property *property) } -int property__get_length_all(const mosquitto_property *property) +unsigned int property__get_length_all(const mosquitto_property *property) { const mosquitto_property *p; - int len = 0; + unsigned int len = 0; p = property; while(p){ @@ -343,9 +343,9 @@ int property__get_length_all(const mosquitto_property *property) /* Return the number of bytes we need to add on to the remaining length when * encoding these properties. */ -int property__get_remaining_length(const mosquitto_property *props) +unsigned int property__get_remaining_length(const mosquitto_property *props) { - int proplen, varbytes; + unsigned int proplen, varbytes; proplen = property__get_length_all(props); varbytes = packet__varint_bytes(proplen); @@ -357,7 +357,7 @@ int property__write(struct mosquitto__packet *packet, const mosquitto_property * { int rc; - rc = packet__write_varint(packet, property->identifier); + rc = packet__write_varint(packet, (uint32_t)property->identifier); if(rc) return rc; switch(property->identifier){ @@ -838,10 +838,12 @@ int mosquitto_property_add_binary(mosquitto_property **proplist, int identifier, int mosquitto_property_add_string(mosquitto_property **proplist, int identifier, const char *value) { mosquitto_property *prop; + size_t slen = 0; if(!proplist) return MOSQ_ERR_INVAL; if(value){ - if(mosquitto_validate_utf8(value, strlen(value))) return MOSQ_ERR_MALFORMED_UTF8; + slen = strlen(value); + if(mosquitto_validate_utf8(value, (int)slen)) return MOSQ_ERR_MALFORMED_UTF8; } if(identifier != MQTT_PROP_CONTENT_TYPE @@ -860,13 +862,13 @@ int mosquitto_property_add_string(mosquitto_property **proplist, int identifier, prop->client_generated = true; prop->identifier = identifier; - if(value && strlen(value)){ + if(value && slen > 0){ prop->value.s.v = mosquitto__strdup(value); if(!prop->value.s.v){ mosquitto__free(prop); return MOSQ_ERR_NOMEM; } - prop->value.s.len = strlen(value); + prop->value.s.len = (uint16_t)slen; } property__add(proplist, prop); @@ -877,14 +879,16 @@ int mosquitto_property_add_string(mosquitto_property **proplist, int identifier, int mosquitto_property_add_string_pair(mosquitto_property **proplist, int identifier, const char *name, const char *value) { mosquitto_property *prop; + size_t slen_name = 0, slen_value = 0; if(!proplist) return MOSQ_ERR_INVAL; if(identifier != MQTT_PROP_USER_PROPERTY) return MOSQ_ERR_INVAL; if(name){ - if(mosquitto_validate_utf8(name, strlen(name))) return MOSQ_ERR_MALFORMED_UTF8; + slen_name = strlen(name); + if(mosquitto_validate_utf8(name, (int)slen_name)) return MOSQ_ERR_MALFORMED_UTF8; } if(value){ - if(mosquitto_validate_utf8(value, strlen(value))) return MOSQ_ERR_MALFORMED_UTF8; + if(mosquitto_validate_utf8(value, (int)slen_value)) return MOSQ_ERR_MALFORMED_UTF8; } prop = mosquitto__calloc(1, sizeof(mosquitto_property)); @@ -893,23 +897,23 @@ int mosquitto_property_add_string_pair(mosquitto_property **proplist, int identi prop->client_generated = true; prop->identifier = identifier; - if(name && strlen(name)){ + if(name){ prop->name.v = mosquitto__strdup(name); if(!prop->name.v){ mosquitto__free(prop); return MOSQ_ERR_NOMEM; } - prop->name.len = strlen(name); + prop->name.len = (uint16_t)strlen(name); } - if(value && strlen(value)){ + if(value){ prop->value.s.v = mosquitto__strdup(value); if(!prop->value.s.v){ mosquitto__free(prop->name.v); mosquitto__free(prop); return MOSQ_ERR_NOMEM; } - prop->value.s.len = strlen(value); + prop->value.s.len = (uint16_t)strlen(value); } property__add(proplist, prop); @@ -1104,7 +1108,7 @@ const mosquitto_property *mosquitto_property_read_binary(const mosquitto_propert if(value){ *len = p->value.bin.len; - *value = calloc(1, *len + 1); + *value = calloc(1, *len + 1U); if(!(*value)) return NULL; memcpy(*value, p->value.bin.v, *len); @@ -1133,7 +1137,7 @@ const mosquitto_property *mosquitto_property_read_string(const mosquitto_propert } if(value){ - *value = calloc(1, p->value.s.len+1); + *value = calloc(1, (size_t)p->value.s.len+1); if(!(*value)) return NULL; memcpy(*value, p->value.s.v, p->value.s.len); @@ -1156,13 +1160,13 @@ const mosquitto_property *mosquitto_property_read_string_pair(const mosquitto_pr if(p->identifier != MQTT_PROP_USER_PROPERTY) return NULL; if(name){ - *name = calloc(1, p->name.len+1); + *name = calloc(1, (size_t)p->name.len+1); if(!(*name)) return NULL; memcpy(*name, p->name.v, p->name.len); } if(value){ - *value = calloc(1, p->value.s.len+1); + *value = calloc(1, (size_t)p->value.s.len+1); if(!(*value)){ if(name){ free(*name); diff --git a/lib/property_mosq.h b/lib/property_mosq.h index 8a7e150b..868faf49 100644 --- a/lib/property_mosq.h +++ b/lib/property_mosq.h @@ -21,7 +21,7 @@ Contributors: struct mqtt__string { char *v; - int len; + uint16_t len; }; struct mqtt5__property { @@ -44,9 +44,9 @@ int property__read_all(int command, struct mosquitto__packet *packet, mosquitto_ int property__write_all(struct mosquitto__packet *packet, const mosquitto_property *property, bool write_len); void property__free(mosquitto_property **property); -int property__get_length(const mosquitto_property *property); -int property__get_length_all(const mosquitto_property *property); +unsigned int property__get_length(const mosquitto_property *property); +unsigned int property__get_length_all(const mosquitto_property *property); -int property__get_remaining_length(const mosquitto_property *props); +unsigned int property__get_remaining_length(const mosquitto_property *props); #endif diff --git a/lib/send_connect.c b/lib/send_connect.c index 537322cf..9e425587 100644 --- a/lib/send_connect.c +++ b/lib/send_connect.c @@ -34,14 +34,14 @@ Contributors: int send__connect(struct mosquitto *mosq, uint16_t keepalive, bool clean_session, const mosquitto_property *properties) { struct mosquitto__packet *packet = NULL; - int payloadlen; + uint32_t payloadlen; uint8_t will = 0; uint8_t byte; int rc; uint8_t version; char *clientid, *username, *password; - int headerlen; - int proplen = 0, varbytes; + uint32_t headerlen; + uint32_t proplen = 0, varbytes; mosquitto_property *local_props = NULL; uint16_t receive_maximum; @@ -96,15 +96,15 @@ int send__connect(struct mosquitto *mosq, uint16_t keepalive, bool clean_session if(!packet) return MOSQ_ERR_NOMEM; if(clientid){ - payloadlen = 2+strlen(clientid); + payloadlen = (uint32_t)(2U+strlen(clientid)); }else{ - payloadlen = 2; + payloadlen = 2U; } if(mosq->will){ will = 1; assert(mosq->will->msg.topic); - payloadlen += 2+strlen(mosq->will->msg.topic) + 2+mosq->will->msg.payloadlen; + payloadlen += (uint32_t)(2+strlen(mosq->will->msg.topic) + 2+(uint32_t)mosq->will->msg.payloadlen); if(mosq->protocol == mosq_p_mqtt5){ payloadlen += property__get_remaining_length(mosq->will->properties); } @@ -120,10 +120,10 @@ int send__connect(struct mosquitto *mosq, uint16_t keepalive, bool clean_session } if(username){ - payloadlen += 2+strlen(username); + payloadlen += (uint32_t)(2+strlen(username)); } if(password){ - payloadlen += 2+strlen(password); + payloadlen += (uint32_t)(2+strlen(password)); } packet->command = CMD_CONNECT; @@ -149,9 +149,9 @@ int send__connect(struct mosquitto *mosq, uint16_t keepalive, bool clean_session packet__write_byte(packet, version); byte = (clean_session&0x1)<<1; if(will){ - byte = byte | ((mosq->will->msg.qos&0x3)<<3) | ((will&0x1)<<2); + byte = byte | (uint8_t)(((mosq->will->msg.qos&0x3)<<3) | ((will&0x1)<<2)); if(mosq->retain_available){ - byte |= (mosq->will->msg.retain&0x1)<<5; + byte |= (uint8_t)((mosq->will->msg.retain&0x1)<<5); } } if(username){ @@ -173,7 +173,7 @@ int send__connect(struct mosquitto *mosq, uint16_t keepalive, bool clean_session /* Payload */ if(clientid){ - packet__write_string(packet, clientid, strlen(clientid)); + packet__write_string(packet, clientid, (uint16_t)strlen(clientid)); }else{ packet__write_uint16(packet, 0); } @@ -182,15 +182,15 @@ int send__connect(struct mosquitto *mosq, uint16_t keepalive, bool clean_session /* Write will properties */ property__write_all(packet, mosq->will->properties, true); } - packet__write_string(packet, mosq->will->msg.topic, strlen(mosq->will->msg.topic)); - packet__write_string(packet, (const char *)mosq->will->msg.payload, mosq->will->msg.payloadlen); + packet__write_string(packet, mosq->will->msg.topic, (uint16_t)strlen(mosq->will->msg.topic)); + packet__write_string(packet, (const char *)mosq->will->msg.payload, (uint16_t)mosq->will->msg.payloadlen); } if(username){ - packet__write_string(packet, username, strlen(username)); + packet__write_string(packet, username, (uint16_t)strlen(username)); } if(password){ - packet__write_string(packet, password, strlen(password)); + packet__write_string(packet, password, (uint16_t)strlen(password)); } mosq->keepalive = keepalive; diff --git a/lib/send_mosq.h b/lib/send_mosq.h index c2a6b536..90f6cf32 100644 --- a/lib/send_mosq.h +++ b/lib/send_mosq.h @@ -21,7 +21,7 @@ Contributors: int send__simple_command(struct mosquitto *mosq, uint8_t command); int send__command_with_mid(struct mosquitto *mosq, uint8_t command, uint16_t mid, bool dup, uint8_t reason_code, const mosquitto_property *properties); -int send__real_publish(struct mosquitto *mosq, uint16_t mid, const char *topic, uint32_t payloadlen, const void *payload, int qos, bool retain, bool dup, const mosquitto_property *cmsg_props, const mosquitto_property *store_props, uint32_t expiry_interval); +int send__real_publish(struct mosquitto *mosq, uint16_t mid, const char *topic, uint32_t payloadlen, const void *payload, uint8_t qos, bool retain, bool dup, const mosquitto_property *cmsg_props, const mosquitto_property *store_props, uint32_t expiry_interval); int send__connect(struct mosquitto *mosq, uint16_t keepalive, bool clean_session, const mosquitto_property *properties); int send__disconnect(struct mosquitto *mosq, uint8_t reason_code, const mosquitto_property *properties); @@ -29,7 +29,7 @@ int send__pingreq(struct mosquitto *mosq); int send__pingresp(struct mosquitto *mosq); int send__puback(struct mosquitto *mosq, uint16_t mid, uint8_t reason_code, const mosquitto_property *properties); int send__pubcomp(struct mosquitto *mosq, uint16_t mid, const mosquitto_property *properties); -int send__publish(struct mosquitto *mosq, uint16_t mid, const char *topic, uint32_t payloadlen, const void *payload, int qos, bool retain, bool dup, const mosquitto_property *cmsg_props, const mosquitto_property *store_props, uint32_t expiry_interval); +int send__publish(struct mosquitto *mosq, uint16_t mid, const char *topic, uint32_t payloadlen, const void *payload, uint8_t qos, bool retain, bool dup, const mosquitto_property *cmsg_props, const mosquitto_property *store_props, uint32_t expiry_interval); int send__pubrec(struct mosquitto *mosq, uint16_t mid, uint8_t reason_code, const mosquitto_property *properties); int send__pubrel(struct mosquitto *mosq, uint16_t mid, const mosquitto_property *properties); int send__subscribe(struct mosquitto *mosq, int *mid, int topic_count, char *const *const topic, int topic_qos, const mosquitto_property *properties); diff --git a/lib/send_publish.c b/lib/send_publish.c index 8dfdad51..bcbc7795 100644 --- a/lib/send_publish.c +++ b/lib/send_publish.c @@ -37,7 +37,7 @@ Contributors: #include "send_mosq.h" -int send__publish(struct mosquitto *mosq, uint16_t mid, const char *topic, uint32_t payloadlen, const void *payload, int qos, bool retain, bool dup, const mosquitto_property *cmsg_props, const mosquitto_property *store_props, uint32_t expiry_interval) +int send__publish(struct mosquitto *mosq, uint16_t mid, const char *topic, uint32_t payloadlen, const void *payload, uint8_t qos, bool retain, bool dup, const mosquitto_property *cmsg_props, const mosquitto_property *store_props, uint32_t expiry_interval) { #ifdef WITH_BROKER size_t len; @@ -132,18 +132,18 @@ int send__publish(struct mosquitto *mosq, uint16_t mid, const char *topic, uint3 } -int send__real_publish(struct mosquitto *mosq, uint16_t mid, const char *topic, uint32_t payloadlen, const void *payload, int qos, bool retain, bool dup, const mosquitto_property *cmsg_props, const mosquitto_property *store_props, uint32_t expiry_interval) +int send__real_publish(struct mosquitto *mosq, uint16_t mid, const char *topic, uint32_t payloadlen, const void *payload, uint8_t qos, bool retain, bool dup, const mosquitto_property *cmsg_props, const mosquitto_property *store_props, uint32_t expiry_interval) { struct mosquitto__packet *packet = NULL; - int packetlen; - int proplen = 0, varbytes; + unsigned int packetlen; + unsigned int proplen = 0, varbytes; int rc; mosquitto_property expiry_prop; assert(mosq); if(topic){ - packetlen = 2+strlen(topic) + payloadlen; + packetlen = 2+(unsigned int)strlen(topic) + payloadlen; }else{ packetlen = 2 + payloadlen; } @@ -184,7 +184,7 @@ int send__real_publish(struct mosquitto *mosq, uint16_t mid, const char *topic, if(!packet) return MOSQ_ERR_NOMEM; packet->mid = mid; - packet->command = CMD_PUBLISH | ((dup&0x1)<<3) | (qos<<1) | retain; + packet->command = (uint8_t)(CMD_PUBLISH | (uint8_t)((dup&0x1)<<3) | (uint8_t)(qos<<1) | retain); packet->remaining_length = packetlen; rc = packet__alloc(packet); if(rc){ @@ -193,7 +193,7 @@ int send__real_publish(struct mosquitto *mosq, uint16_t mid, const char *topic, } /* Variable header (topic string) */ if(topic){ - packet__write_string(packet, topic, strlen(topic)); + packet__write_string(packet, topic, (uint16_t)strlen(topic)); }else{ packet__write_uint16(packet, 0); } diff --git a/lib/send_subscribe.c b/lib/send_subscribe.c index 11830a52..d9858234 100644 --- a/lib/send_subscribe.c +++ b/lib/send_subscribe.c @@ -40,21 +40,27 @@ int send__subscribe(struct mosquitto *mosq, int *mid, int topic_count, const cha uint16_t local_mid; int rc; int i; + size_t tlen; assert(mosq); assert(topic); - packet = mosquitto__calloc(1, sizeof(struct mosquitto__packet)); - if(!packet) return MOSQ_ERR_NOMEM; - packetlen = 2; if(mosq->protocol == mosq_p_mqtt5){ packetlen += property__get_remaining_length(properties); } for(i=0; i UINT16_MAX){ + return MOSQ_ERR_INVAL; + } + packetlen += 2U+(uint16_t)tlen + 1U; } + packet = mosquitto__calloc(1, sizeof(struct mosquitto__packet)); + if(!packet) return MOSQ_ERR_NOMEM; + + packet->command = CMD_SUBSCRIBE | (1<<1); packet->remaining_length = packetlen; rc = packet__alloc(packet); @@ -74,8 +80,8 @@ int send__subscribe(struct mosquitto *mosq, int *mid, int topic_count, const cha /* Payload */ for(i=0; i UINT16_MAX){ + return MOSQ_ERR_INVAL; + } + packetlen += 2U+(uint16_t)tlen; + } + packet = mosquitto__calloc(1, sizeof(struct mosquitto__packet)); if(!packet) return MOSQ_ERR_NOMEM; - packetlen = 2; - - for(i=0; iprotocol == mosq_p_mqtt5){ packetlen += property__get_remaining_length(properties); } @@ -77,7 +81,7 @@ int send__unsubscribe(struct mosquitto *mosq, int *mid, int topic_count, char *c /* Payload */ for(i=0; i 256) return MOSQ_ERR_INVAL; - if(port < 1 || port > 65535) return MOSQ_ERR_INVAL; + if(port < 1 || port > UINT16_MAX) return MOSQ_ERR_INVAL; mosquitto__free(mosq->socks5_host); mosq->socks5_host = NULL; @@ -73,7 +73,7 @@ int mosquitto_socks5_set(struct mosquitto *mosq, const char *host, int port, con return MOSQ_ERR_NOMEM; } - mosq->socks5_port = port; + mosq->socks5_port = (uint16_t)port; mosquitto__free(mosq->socks5_username); mosq->socks5_username = NULL; @@ -82,12 +82,18 @@ int mosquitto_socks5_set(struct mosquitto *mosq, const char *host, int port, con mosq->socks5_password = NULL; if(username){ + if(strlen(username) > UINT8_MAX){ + return MOSQ_ERR_INVAL; + } mosq->socks5_username = mosquitto__strdup(username); if(!mosq->socks5_username){ return MOSQ_ERR_NOMEM; } if(password){ + if(strlen(password) > UINT8_MAX){ + return MOSQ_ERR_INVAL; + } mosq->socks5_password = mosquitto__strdup(password); if(!mosq->socks5_password){ mosquitto__free(mosq->socks5_username); @@ -106,8 +112,8 @@ int mosquitto_socks5_set(struct mosquitto *mosq, const char *host, int port, con int socks5__send(struct mosquitto *mosq) { struct mosquitto__packet *packet; - int slen; - int ulen, plen; + size_t slen; + uint8_t ulen, plen; struct in_addr addr_ipv4; struct in6_addr addr_ipv6; @@ -188,7 +194,7 @@ int socks5__send(struct mosquitto *mosq) mosquitto__free(packet); return MOSQ_ERR_NOMEM; } - packet->packet_length = 7 + slen; + packet->packet_length = 7U + (uint32_t)slen; packet->payload = mosquitto__malloc(sizeof(uint8_t)*packet->packet_length); if(!packet->payload){ mosquitto__free(packet); @@ -221,9 +227,9 @@ int socks5__send(struct mosquitto *mosq) packet = mosquitto__calloc(1, sizeof(struct mosquitto__packet)); if(!packet) return MOSQ_ERR_NOMEM; - ulen = strlen(mosq->socks5_username); - plen = strlen(mosq->socks5_password); - packet->packet_length = 3 + ulen + plen; + ulen = (uint8_t)strlen(mosq->socks5_username); + plen = (uint8_t)strlen(mosq->socks5_password); + packet->packet_length = 3U + ulen + plen; packet->payload = mosquitto__malloc(sizeof(uint8_t)*packet->packet_length); @@ -262,8 +268,8 @@ int socks5__read(struct mosquitto *mosq) while(mosq->in_packet.to_process > 0){ len = net__read(mosq, &(mosq->in_packet.payload[mosq->in_packet.pos]), mosq->in_packet.to_process); if(len > 0){ - mosq->in_packet.pos += len; - mosq->in_packet.to_process -= len; + mosq->in_packet.pos += (uint32_t)len; + mosq->in_packet.to_process -= (uint32_t)len; }else{ #ifdef WIN32 errno = WSAGetLastError(); @@ -304,8 +310,8 @@ int socks5__read(struct mosquitto *mosq) while(mosq->in_packet.to_process > 0){ len = net__read(mosq, &(mosq->in_packet.payload[mosq->in_packet.pos]), mosq->in_packet.to_process); if(len > 0){ - mosq->in_packet.pos += len; - mosq->in_packet.to_process -= len; + mosq->in_packet.pos += (uint32_t)len; + mosq->in_packet.to_process -= (uint32_t)len; }else{ #ifdef WIN32 errno = WSAGetLastError(); @@ -360,8 +366,8 @@ int socks5__read(struct mosquitto *mosq) while(mosq->in_packet.to_process > 0){ len = net__read(mosq, &(mosq->in_packet.payload[mosq->in_packet.pos]), mosq->in_packet.to_process); if(len > 0){ - mosq->in_packet.pos += len; - mosq->in_packet.to_process -= len; + mosq->in_packet.pos += (uint32_t)len; + mosq->in_packet.to_process -= (uint32_t)len; }else{ #ifdef WIN32 errno = WSAGetLastError(); diff --git a/lib/tls_mosq.c b/lib/tls_mosq.c index f63bd387..e3c4a8d0 100644 --- a/lib/tls_mosq.c +++ b/lib/tls_mosq.c @@ -84,8 +84,8 @@ int mosquitto__server_certificate_verify(int preverify_ok, X509_STORE_CTX *ctx) int mosquitto__cmp_hostname_wildcard(char *certname, const char *hostname) { - int i; - int len; + size_t i; + size_t len; if(!certname || !hostname){ return 1; diff --git a/lib/util_mosq.c b/lib/util_mosq.c index c34c2b67..7c886cf2 100644 --- a/lib/util_mosq.c +++ b/lib/util_mosq.c @@ -242,7 +242,7 @@ int util__random_bytes(void *bytes, int count) rc = MOSQ_ERR_SUCCESS; } #elif defined(HAVE_GETRANDOM) - if(getrandom(bytes, count, 0) == count){ + if(getrandom(bytes, (size_t)count, 0) == count){ rc = MOSQ_ERR_SUCCESS; } #elif defined(WIN32) diff --git a/lib/will_mosq.c b/lib/will_mosq.c index 59639f4a..be20f7f4 100644 --- a/lib/will_mosq.c +++ b/lib/will_mosq.c @@ -45,7 +45,7 @@ int will__set(struct mosquitto *mosq, const char *topic, int payloadlen, const v if(payloadlen > 0 && !payload) return MOSQ_ERR_INVAL; if(mosquitto_pub_topic_check(topic)) return MOSQ_ERR_INVAL; - if(mosquitto_validate_utf8(topic, strlen(topic))) return MOSQ_ERR_MALFORMED_UTF8; + if(mosquitto_validate_utf8(topic, (uint16_t)strlen(topic))) return MOSQ_ERR_MALFORMED_UTF8; if(properties){ if(mosq->protocol != mosq_p_mqtt5){ @@ -79,13 +79,13 @@ int will__set(struct mosquitto *mosq, const char *topic, int payloadlen, const v rc = MOSQ_ERR_INVAL; goto cleanup; } - mosq->will->msg.payload = mosquitto__malloc(sizeof(char)*mosq->will->msg.payloadlen); + mosq->will->msg.payload = mosquitto__malloc(sizeof(char)*(unsigned int)mosq->will->msg.payloadlen); if(!mosq->will->msg.payload){ rc = MOSQ_ERR_NOMEM; goto cleanup; } - memcpy(mosq->will->msg.payload, payload, payloadlen); + memcpy(mosq->will->msg.payload, payload, (unsigned int)payloadlen); } mosq->will->msg.qos = qos; mosq->will->msg.retain = retain; diff --git a/src/bridge.c b/src/bridge.c index 59fe0e71..36f8db6f 100644 --- a/src/bridge.c +++ b/src/bridge.c @@ -125,7 +125,7 @@ int bridge__new(struct mosquitto_db *db, struct mosquitto__bridge *bridge) new_context->retain_available = bridge->outgoing_retain; new_context->protocol = bridge->protocol_version; - bridges = mosquitto__realloc(db->bridges, (db->bridge_count+1)*sizeof(struct mosquitto *)); + bridges = mosquitto__realloc(db->bridges, (size_t)(db->bridge_count+1)*sizeof(struct mosquitto *)); if(bridges){ db->bridges = bridges; db->bridge_count++; @@ -325,7 +325,7 @@ int bridge__connect(struct mosquitto_db *db, struct mosquitto *context) int rc, rc2; int i; char *notification_topic; - int notification_topic_len; + size_t notification_topic_len; uint8_t notification_payload; if(!context || !context->bridge) return MOSQ_ERR_INVAL; @@ -453,7 +453,7 @@ int bridge__on_connect(struct mosquitto_db *db, struct mosquitto *context) { int i; char *notification_topic; - int notification_topic_len; + size_t notification_topic_len; char notification_payload; int sub_opts; bool retain = true; diff --git a/src/bridge_topic.c b/src/bridge_topic.c index f68b3507..e9e7da49 100644 --- a/src/bridge_topic.c +++ b/src/bridge_topic.c @@ -23,7 +23,7 @@ Contributors: #ifdef WITH_BRIDGE static int bridge__create_remap_topic(const char *prefix, const char *topic, char **remap_topic) { - int len; + size_t len; if(prefix){ if(topic){ @@ -55,7 +55,7 @@ static int bridge__create_remap_topic(const char *prefix, const char *topic, cha static int bridge__create_prefix(char **full_prefix, const char *topic, const char *prefix, const char *direction) { - int len; + size_t len; if(mosquitto_pub_topic_check(prefix) != MOSQ_ERR_SUCCESS){ log__printf(NULL, MOSQ_LOG_ERR, "Error: Invalid bridge topic local prefix '%s'.", prefix); @@ -96,7 +96,7 @@ static int bridge__create_prefix(char **full_prefix, const char *topic, const ch /* topic [[[out | in | both] qos-level] local-prefix remote-prefix] */ -int bridge__add_topic(struct mosquitto__bridge *bridge, const char *topic, enum mosquitto__bridge_direction direction, int qos, const char *local_prefix, const char *remote_prefix) +int bridge__add_topic(struct mosquitto__bridge *bridge, const char *topic, enum mosquitto__bridge_direction direction, uint8_t qos, const char *local_prefix, const char *remote_prefix) { struct mosquitto__bridge_topic *topics; struct mosquitto__bridge_topic *cur_topic; @@ -127,7 +127,7 @@ int bridge__add_topic(struct mosquitto__bridge *bridge, const char *topic, enum bridge->topic_count++; topics = mosquitto__realloc(bridge->topics, - sizeof(struct mosquitto__bridge_topic)*bridge->topic_count); + sizeof(struct mosquitto__bridge_topic)*(size_t)bridge->topic_count); if(topics == NULL){ log__printf(NULL, MOSQ_LOG_ERR, "Error: Out of memory."); @@ -188,7 +188,7 @@ int bridge__remap_topic_in(struct mosquitto *context, char **topic) struct mosquitto__bridge_topic *cur_topic; char *topic_temp; int i; - int len; + size_t len; int rc; bool match; diff --git a/src/conf.c b/src/conf.c index aba8cb41..a759ad2e 100644 --- a/src/conf.c +++ b/src/conf.c @@ -48,9 +48,9 @@ Contributors: #include "mqtt_protocol.h" struct config_recurse { - int log_dest; + unsigned int log_dest; int log_dest_set; - int log_type; + unsigned int log_type; int log_type_set; unsigned long max_inflight_bytes; unsigned long max_queued_bytes; @@ -81,7 +81,7 @@ static void conf__set_cur_security_options(struct mosquitto__config *config, str } } -static int conf__attempt_resolve(const char *host, const char *text, int log, const char *msg) +static int conf__attempt_resolve(const char *host, const char *text, unsigned int log, const char *msg) { struct addrinfo gai_hints; struct addrinfo *gai_res; @@ -393,7 +393,7 @@ int config__parse_args(struct mosquitto_db *db, struct mosquitto__config *config }else if(!strcmp(argv[i], "-p") || !strcmp(argv[i], "--port")){ if(i65535){ + if(port_tmp<1 || port_tmp>UINT16_MAX){ log__printf(NULL, MOSQ_LOG_ERR, "Error: Invalid port specified (%d).", port_tmp); return MOSQ_ERR_INVAL; }else{ @@ -401,7 +401,7 @@ int config__parse_args(struct mosquitto_db *db, struct mosquitto__config *config log__printf(NULL, MOSQ_LOG_ERR, "Error: Only %d ports can be specified on the command line.", CMD_PORT_LIMIT); return MOSQ_ERR_INVAL; } - config->cmd_port[config->cmd_port_count] = port_tmp; + config->cmd_port[config->cmd_port_count] = (uint16_t)port_tmp; config->cmd_port_count++; } }else{ @@ -450,7 +450,7 @@ int config__parse_args(struct mosquitto_db *db, struct mosquitto__config *config ){ config->listener_count++; - config->listeners = mosquitto__realloc(config->listeners, sizeof(struct mosquitto__listener)*config->listener_count); + config->listeners = mosquitto__realloc(config->listeners, sizeof(struct mosquitto__listener)*(size_t)config->listener_count); if(!config->listeners){ log__printf(NULL, MOSQ_LOG_ERR, "Error: Out of memory."); return MOSQ_ERR_NOMEM; @@ -594,7 +594,7 @@ int config__read(struct mosquitto_db *db, struct mosquitto__config *config, bool struct config_recurse cr; int lineno = 0; #ifdef WITH_PERSISTENCE - int len; + size_t len; #endif struct mosquitto__config config_reload; int i; @@ -734,7 +734,7 @@ int config__read_file_core(struct mosquitto__config *config, bool reload, struct struct mosquitto__listener *cur_listener = &config->default_listener; int i; int lineno_ext = 0; - int prefix_len; + size_t prefix_len; char **files; int file_count; #ifdef WITH_TLS @@ -771,7 +771,7 @@ int config__read_file_core(struct mosquitto__config *config, bool reload, struct break; } cur_bridge->address_count++; - cur_bridge->addresses = mosquitto__realloc(cur_bridge->addresses, sizeof(struct bridge_address)*cur_bridge->address_count); + cur_bridge->addresses = mosquitto__realloc(cur_bridge->addresses, sizeof(struct bridge_address)*(size_t)cur_bridge->address_count); if(!cur_bridge->addresses){ log__printf(NULL, MOSQ_LOG_ERR, "Error: Out of memory."); return MOSQ_ERR_NOMEM; @@ -791,11 +791,11 @@ int config__read_file_core(struct mosquitto__config *config, bool reload, struct /* The remainder of the string */ tmp_int = atoi(&tmp_char[1]); - if(tmp_int < 1 || tmp_int > 65535){ + if(tmp_int < 1 || tmp_int > UINT16_MAX){ log__printf(NULL, MOSQ_LOG_ERR, "Error: Invalid port value (%d).", tmp_int); return MOSQ_ERR_INVAL; } - cur_bridge->addresses[i].port = tmp_int; + cur_bridge->addresses[i].port = (uint16_t)tmp_int; }else{ cur_bridge->addresses[i].port = 1883; } @@ -854,7 +854,7 @@ int config__read_file_core(struct mosquitto__config *config, bool reload, struct } if(token[0]){ cur_auth_plugin_config->option_count++; - cur_auth_plugin_config->options = mosquitto__realloc(cur_auth_plugin_config->options, cur_auth_plugin_config->option_count*sizeof(struct mosquitto_auth_opt)); + cur_auth_plugin_config->options = mosquitto__realloc(cur_auth_plugin_config->options, (size_t)cur_auth_plugin_config->option_count*sizeof(struct mosquitto_auth_opt)); if(!cur_auth_plugin_config->options){ log__printf(NULL, MOSQ_LOG_ERR, "Error: Out of memory."); mosquitto__free(key); @@ -874,7 +874,7 @@ int config__read_file_core(struct mosquitto__config *config, bool reload, struct }else if(!strcmp(token, "auth_plugin") || !strcmp(token, "plugin")){ if(reload) continue; // Auth plugin not currently valid for reloading. conf__set_cur_security_options(config, cur_listener, &cur_security_options); - cur_security_options->auth_plugin_configs = mosquitto__realloc(cur_security_options->auth_plugin_configs, (cur_security_options->auth_plugin_config_count+1)*sizeof(struct mosquitto__auth_plugin_config)); + cur_security_options->auth_plugin_configs = mosquitto__realloc(cur_security_options->auth_plugin_configs, (size_t)(cur_security_options->auth_plugin_config_count+1)*sizeof(struct mosquitto__auth_plugin_config)); if(!cur_security_options->auth_plugin_configs){ log__printf(NULL, MOSQ_LOG_ERR, "Error: Out of memory."); return MOSQ_ERR_NOMEM; @@ -898,7 +898,7 @@ int config__read_file_core(struct mosquitto__config *config, bool reload, struct conf__set_cur_security_options(config, cur_listener, &cur_security_options); if(conf__parse_string(&token, "auto_id_prefix", &cur_security_options->auto_id_prefix, saveptr)) return MOSQ_ERR_INVAL; if(cur_security_options->auto_id_prefix){ - cur_security_options->auto_id_prefix_len = strlen(cur_security_options->auto_id_prefix); + cur_security_options->auto_id_prefix_len = (uint16_t)strlen(cur_security_options->auto_id_prefix); }else{ cur_security_options->auto_id_prefix_len = 0; } @@ -1209,7 +1209,7 @@ int config__read_file_core(struct mosquitto__config *config, bool reload, struct } config->bridge_count++; - config->bridges = mosquitto__realloc(config->bridges, config->bridge_count*sizeof(struct mosquitto__bridge)); + config->bridges = mosquitto__realloc(config->bridges, (size_t)config->bridge_count*sizeof(struct mosquitto__bridge)); if(!config->bridges){ log__printf(NULL, MOSQ_LOG_ERR, "Error: Out of memory."); return MOSQ_ERR_NOMEM; @@ -1318,11 +1318,16 @@ int config__read_file_core(struct mosquitto__config *config, bool reload, struct log__printf(NULL, MOSQ_LOG_ERR, "Error: Invalid bridge configuration."); return MOSQ_ERR_INVAL; } - if(conf__parse_int(&token, "keepalive_interval", &cur_bridge->keepalive, saveptr)) return MOSQ_ERR_INVAL; - if(cur_bridge->keepalive < 5){ - log__printf(NULL, MOSQ_LOG_NOTICE, "keepalive interval too low, using 5 seconds."); - cur_bridge->keepalive = 5; + if(conf__parse_int(&token, "keepalive_interval", &tmp_int, saveptr)) return MOSQ_ERR_INVAL; + if(tmp_int > UINT16_MAX){ + log__printf(NULL, MOSQ_LOG_ERR, "Error: Bridge keepalive value too high."); + return MOSQ_ERR_INVAL; } + if(tmp_int < 5){ + log__printf(NULL, MOSQ_LOG_NOTICE, "keepalive interval too low, using 5 seconds."); + tmp_int = 5; + } + cur_bridge->keepalive = (uint16_t)tmp_int; #else log__printf(NULL, MOSQ_LOG_WARNING, "Warning: Bridge support not available."); #endif @@ -1339,9 +1344,9 @@ int config__read_file_core(struct mosquitto__config *config, bool reload, struct if(token){ tmp_int = atoi(token); #ifdef WITH_UNIX_SOCKETS - if(tmp_int < 0 || tmp_int > 65535){ + if(tmp_int < 0 || tmp_int > UINT16_MAX){ #else - if(tmp_int < 1 || tmp_int > 65535){ + if(tmp_int < 1 || tmp_int > UINT16_MAX){ #endif log__printf(NULL, MOSQ_LOG_ERR, "Error: Invalid port value (%d).", tmp_int); return MOSQ_ERR_INVAL; @@ -1388,7 +1393,7 @@ int config__read_file_core(struct mosquitto__config *config, bool reload, struct } }else{ config->listener_count++; - config->listeners = mosquitto__realloc(config->listeners, sizeof(struct mosquitto__listener)*config->listener_count); + config->listeners = mosquitto__realloc(config->listeners, sizeof(struct mosquitto__listener)*(size_t)config->listener_count); if(!config->listeners){ log__printf(NULL, MOSQ_LOG_ERR, "Error: Out of memory."); return MOSQ_ERR_NOMEM; @@ -1398,7 +1403,7 @@ int config__read_file_core(struct mosquitto__config *config, bool reload, struct } listener__set_defaults(cur_listener); - cur_listener->port = tmp_int; + cur_listener->port = (uint16_t)tmp_int; mosquitto__free(cur_listener->host); cur_listener->host = NULL; @@ -1598,41 +1603,41 @@ int config__read_file_core(struct mosquitto__config *config, bool reload, struct log__printf(NULL, MOSQ_LOG_ERR, "Error: maximum_qos must be between 0 and 2 inclusive."); return MOSQ_ERR_INVAL; } - cur_listener->maximum_qos = tmp_int; + cur_listener->maximum_qos = (uint8_t)tmp_int; }else if(!strcmp(token, "max_inflight_bytes")){ token = strtok_r(NULL, " ", &saveptr); if(token){ - cr->max_inflight_bytes = atol(token); + cr->max_inflight_bytes = (unsigned long)atol(token); }else{ log__printf(NULL, MOSQ_LOG_ERR, "Error: Empty max_inflight_bytes value in configuration."); } }else if(!strcmp(token, "max_inflight_messages")){ if(conf__parse_int(&token, "max_inflight_messages", &tmp_int, saveptr)) return MOSQ_ERR_INVAL; - if(tmp_int < 0 || tmp_int == 65535){ + if(tmp_int < 0 || tmp_int == UINT16_MAX){ tmp_int = 0; - }else if(tmp_int > 65535){ + }else if(tmp_int > UINT16_MAX){ log__printf(NULL, MOSQ_LOG_ERR, "Error: max_inflight_messages must be <= 65535."); return MOSQ_ERR_INVAL; } - config->max_inflight_messages = tmp_int; + config->max_inflight_messages = (uint16_t)tmp_int; }else if(!strcmp(token, "max_keepalive")){ if(conf__parse_int(&token, "max_keepalive", &tmp_int, saveptr)) return MOSQ_ERR_INVAL; - if(tmp_int < 10 || tmp_int > 65535){ + if(tmp_int < 10 || tmp_int > UINT16_MAX){ log__printf(NULL, MOSQ_LOG_ERR, "Error: Invalid max_keepalive value (%d).", tmp_int); return MOSQ_ERR_INVAL; } - config->max_keepalive = tmp_int; + config->max_keepalive = (uint16_t)tmp_int; }else if(!strcmp(token, "max_packet_size")){ if(conf__parse_int(&token, "max_packet_size", &tmp_int, saveptr)) return MOSQ_ERR_INVAL; if(tmp_int < 20){ log__printf(NULL, MOSQ_LOG_ERR, "Error: Invalid max_packet_size value (%d).", tmp_int); return MOSQ_ERR_INVAL; } - config->max_packet_size = tmp_int; + config->max_packet_size = (uint32_t)tmp_int; }else if(!strcmp(token, "max_queued_bytes")){ token = strtok_r(NULL, " ", &saveptr); if(token){ - cr->max_queued_bytes = atol(token); /* 63 bits is ok right? */ + cr->max_queued_bytes = (unsigned long)atol(token); /* 63 bits is ok right? */ }else{ log__printf(NULL, MOSQ_LOG_ERR, "Error: Empty max_queued_bytes value in configuration."); } @@ -1651,7 +1656,7 @@ int config__read_file_core(struct mosquitto__config *config, bool reload, struct log__printf(NULL, MOSQ_LOG_ERR, "Error: Invalid memory_limit value (%ld).", lim); return MOSQ_ERR_INVAL; } - memory__set_limit(lim); + memory__set_limit((size_t)lim); }else if(!strcmp(token, "message_size_limit")){ if(conf__parse_int(&token, "message_size_limit", (int *)&config->message_size_limit, saveptr)) return MOSQ_ERR_INVAL; if(config->message_size_limit > MQTT_MAX_PAYLOAD){ @@ -1777,11 +1782,11 @@ int config__read_file_core(struct mosquitto__config *config, bool reload, struct log__printf(NULL, MOSQ_LOG_WARNING, "Warning: Default listener port specified multiple times. Only the latest will be used."); } if(conf__parse_int(&token, "port", &tmp_int, saveptr)) return MOSQ_ERR_INVAL; - if(tmp_int < 1 || tmp_int > 65535){ + if(tmp_int < 1 || tmp_int > UINT16_MAX){ log__printf(NULL, MOSQ_LOG_ERR, "Error: Invalid port value (%d).", tmp_int); return MOSQ_ERR_INVAL; } - config->default_listener.port = tmp_int; + config->default_listener.port = (uint16_t)tmp_int; }else if(!strcmp(token, "protocol")){ token = strtok_r(NULL, " ", &saveptr); if(token){ @@ -1992,7 +1997,7 @@ int config__read_file_core(struct mosquitto__config *config, bool reload, struct } char *topic = NULL; enum mosquitto__bridge_direction direction = bd_out; - int qos = 0; + uint8_t qos = 0; char *local_prefix = NULL, *remote_prefix = NULL; token = strtok_r(NULL, " ", &saveptr); @@ -2019,7 +2024,7 @@ int config__read_file_core(struct mosquitto__config *config, bool reload, struct if (token[0] == '#'){ strtok_r(NULL, "", &saveptr); } - qos = atoi(token); + qos = (uint8_t)atoi(token); if(qos < 0 || qos > 2){ log__printf(NULL, MOSQ_LOG_ERR, "Error: Invalid bridge QoS level '%s'.", token); return MOSQ_ERR_INVAL; @@ -2058,9 +2063,15 @@ int config__read_file_core(struct mosquitto__config *config, bool reload, struct if(reload) continue; /* Listeners not valid for reloading. */ token = strtok_r(NULL, " ", &saveptr); if(token){ - cur_listener->max_topic_alias = atoi(token); + tmp_int = atoi(token); + if(tmp_int < 0 || tmp_int > UINT16_MAX){ + log__printf(NULL, MOSQ_LOG_ERR, "Error: Invalid max_topic_alias value in configuration."); + return MOSQ_ERR_INVAL; + } + cur_listener->max_topic_alias = (uint16_t)tmp_int; }else{ log__printf(NULL, MOSQ_LOG_ERR, "Error: Empty max_topic_alias value in configuration."); + return MOSQ_ERR_INVAL; } }else if(!strcmp(token, "try_private")){ #ifdef WITH_BRIDGE @@ -2147,7 +2158,7 @@ int config__read_file(struct mosquitto__config *config, bool reload, const char } buflen = 1000; - buf = mosquitto__malloc(buflen); + buf = mosquitto__malloc((size_t)buflen); if(!buf){ log__printf(NULL, MOSQ_LOG_ERR, "Error: Out of memory."); fclose(fptr); @@ -2172,7 +2183,7 @@ static int config__check(struct mosquitto__config *config) int j; struct mosquitto__bridge *bridge1, *bridge2; char hostname[256]; - int len; + size_t len; /* Check for bridge duplicate local_clientid, need to generate missing IDs * first. */ @@ -2291,6 +2302,8 @@ static int conf__parse_ssize_t(char **token, const char *name, ssize_t *value, c static int conf__parse_string(char **token, const char *name, char **value, char *saveptr) { + size_t tlen; + *token = strtok_r(NULL, "", &saveptr); if(*token){ if(*value){ @@ -2304,7 +2317,11 @@ static int conf__parse_string(char **token, const char *name, char **value, char return MOSQ_ERR_INVAL; } - if(mosquitto_validate_utf8(*token, strlen(*token))){ + tlen = strlen(*token); + if(tlen > UINT16_MAX){ + return MOSQ_ERR_INVAL; + } + if(mosquitto_validate_utf8(*token, (uint16_t)tlen)){ log__printf(NULL, MOSQ_LOG_ERR, "Error: Malformed UTF-8 in configuration."); return MOSQ_ERR_INVAL; } diff --git a/src/conf_includedir.c b/src/conf_includedir.c index b602f963..9417f992 100644 --- a/src/conf_includedir.c +++ b/src/conf_includedir.c @@ -142,7 +142,7 @@ int config__get_dir_files(const char *include_dir, char ***files, int *file_coun char **l_files = NULL; int l_file_count = 0; char **files_tmp; - int len; + size_t len; int i; DIR *dh; @@ -159,7 +159,7 @@ int config__get_dir_files(const char *include_dir, char ***files, int *file_coun len = strlen(include_dir)+1+strlen(de->d_name)+1; l_file_count++; - files_tmp = mosquitto__realloc(l_files, l_file_count*sizeof(char *)); + files_tmp = mosquitto__realloc(l_files, (size_t)l_file_count*sizeof(char *)); if(!files_tmp){ for(i=0; iwill->msg.topic, - ctxt->will->msg.payloadlen, + (uint32_t)ctxt->will->msg.payloadlen, ctxt->will->msg.payload, - ctxt->will->msg.qos, + (uint8_t)ctxt->will->msg.qos, ctxt->will->msg.retain, MOSQ_ACL_WRITE) == MOSQ_ERR_SUCCESS){ /* Unexpected disconnect, queue the client will. */ db__messages_easy_queue(db, ctxt, ctxt->will->msg.topic, - ctxt->will->msg.qos, - ctxt->will->msg.payloadlen, + (uint8_t)ctxt->will->msg.qos, + (uint32_t)ctxt->will->msg.payloadlen, ctxt->will->msg.payload, ctxt->will->msg.retain, ctxt->will->expiry_interval, diff --git a/src/control.c b/src/control.c index dc1975e4..b74cbec0 100644 --- a/src/control.c +++ b/src/control.c @@ -77,7 +77,7 @@ int control__register_callback(struct mosquitto_db *db, struct mosquitto__securi { #ifdef WITH_CONTROL struct mosquitto__callback *cb_found, *cb_new; - int topic_len; + size_t topic_len; if(topic == NULL || cb_func == NULL) return MOSQ_ERR_INVAL; topic_len = strlen(topic); @@ -114,7 +114,7 @@ int control__unregister_callback(struct mosquitto_db *db, struct mosquitto__secu { #ifdef WITH_CONTROL struct mosquitto__callback *cb_found; - int topic_len; + size_t topic_len; if(topic == NULL) return MOSQ_ERR_INVAL; topic_len = strlen(topic); diff --git a/src/database.c b/src/database.c index 992cecff..d911c647 100644 --- a/src/database.c +++ b/src/database.c @@ -352,7 +352,7 @@ int db__message_delete_outgoing(struct mosquitto_db *db, struct mosquitto *conte return db__message_write_inflight_out_latest(db, context); } -int db__message_insert(struct mosquitto_db *db, struct mosquitto *context, uint16_t mid, enum mosquitto_msg_direction dir, int qos, bool retain, struct mosquitto_msg_store *stored, mosquitto_property *properties, bool update) +int db__message_insert(struct mosquitto_db *db, struct mosquitto *context, uint16_t mid, enum mosquitto_msg_direction dir, uint8_t qos, bool retain, struct mosquitto_msg_store *stored, mosquitto_property *properties, bool update) { struct mosquitto_client_msg *msg; struct mosquitto_msg_data *msg_data; @@ -508,7 +508,7 @@ int db__message_insert(struct mosquitto_db *db, struct mosquitto *context, uint1 * multiple times for overlapping subscriptions, although this is only the * case for SUBSCRIPTION with multiple subs in so is a minor concern. */ - dest_ids = mosquitto__realloc(stored->dest_ids, sizeof(char *)*(stored->dest_id_count+1)); + dest_ids = mosquitto__realloc(stored->dest_ids, sizeof(char *)*(size_t)(stored->dest_id_count+1)); if(dest_ids){ stored->dest_ids = dest_ids; stored->dest_id_count++; @@ -602,7 +602,7 @@ int db__messages_delete(struct mosquitto_db *db, struct mosquitto *context, bool return MOSQ_ERR_SUCCESS; } -int db__messages_easy_queue(struct mosquitto_db *db, struct mosquitto *context, const char *topic, int qos, uint32_t payloadlen, const void *payload, int retain, uint32_t message_expiry_interval, mosquitto_property **properties) +int db__messages_easy_queue(struct mosquitto_db *db, struct mosquitto *context, const char *topic, uint8_t qos, uint32_t payloadlen, const void *payload, int retain, uint32_t message_expiry_interval, mosquitto_property **properties) { struct mosquitto_msg_store *stored; char *source_id; @@ -1002,7 +1002,7 @@ static int db__message_write_inflight_out_single(struct mosquitto_db *db, struct int retries; int retain; const char *topic; - int qos; + uint8_t qos; uint32_t payloadlen; const void *payload; time_t now = 0; @@ -1018,14 +1018,14 @@ static int db__message_write_inflight_out_single(struct mosquitto_db *db, struct db__message_remove(db, &context->msgs_out, msg); return MOSQ_ERR_SUCCESS; }else{ - expiry_interval = msg->store->message_expiry_time - now; + expiry_interval = (uint32_t)(msg->store->message_expiry_time - now); } } mid = msg->mid; retries = msg->dup; retain = msg->retain; topic = msg->store->topic; - qos = msg->qos; + qos = (uint8_t)msg->qos; payloadlen = msg->store->payloadlen; payload = UHPA_ACCESS_PAYLOAD(msg->store); cmsg_props = msg->properties; diff --git a/src/handle_connect.c b/src/handle_connect.c index 1e17d7b7..f49117c4 100644 --- a/src/handle_connect.c +++ b/src/handle_connect.c @@ -40,13 +40,13 @@ Contributors: static char nibble_to_hex(uint8_t value) { if(value < 0x0A){ - return '0'+value; + return (char)('0'+value); }else{ - return 'A'+value-0x0A; + return (char)(65 /*'A'*/ +value-10); } } -static char *client_id_gen(int *idlen, const char *auto_id_prefix, int auto_id_prefix_len) +static char *client_id_gen(uint16_t *idlen, const char *auto_id_prefix, uint16_t auto_id_prefix_len) { char *client_id; uint8_t rnd[16]; @@ -55,9 +55,9 @@ static char *client_id_gen(int *idlen, const char *auto_id_prefix, int auto_id_p if(util__random_bytes(rnd, 16)) return NULL; - *idlen = 36 + auto_id_prefix_len; + *idlen = (uint16_t)(auto_id_prefix_len + 36); - client_id = (char *)mosquitto__calloc((*idlen) + 1, sizeof(char)); + client_id = (char *)mosquitto__calloc((size_t)(*idlen) + 1, sizeof(char)); if(!client_id){ return NULL; } @@ -289,7 +289,8 @@ error: static int will__read(struct mosquitto *context, struct mosquitto_message_all **will, uint8_t will_qos, int will_retain) { int rc = MOSQ_ERR_SUCCESS; - int slen; + size_t slen; + uint16_t tlen; struct mosquitto_message_all *will_struct = NULL; char *will_topic_mount = NULL; uint16_t payloadlen; @@ -308,9 +309,9 @@ static int will__read(struct mosquitto *context, struct mosquitto_message_all ** mosquitto_property_free_all(&properties); if(rc) goto error_cleanup; } - rc = packet__read_string(&context->in_packet, &will_struct->msg.topic, &slen); + rc = packet__read_string(&context->in_packet, &will_struct->msg.topic, &tlen); if(rc) goto error_cleanup; - if(!slen){ + if(!tlen){ rc = MOSQ_ERR_PROTOCOL; goto error_cleanup; } @@ -338,13 +339,13 @@ static int will__read(struct mosquitto *context, struct mosquitto_message_all ** will_struct->msg.payloadlen = payloadlen; if(will_struct->msg.payloadlen > 0){ - will_struct->msg.payload = mosquitto__malloc(will_struct->msg.payloadlen); + will_struct->msg.payload = mosquitto__malloc((size_t)will_struct->msg.payloadlen); if(!will_struct->msg.payload){ rc = MOSQ_ERR_NOMEM; goto error_cleanup; } - rc = packet__read_bytes(&context->in_packet, will_struct->msg.payload, will_struct->msg.payloadlen); + rc = packet__read_bytes(&context->in_packet, will_struct->msg.payload, (uint32_t)will_struct->msg.payloadlen); if(rc) goto error_cleanup; } @@ -377,8 +378,7 @@ int handle__connect(struct mosquitto_db *db, struct mosquitto *context) uint8_t username_flag, password_flag; char *username = NULL, *password = NULL; int rc; - int slen; - uint16_t slen16; + uint16_t slen; mosquitto_property *properties = NULL; void *auth_data = NULL; uint16_t auth_data_len = 0; @@ -413,11 +413,10 @@ int handle__connect(struct mosquitto_db *db, struct mosquitto *context) /* Read protocol name as length then bytes rather than with read_string * because the length is fixed and we can check that. Removes the need * for another malloc as well. */ - if(packet__read_uint16(&context->in_packet, &slen16)){ + if(packet__read_uint16(&context->in_packet, &slen)){ rc = 1; goto handle_connect_error; } - slen = slen16; if(slen != 4 /* MQTT */ && slen != 6 /* MQIsdp */){ rc = MOSQ_ERR_PROTOCOL; goto handle_connect_error; @@ -756,13 +755,13 @@ int handle__connect(struct mosquitto_db *db, struct mosquitto *context) X509_NAME_print_ex(subject_bio, X509_get_subject_name(client_cert), 0, XN_FLAG_RFC2253); data_start = NULL; name_length = BIO_get_mem_data(subject_bio, &data_start); - subject = mosquitto__malloc(sizeof(char)*name_length+1); + subject = mosquitto__malloc(sizeof(char)*(size_t)(name_length+1)); if(!subject){ BIO_free(subject_bio); rc = MOSQ_ERR_NOMEM; goto handle_connect_error; } - memcpy(subject, data_start, name_length); + memcpy(subject, data_start, (size_t)name_length); subject[name_length] = '\0'; BIO_free(subject_bio); context->username = subject; diff --git a/src/handle_publish.c b/src/handle_publish.c index b591006a..63c3a9ed 100644 --- a/src/handle_publish.c +++ b/src/handle_publish.c @@ -40,8 +40,8 @@ int handle__publish(struct mosquitto_db *db, struct mosquitto *context) uint8_t header = context->in_packet.command; int res = 0; struct mosquitto_msg_store *msg, *stored = NULL; - int len; - int slen; + size_t len; + uint16_t slen; char *topic_mount; mosquitto_property *properties = NULL; mosquitto_property *p, *p_prev; @@ -175,13 +175,13 @@ int handle__publish(struct mosquitto_db *db, struct mosquitto *context) return MOSQ_ERR_TOPIC_ALIAS_INVALID; }else if(topic_alias > 0){ if(msg->topic){ - rc = alias__add(context, msg->topic, topic_alias); + rc = alias__add(context, msg->topic, (uint16_t)topic_alias); if(rc){ db__msg_store_free(msg); return rc; } }else{ - rc = alias__find(context, &msg->topic, topic_alias); + rc = alias__find(context, &msg->topic, (uint16_t)topic_alias); if(rc){ db__msg_store_free(msg); return MOSQ_ERR_TOPIC_ALIAS_INVALID; diff --git a/src/handle_subscribe.c b/src/handle_subscribe.c index 2879adb2..a9c3edab 100644 --- a/src/handle_subscribe.c +++ b/src/handle_subscribe.c @@ -39,8 +39,8 @@ int handle__subscribe(struct mosquitto_db *db, struct mosquitto *context) uint8_t retain_handling = 0; uint8_t *payload = NULL, *tmp_payload; uint32_t payloadlen = 0; - int len; - int slen; + size_t len; + uint16_t slen; char *sub_mount; mosquitto_property *properties = NULL; bool allowed; diff --git a/src/handle_unsubscribe.c b/src/handle_unsubscribe.c index 9c5f54f5..cc8f67e7 100644 --- a/src/handle_unsubscribe.c +++ b/src/handle_unsubscribe.c @@ -29,7 +29,7 @@ int handle__unsubscribe(struct mosquitto_db *db, struct mosquitto *context) { uint16_t mid; char *sub; - int slen; + uint16_t slen; int rc; uint8_t reason; int reason_code_count = 0; @@ -77,7 +77,7 @@ int handle__unsubscribe(struct mosquitto_db *db, struct mosquitto *context) } reason_code_max = 10; - reason_codes = mosquitto__malloc(reason_code_max); + reason_codes = mosquitto__malloc((size_t)reason_code_max); if(!reason_codes){ return MOSQ_ERR_NOMEM; } @@ -135,7 +135,7 @@ int handle__unsubscribe(struct mosquitto_db *db, struct mosquitto *context) reason_codes[reason_code_count] = reason; reason_code_count++; if(reason_code_count == reason_code_max){ - reason_tmp = mosquitto__realloc(reason_codes, reason_code_max*2); + reason_tmp = mosquitto__realloc(reason_codes, (size_t)(reason_code_max*2)); if(!reason_tmp){ mosquitto__free(reason_codes); return MOSQ_ERR_NOMEM; diff --git a/src/logging.c b/src/logging.c index 217b0916..44647e22 100644 --- a/src/logging.c +++ b/src/logging.c @@ -51,8 +51,8 @@ HANDLE syslog_h; /* Give option of logging timestamp. * Logging pid. */ -static int log_destinations = MQTT3_LOG_STDERR; -static int log_priorities = MOSQ_LOG_ERR | MOSQ_LOG_WARNING | MOSQ_LOG_NOTICE | MOSQ_LOG_INFO; +static unsigned int log_destinations = MQTT3_LOG_STDERR; +static unsigned int log_priorities = MOSQ_LOG_ERR | MOSQ_LOG_WARNING | MOSQ_LOG_NOTICE | MOSQ_LOG_INFO; #ifdef WITH_DLT static DltContext dltContext; @@ -180,7 +180,7 @@ int log__close(struct mosquitto__config *config) } #ifdef WITH_DLT -DltLogLevelType get_dlt_level(int priority) +DltLogLevelType get_dlt_level(unsigned int priority) { switch (priority) { case MOSQ_LOG_ERR: @@ -201,11 +201,11 @@ DltLogLevelType get_dlt_level(int priority) } #endif -int log__vprintf(int priority, const char *fmt, va_list va) +int log__vprintf(unsigned int priority, const char *fmt, va_list va) { char *s; char *st; - int len; + size_t len; #ifdef WIN32 char *sp; #endif @@ -363,10 +363,10 @@ int log__vprintf(int priority, const char *fmt, va_list va) return MOSQ_ERR_NOMEM; } snprintf(st, len, "%d: %s", (int)now, s); - db__messages_easy_queue(&int_db, NULL, topic, 2, strlen(st), st, 0, 20, NULL); + db__messages_easy_queue(&int_db, NULL, topic, 2, (uint32_t)strlen(st), st, 0, 20, NULL); mosquitto__free(st); }else{ - db__messages_easy_queue(&int_db, NULL, topic, 2, strlen(s), s, 0, 20, NULL); + db__messages_easy_queue(&int_db, NULL, topic, 2, (uint32_t)strlen(s), s, 0, 20, NULL); } } #ifdef WITH_DLT @@ -380,7 +380,7 @@ int log__vprintf(int priority, const char *fmt, va_list va) return MOSQ_ERR_SUCCESS; } -int log__printf(struct mosquitto *mosq, int priority, const char *fmt, ...) +int log__printf(struct mosquitto *mosq, unsigned int priority, const char *fmt, ...) { va_list va; int rc; @@ -414,7 +414,7 @@ void log__internal(const char *fmt, ...) int mosquitto_log_vprintf(int level, const char *fmt, va_list va) { - return log__vprintf(level, fmt, va); + return log__vprintf((unsigned int)level, fmt, va); } void mosquitto_log_printf(int level, const char *fmt, ...) @@ -422,7 +422,7 @@ void mosquitto_log_printf(int level, const char *fmt, ...) va_list va; va_start(va, fmt); - log__vprintf(level, fmt, va); + log__vprintf((unsigned int)level, fmt, va); va_end(va); } diff --git a/src/loop.c b/src/loop.c index 8e876025..b4f27d2b 100644 --- a/src/loop.c +++ b/src/loop.c @@ -70,7 +70,7 @@ static struct lws_sorted_usec_list sul; static int single_publish(struct mosquitto_db *db, struct mosquitto *context, struct mosquitto_message_v5 *msg) { struct mosquitto_msg_store *stored; - int mid; + uint16_t mid; stored = mosquitto__calloc(1, sizeof(struct mosquitto_msg_store)); if(stored == NULL) return MOSQ_ERR_NOMEM; @@ -78,7 +78,7 @@ static int single_publish(struct mosquitto_db *db, struct mosquitto *context, st stored->topic = msg->topic; msg->topic = NULL; stored->retain = 0; - stored->payloadlen = msg->payloadlen; + stored->payloadlen = (uint32_t)msg->payloadlen; if(UHPA_ALLOC(stored->payload, stored->payloadlen) == 0){ db__msg_store_free(stored); return MOSQ_ERR_NOMEM; @@ -97,7 +97,7 @@ static int single_publish(struct mosquitto_db *db, struct mosquitto *context, st }else{ mid = 0; } - return db__message_insert(db, context, mid, mosq_md_out, msg->qos, 0, stored, msg->properties, true); + return db__message_insert(db, context, mid, mosq_md_out, (uint8_t)msg->qos, 0, stored, msg->properties, true); } @@ -114,7 +114,7 @@ void queue_plugin_msgs(struct mosquitto_db *db) single_publish(db, context, msg); } }else{ - db__messages_easy_queue(db, NULL, msg->topic, msg->qos, msg->payloadlen, msg->payload, msg->retain, 0, &msg->properties); + db__messages_easy_queue(db, NULL, msg->topic, (uint8_t)msg->qos, (uint32_t)msg->payloadlen, msg->payload, msg->retain, 0, &msg->properties); } mosquitto__free(msg->topic); mosquitto__free(msg->payload); diff --git a/src/mosquitto.c b/src/mosquitto.c index e1d0a16d..72942e58 100644 --- a/src/mosquitto.c +++ b/src/mosquitto.c @@ -226,7 +226,7 @@ int listeners__start_single_mqtt(struct mosquitto_db *db, mosq_sock_t **listenso return 1; } (*listensock_count) += listener->sock_count; - listensock_new = mosquitto__realloc(*listensock, sizeof(mosq_sock_t)*(*listensock_count)); + listensock_new = mosquitto__realloc(*listensock, sizeof(mosq_sock_t)*(size_t)(*listensock_count)); if(!listensock_new){ return 1; } @@ -247,7 +247,7 @@ int listeners__add_local(struct mosquitto_db *db, mosq_sock_t **listensock, int { struct mosquitto__listener *listeners; - listeners = mosquitto__realloc(db->config->listeners, (db->config->listener_count+1)*sizeof(struct mosquitto__listener)); + listeners = mosquitto__realloc(db->config->listeners, (size_t)(db->config->listener_count+1)*sizeof(struct mosquitto__listener)); if(listeners == NULL){ return MOSQ_ERR_NOMEM; } @@ -444,7 +444,7 @@ int main(int argc, char *argv[]) srand(st.wSecond + st.wMilliseconds); #else gettimeofday(&tv, NULL); - srand(tv.tv_sec + tv.tv_usec); + srand((unsigned int)(tv.tv_sec + tv.tv_usec)); #endif #ifdef WIN32 diff --git a/src/mosquitto_broker_internal.h b/src/mosquitto_broker_internal.h index d06581fc..5802e3c1 100644 --- a/src/mosquitto_broker_internal.h +++ b/src/mosquitto_broker_internal.h @@ -254,7 +254,7 @@ struct mosquitto__security_options { int8_t allow_anonymous; bool allow_zero_length_clientid; char *auto_id_prefix; - int auto_id_prefix_len; + uint16_t auto_id_prefix_len; struct plugin__callbacks plugin_callbacks; mosquitto_plugin_id_t *pid; /* For registering as a "plugin" */ }; @@ -320,7 +320,7 @@ struct mosquitto__config { struct mosquitto__listener *listeners; int listener_count; bool local_only; - int log_dest; + unsigned int log_dest; int log_facility; unsigned int log_type; bool log_timestamp; @@ -536,17 +536,17 @@ enum mosquitto_bridge_start_type{ struct mosquitto__bridge_topic{ char *topic; - int qos; - enum mosquitto__bridge_direction direction; char *local_prefix; char *remote_prefix; char *local_topic; /* topic prefixed with local_prefix */ char *remote_topic; /* topic prefixed with remote_prefix */ + enum mosquitto__bridge_direction direction; + uint8_t qos; }; struct bridge_address{ char *address; - int port; + uint16_t port; }; struct mosquitto__bridge{ @@ -561,7 +561,7 @@ struct mosquitto__bridge{ bool try_private_accepted; bool clean_start; int8_t clean_start_local; - int keepalive; + uint16_t keepalive; struct mosquitto__bridge_topic *topics; int topic_count; bool topic_remapping; @@ -642,10 +642,10 @@ int restore_privileges(void); /* ============================================================ * Server send functions * ============================================================ */ -int send__connack(struct mosquitto_db *db, struct mosquitto *context, int ack, int reason_code, const mosquitto_property *properties); +int send__connack(struct mosquitto_db *db, struct mosquitto *context, uint8_t ack, uint8_t reason_code, const mosquitto_property *properties); int send__suback(struct mosquitto *context, uint16_t mid, uint32_t payloadlen, const void *payload); int send__unsuback(struct mosquitto *context, uint16_t mid, int reason_code_count, uint8_t *reason_codes, const mosquitto_property *properties); -int send__auth(struct mosquitto_db *db, struct mosquitto *context, int reason_code, const void *auth_data, uint16_t auth_data_len); +int send__auth(struct mosquitto_db *db, struct mosquitto *context, uint8_t reason_code, const void *auth_data, uint16_t auth_data_len); /* ============================================================ * Network functions @@ -654,7 +654,7 @@ void net__broker_init(void); void net__broker_cleanup(void); int net__socket_accept(struct mosquitto_db *db, mosq_sock_t listensock); int net__socket_listen(struct mosquitto__listener *listener); -int net__socket_get_address(mosq_sock_t sock, char *buf, int len); +int net__socket_get_address(mosq_sock_t sock, char *buf, size_t len); int net__tls_load_verify(struct mosquitto__listener *listener); int net__tls_server_ctx(struct mosquitto__listener *listener); @@ -683,12 +683,12 @@ void db__limits_set(unsigned long inflight_bytes, int queued, unsigned long queu /* Return the number of in-flight messages in count. */ int db__message_count(int *count); int db__message_delete_outgoing(struct mosquitto_db *db, struct mosquitto *context, uint16_t mid, enum mosquitto_msg_state expect_state, int qos); -int db__message_insert(struct mosquitto_db *db, struct mosquitto *context, uint16_t mid, enum mosquitto_msg_direction dir, int qos, bool retain, struct mosquitto_msg_store *stored, mosquitto_property *properties, bool update); +int db__message_insert(struct mosquitto_db *db, struct mosquitto *context, uint16_t mid, enum mosquitto_msg_direction dir, uint8_t qos, bool retain, struct mosquitto_msg_store *stored, mosquitto_property *properties, bool update); int db__message_release_incoming(struct mosquitto_db *db, struct mosquitto *context, uint16_t mid); int db__message_update_outgoing(struct mosquitto *context, uint16_t mid, enum mosquitto_msg_state state, int qos); void db__message_dequeue_first(struct mosquitto *context, struct mosquitto_msg_data *msg_data); int db__messages_delete(struct mosquitto_db *db, struct mosquitto *context, bool force_free); -int db__messages_easy_queue(struct mosquitto_db *db, struct mosquitto *context, const char *topic, int qos, uint32_t payloadlen, const void *payload, int retain, uint32_t message_expiry_interval, mosquitto_property **properties); +int db__messages_easy_queue(struct mosquitto_db *db, struct mosquitto *context, const char *topic, uint8_t qos, uint32_t payloadlen, const void *payload, int retain, uint32_t message_expiry_interval, mosquitto_property **properties); int db__message_store(struct mosquitto_db *db, const struct mosquitto *source, struct mosquitto_msg_store *stored, uint32_t message_expiry_interval, dbid_t store_id, enum mosquitto_msg_origin origin); int db__message_store_find(struct mosquitto *context, uint16_t mid, struct mosquitto_msg_store **stored); void db__msg_store_add(struct mosquitto_db *db, struct mosquitto_msg_store *store); @@ -711,13 +711,12 @@ int db__message_write_queued_in(struct mosquitto_db *db, struct mosquitto *conte /* ============================================================ * Subscription functions * ============================================================ */ -int sub__add(struct mosquitto_db *db, struct mosquitto *context, const char *sub, int qos, uint32_t identifier, int options, struct mosquitto__subhier **root); -struct mosquitto__subhier *sub__add_hier_entry(struct mosquitto__subhier *parent, struct mosquitto__subhier **sibling, const char *topic, size_t len); +int sub__add(struct mosquitto_db *db, struct mosquitto *context, const char *sub, uint8_t qos, uint32_t identifier, int options, struct mosquitto__subhier **root); +struct mosquitto__subhier *sub__add_hier_entry(struct mosquitto__subhier *parent, struct mosquitto__subhier **sibling, const char *topic, uint16_t len); int sub__remove(struct mosquitto_db *db, struct mosquitto *context, const char *sub, struct mosquitto__subhier *root, uint8_t *reason); void sub__tree_print(struct mosquitto__subhier *root, int level); int sub__clean_session(struct mosquitto_db *db, struct mosquitto *context); -int sub__retain_queue(struct mosquitto_db *db, struct mosquitto *context, const char *sub, int sub_qos, uint32_t subscription_identifier); -int sub__messages_queue(struct mosquitto_db *db, const char *source_id, const char *topic, int qos, int retain, struct mosquitto_msg_store **stored); +int sub__messages_queue(struct mosquitto_db *db, const char *source_id, const char *topic, uint8_t qos, int retain, struct mosquitto_msg_store **stored); int sub__topic_tokenise(const char *subtopic, char **local_sub, char ***topics, const char **sharename); void sub__topic_tokens_free(struct sub__token *tokens); @@ -751,7 +750,7 @@ int control__unregister_callback(struct mosquitto_db *db, struct mosquitto__secu * ============================================================ */ int log__init(struct mosquitto__config *config); int log__close(struct mosquitto__config *config); -int log__printf(struct mosquitto *mosq, int level, const char *fmt, ...) __attribute__((format(printf, 3, 4))); +int log__printf(struct mosquitto *mosq, unsigned int level, const char *fmt, ...) __attribute__((format(printf, 3, 4))); void log__internal(const char *fmt, ...) __attribute__((format(printf, 1, 2))); /* ============================================================ @@ -769,7 +768,7 @@ int bridge__on_connect(struct mosquitto_db *db, struct mosquitto *context); void bridge__packet_cleanup(struct mosquitto *context); void bridge_check(struct mosquitto_db *db); int bridge__register_local_connections(struct mosquitto_db *db); -int bridge__add_topic(struct mosquitto__bridge *bridge, const char *topic, enum mosquitto__bridge_direction direction, int qos, const char *local_prefix, const char *remote_prefix); +int bridge__add_topic(struct mosquitto__bridge *bridge, const char *topic, enum mosquitto__bridge_direction direction, uint8_t qos, const char *local_prefix, const char *remote_prefix); int bridge__remap_topic_in(struct mosquitto *context, char **topic); #endif @@ -819,7 +818,7 @@ int property__process_disconnect(struct mosquitto *context, mosquitto_property * * ============================================================ */ int retain__init(struct mosquitto_db *db); void retain__clean(struct mosquitto_db *db, struct mosquitto__retainhier **retainhier); -int retain__queue(struct mosquitto_db *db, struct mosquitto *context, const char *sub, int sub_qos, uint32_t subscription_identifier); +int retain__queue(struct mosquitto_db *db, struct mosquitto *context, const char *sub, uint8_t sub_qos, uint32_t subscription_identifier); int retain__store(struct mosquitto_db *db, const char *topic, struct mosquitto_msg_store *stored, char **split_topics); /* ============================================================ @@ -832,7 +831,7 @@ int mosquitto_security_module_cleanup(struct mosquitto_db *db); int mosquitto_security_init(struct mosquitto_db *db, bool reload); int mosquitto_security_apply(struct mosquitto_db *db); int mosquitto_security_cleanup(struct mosquitto_db *db, bool reload); -int mosquitto_acl_check(struct mosquitto_db *db, struct mosquitto *context, const char *topic, long payloadlen, void* payload, int qos, bool retain, int access); +int mosquitto_acl_check(struct mosquitto_db *db, struct mosquitto *context, const char *topic, uint32_t payloadlen, void* payload, uint8_t qos, bool retain, int access); int mosquitto_unpwd_check(struct mosquitto_db *db, struct mosquitto *context); int mosquitto_psk_key_get(struct mosquitto_db *db, struct mosquitto *context, const char *hint, const char *identity, char *key, int max_key_len); diff --git a/src/net.c b/src/net.c index 6494db37..ee07fa59 100644 --- a/src/net.c +++ b/src/net.c @@ -88,7 +88,7 @@ void net__broker_cleanup(void) } -static void net__print_error(int log, const char *format_str) +static void net__print_error(unsigned int log, const char *format_str) { char *buf; @@ -291,12 +291,12 @@ static unsigned int psk_server_callback(SSL *ssl, const char *identity, unsigned psk_key = mosquitto__calloc(1, max_psk_len*2 + 1); if(!psk_key) return 0; - if(mosquitto_psk_key_get(db, context, psk_hint, identity, psk_key, max_psk_len*2) != MOSQ_ERR_SUCCESS){ + if(mosquitto_psk_key_get(db, context, psk_hint, identity, psk_key, (int)max_psk_len*2) != MOSQ_ERR_SUCCESS){ mosquitto__free(psk_key); return 0; } - len = mosquitto__hex2bin(psk_key, psk, max_psk_len); + len = mosquitto__hex2bin(psk_key, psk, (int)max_psk_len); if (len < 0){ mosquitto__free(psk_key); return 0; @@ -311,7 +311,7 @@ static unsigned int psk_server_callback(SSL *ssl, const char *identity, unsigned } mosquitto__free(psk_key); - return len; + return (unsigned int)len; } #endif @@ -383,7 +383,7 @@ int net__tls_server_ctx(struct mosquitto__listener *listener) #endif snprintf(buf, 256, "mosquitto-%d", listener->port); - SSL_CTX_set_session_id_context(listener->ssl_ctx, (unsigned char *)buf, strlen(buf)); + SSL_CTX_set_session_id_context(listener->ssl_ctx, (unsigned char *)buf, (unsigned int)strlen(buf)); if(listener->ciphers){ rc = SSL_CTX_set_cipher_list(listener->ssl_ctx, listener->ciphers); @@ -637,7 +637,7 @@ static int net__socket_listen_tcp(struct mosquitto__listener *listener) continue; } listener->sock_count++; - listener->socks = mosquitto__realloc(listener->socks, sizeof(mosq_sock_t)*listener->sock_count); + listener->socks = mosquitto__realloc(listener->socks, sizeof(mosq_sock_t)*(size_t)listener->sock_count); if(!listener->socks){ log__printf(NULL, MOSQ_LOG_ERR, "Error: Out of memory."); freeaddrinfo(ainfo); @@ -726,7 +726,7 @@ static int net__socket_listen_unix(struct mosquitto__listener *listener) return 1; } listener->sock_count++; - listener->socks = mosquitto__realloc(listener->socks, sizeof(mosq_sock_t)*listener->sock_count); + listener->socks = mosquitto__realloc(listener->socks, sizeof(mosq_sock_t)*(size_t)listener->sock_count); if(!listener->socks){ log__printf(NULL, MOSQ_LOG_ERR, "Error: Out of memory."); return MOSQ_ERR_NOMEM; @@ -818,7 +818,7 @@ int net__socket_listen(struct mosquitto__listener *listener) } } -int net__socket_get_address(mosq_sock_t sock, char *buf, int len) +int net__socket_get_address(mosq_sock_t sock, char *buf, size_t len) { struct sockaddr_storage addr; socklen_t addrlen; @@ -827,11 +827,11 @@ int net__socket_get_address(mosq_sock_t sock, char *buf, int len) addrlen = sizeof(addr); if(!getpeername(sock, (struct sockaddr *)&addr, &addrlen)){ if(addr.ss_family == AF_INET){ - if(inet_ntop(AF_INET, &((struct sockaddr_in *)&addr)->sin_addr.s_addr, buf, len)){ + if(inet_ntop(AF_INET, &((struct sockaddr_in *)&addr)->sin_addr.s_addr, buf, (socklen_t)len)){ return 0; } }else if(addr.ss_family == AF_INET6){ - if(inet_ntop(AF_INET6, &((struct sockaddr_in6 *)&addr)->sin6_addr.s6_addr, buf, len)){ + if(inet_ntop(AF_INET6, &((struct sockaddr_in6 *)&addr)->sin6_addr.s6_addr, buf, (socklen_t)len)){ return 0; } #ifdef WITH_UNIX_SOCKETS diff --git a/src/password_mosq.c b/src/password_mosq.c index 0123bbe0..45971cda 100644 --- a/src/password_mosq.c +++ b/src/password_mosq.c @@ -67,7 +67,7 @@ int base64_encode(unsigned char *in, unsigned int in_len, char **encoded) BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL); bmem = BIO_new(BIO_s_mem()); b64 = BIO_push(b64, bmem); - BIO_write(b64, in, in_len); + BIO_write(b64, in, (int)in_len); if(BIO_flush(b64) != 1){ BIO_free_all(b64); return 1; @@ -89,7 +89,8 @@ int base64_encode(unsigned char *in, unsigned int in_len, char **encoded) int base64__decode(char *in, unsigned char **decoded, unsigned int *decoded_len) { BIO *bmem, *b64; - int slen; + size_t slen; + int len; slen = strlen(in); @@ -105,7 +106,7 @@ int base64__decode(char *in, unsigned char **decoded, unsigned int *decoded_len) return 1; } b64 = BIO_push(b64, bmem); - BIO_write(bmem, in, slen); + BIO_write(bmem, in, (int)slen); if(BIO_flush(bmem) != 1){ BIO_free_all(b64); @@ -116,15 +117,16 @@ int base64__decode(char *in, unsigned char **decoded, unsigned int *decoded_len) BIO_free_all(b64); return 1; } - *decoded_len = BIO_read(b64, *decoded, slen); + len = BIO_read(b64, *decoded, (int)slen); BIO_free_all(b64); - if(*decoded_len <= 0){ + if(len <= 0){ mosquitto__free(*decoded); *decoded = NULL; *decoded_len = 0; return 1; } + *decoded_len = (unsigned int)len; return 0; } @@ -172,9 +174,9 @@ int pw__hash(const char *password, struct mosquitto_pw *pw, bool new_salt) #endif }else{ hash_len = sizeof(pw->password_hash); - PKCS5_PBKDF2_HMAC(password, strlen(password), + PKCS5_PBKDF2_HMAC(password, (int)strlen(password), pw->salt, sizeof(pw->salt), 20000, - digest, hash_len, pw->password_hash); + digest, (int)hash_len, pw->password_hash); } return MOSQ_ERR_SUCCESS; diff --git a/src/persist.h b/src/persist.h index 816ec400..8119e690 100644 --- a/src/persist.h +++ b/src/persist.h @@ -144,19 +144,19 @@ struct P_retain{ int persist__read_string_len(FILE *db_fptr, char **str, uint16_t len); int persist__read_string(FILE *db_fptr, char **str); -int persist__chunk_header_read(FILE *db_fptr, int *chunk, int *length); +int persist__chunk_header_read(FILE *db_fptr, uint32_t *chunk, uint32_t *length); -int persist__chunk_header_read_v234(FILE *db_fptr, int *chunk, int *length); +int persist__chunk_header_read_v234(FILE *db_fptr, uint32_t *chunk, uint32_t *length); int persist__chunk_cfg_read_v234(FILE *db_fptr, struct PF_cfg *chunk); -int persist__chunk_client_read_v234(FILE *db_fptr, struct P_client *chunk, int db_version); +int persist__chunk_client_read_v234(FILE *db_fptr, struct P_client *chunk, uint32_t db_version); int persist__chunk_client_msg_read_v234(FILE *db_fptr, struct P_client_msg *chunk); -int persist__chunk_msg_store_read_v234(FILE *db_fptr, struct P_msg_store *chunk, int db_version); +int persist__chunk_msg_store_read_v234(FILE *db_fptr, struct P_msg_store *chunk, uint32_t db_version); int persist__chunk_retain_read_v234(FILE *db_fptr, struct P_retain *chunk); int persist__chunk_sub_read_v234(FILE *db_fptr, struct P_sub *chunk); -int persist__chunk_header_read_v56(FILE *db_fptr, int *chunk, int *length); +int persist__chunk_header_read_v56(FILE *db_fptr, uint32_t *chunk, uint32_t *length); int persist__chunk_cfg_read_v56(FILE *db_fptr, struct PF_cfg *chunk); -int persist__chunk_client_read_v56(FILE *db_fptr, struct P_client *chunk, int db_version); +int persist__chunk_client_read_v56(FILE *db_fptr, struct P_client *chunk, uint32_t db_version); int persist__chunk_client_msg_read_v56(FILE *db_fptr, struct P_client_msg *chunk, uint32_t length); int persist__chunk_msg_store_read_v56(FILE *db_fptr, struct P_msg_store *chunk, uint32_t length); int persist__chunk_retain_read_v56(FILE *db_fptr, struct P_retain *chunk); diff --git a/src/persist_read.c b/src/persist_read.c index 898a831e..c95c3448 100644 --- a/src/persist_read.c +++ b/src/persist_read.c @@ -41,7 +41,7 @@ uint32_t db_version; const unsigned char magic[15] = {0x00, 0xB5, 0x00, 'm','o','s','q','u','i','t','t','o',' ','d','b'}; -static int persist__restore_sub(struct mosquitto_db *db, const char *client_id, const char *sub, int qos, uint32_t identifier, int options); +static int persist__restore_sub(struct mosquitto_db *db, const char *client_id, const char *sub, uint8_t qos, uint32_t identifier, int options); static struct mosquitto *persist__find_or_add_context(struct mosquitto_db *db, const char *client_id, uint16_t last_mid) { @@ -76,7 +76,7 @@ int persist__read_string_len(FILE *db_fptr, char **str, uint16_t len) char *s = NULL; if(len){ - s = mosquitto__malloc(len+1); + s = mosquitto__malloc(len+1U); if(!s){ fclose(db_fptr); log__printf(NULL, MOSQ_LOG_ERR, "Error: Out of memory."); @@ -406,7 +406,7 @@ static int persist__sub_chunk_restore(struct mosquitto_db *db, FILE *db_fptr) } -int persist__chunk_header_read(FILE *db_fptr, int *chunk, int *length) +int persist__chunk_header_read(FILE *db_fptr, uint32_t *chunk, uint32_t *length) { if(db_version == 6 || db_version == 5){ return persist__chunk_header_read_v56(db_fptr, chunk, length); @@ -423,8 +423,8 @@ int persist__restore(struct mosquitto_db *db) int rc = 0; uint32_t crc; uint32_t i32temp; - int chunk, length; - ssize_t rlen; + uint32_t chunk, length; + size_t rlen; char *err; struct mosquitto_msg_store_load *load, *load_tmp; struct PF_cfg cfg_chunk; @@ -540,7 +540,7 @@ error: return 1; } -static int persist__restore_sub(struct mosquitto_db *db, const char *client_id, const char *sub, int qos, uint32_t identifier, int options) +static int persist__restore_sub(struct mosquitto_db *db, const char *client_id, const char *sub, uint8_t qos, uint32_t identifier, int options) { struct mosquitto *context; diff --git a/src/persist_read_v234.c b/src/persist_read_v234.c index 27f52bd5..8e392207 100644 --- a/src/persist_read_v234.c +++ b/src/persist_read_v234.c @@ -36,7 +36,7 @@ Contributors: #include "util_mosq.h" -int persist__chunk_header_read_v234(FILE *db_fptr, int *chunk, int *length) +int persist__chunk_header_read_v234(FILE *db_fptr, uint32_t *chunk, uint32_t *length) { size_t rlen; uint16_t i16temp; @@ -68,7 +68,7 @@ error: } -int persist__chunk_client_read_v234(FILE *db_fptr, struct P_client *chunk, int db_version) +int persist__chunk_client_read_v234(FILE *db_fptr, struct P_client *chunk, uint32_t db_version) { uint16_t i16temp; int rc; @@ -116,7 +116,7 @@ int persist__chunk_client_msg_read_v234(FILE *db_fptr, struct P_client_msg *chun read_e(db_fptr, &chunk->F.state, sizeof(uint8_t)); read_e(db_fptr, &dup, sizeof(uint8_t)); - chunk->F.retain_dup = (retain&0x0F)<<4 | (dup&0x0F); + chunk->F.retain_dup = (uint8_t)((retain&0x0F)<<4 | (dup&0x0F)); return MOSQ_ERR_SUCCESS; error: @@ -127,7 +127,7 @@ error: } -int persist__chunk_msg_store_read_v234(FILE *db_fptr, struct P_msg_store *chunk, int db_version) +int persist__chunk_msg_store_read_v234(FILE *db_fptr, struct P_msg_store *chunk, uint32_t db_version) { uint32_t i32temp; uint16_t i16temp; diff --git a/src/persist_read_v5.c b/src/persist_read_v5.c index 62ec530b..798c318b 100644 --- a/src/persist_read_v5.c +++ b/src/persist_read_v5.c @@ -38,7 +38,7 @@ Contributors: #include "util_mosq.h" -int persist__chunk_header_read_v56(FILE *db_fptr, int *chunk, int *length) +int persist__chunk_header_read_v56(FILE *db_fptr, uint32_t *chunk, uint32_t *length) { size_t rlen; struct PF_header header; @@ -64,7 +64,7 @@ int persist__chunk_cfg_read_v56(FILE *db_fptr, struct PF_cfg *chunk) } -int persist__chunk_client_read_v56(FILE *db_fptr, struct P_client *chunk, int db_version) +int persist__chunk_client_read_v56(FILE *db_fptr, struct P_client *chunk, uint32_t db_version) { int rc; @@ -115,7 +115,7 @@ int persist__chunk_client_msg_read_v56(FILE *db_fptr, struct P_client_msg *chunk chunk->F.mid = ntohs(chunk->F.mid); chunk->F.id_len = ntohs(chunk->F.id_len); - length -= (sizeof(struct PF_client_msg) + chunk->F.id_len); + length -= (uint32_t)(sizeof(struct PF_client_msg) + chunk->F.id_len); rc = persist__read_string_len(db_fptr, &chunk->client_id, chunk->F.id_len); if(rc){ @@ -164,7 +164,7 @@ int persist__chunk_msg_store_read_v56(FILE *db_fptr, struct P_msg_store *chunk, chunk->F.topic_len = ntohs(chunk->F.topic_len); chunk->F.source_port = ntohs(chunk->F.source_port); - length -= (sizeof(struct PF_msg_store) + chunk->F.payloadlen + chunk->F.source_id_len + chunk->F.source_username_len + chunk->F.topic_len); + length -= (uint32_t)(sizeof(struct PF_msg_store) + chunk->F.payloadlen + chunk->F.source_id_len + chunk->F.source_username_len + chunk->F.topic_len); if(chunk->F.source_id_len){ rc = persist__read_string_len(db_fptr, &chunk->source.id, chunk->F.source_id_len); diff --git a/src/persist_write.c b/src/persist_write.c index 56d0c9c8..8b07eee4 100644 --- a/src/persist_write.c +++ b/src/persist_write.c @@ -62,9 +62,9 @@ static int persist__client_messages_save(struct mosquitto_db *db, FILE *db_fptr, chunk.F.store_id = cmsg->store->db_id; chunk.F.mid = cmsg->mid; - chunk.F.id_len = strlen(context->id); + chunk.F.id_len = (uint16_t)strlen(context->id); chunk.F.qos = cmsg->qos; - chunk.F.retain_dup = (cmsg->retain&0x0F)<<4 | (cmsg->dup&0x0F); + chunk.F.retain_dup = (uint8_t)((cmsg->retain&0x0F)<<4 | (cmsg->dup&0x0F)); chunk.F.direction = cmsg->direction; chunk.F.state = cmsg->state; chunk.client_id = context->id; @@ -120,21 +120,21 @@ static int persist__message_store_save(struct mosquitto_db *db, FILE *db_fptr) chunk.F.payloadlen = stored->payloadlen; chunk.F.source_mid = stored->source_mid; if(stored->source_id){ - chunk.F.source_id_len = strlen(stored->source_id); + chunk.F.source_id_len = (uint16_t)strlen(stored->source_id); chunk.source.id = stored->source_id; }else{ chunk.F.source_id_len = 0; chunk.source.id = NULL; } if(stored->source_username){ - chunk.F.source_username_len = strlen(stored->source_username); + chunk.F.source_username_len = (uint16_t)strlen(stored->source_username); chunk.source.username = stored->source_username; }else{ chunk.F.source_username_len = 0; chunk.source.username = NULL; } - chunk.F.topic_len = strlen(stored->topic); + chunk.F.topic_len = (uint16_t)strlen(stored->topic); chunk.topic = stored->topic; if(stored->source_listener){ @@ -172,10 +172,10 @@ static int persist__client_save(struct mosquitto_db *db, FILE *db_fptr) chunk.F.session_expiry_time = context->session_expiry_time; chunk.F.session_expiry_interval = context->session_expiry_interval; chunk.F.last_mid = context->last_mid; - chunk.F.id_len = strlen(context->id); + chunk.F.id_len = (uint16_t)strlen(context->id); chunk.client_id = context->id; if(context->username){ - chunk.F.username_len = strlen(context->username); + chunk.F.username_len = (uint16_t)strlen(context->username); chunk.username = context->username; if(context->listener){ chunk.F.listener_port = context->listener->port; @@ -228,10 +228,10 @@ static int persist__subs_save(struct mosquitto_db *db, FILE *db_fptr, struct mos while(sub){ if(sub->context->clean_start == false && sub->context->id){ sub_chunk.F.identifier = sub->identifier; - sub_chunk.F.id_len = strlen(sub->context->id); - sub_chunk.F.topic_len = strlen(thistopic); + sub_chunk.F.id_len = (uint16_t)strlen(sub->context->id); + sub_chunk.F.topic_len = (uint16_t)strlen(thistopic); sub_chunk.F.qos = (uint8_t)sub->qos; - sub_chunk.F.options = sub->no_local<<2 | sub->retain_as_published<<3; + sub_chunk.F.options = (uint8_t)(sub->no_local<<2 | sub->retain_as_published<<3); sub_chunk.client_id = sub->context->id; sub_chunk.topic = thistopic; @@ -308,7 +308,7 @@ int persist__backup(struct mosquitto_db *db, bool shutdown) uint32_t crc = 0; char *err; char *outfile = NULL; - int len; + size_t len; struct PF_cfg cfg_chunk; if(db == NULL || db->config == NULL) return MOSQ_ERR_INVAL; diff --git a/src/persist_write_v5.c b/src/persist_write_v5.c index cc6d7ab9..6ea3eba5 100644 --- a/src/persist_write_v5.c +++ b/src/persist_write_v5.c @@ -66,7 +66,7 @@ int persist__chunk_client_write_v6(FILE *db_fptr, struct P_client *chunk) chunk->F.listener_port = htons(chunk->F.listener_port); header.chunk = htonl(DB_CHUNK_CLIENT); - header.length = htonl(sizeof(struct PF_client)+id_len+username_len); + header.length = htonl((uint32_t)sizeof(struct PF_client)+id_len+username_len); write_e(db_fptr, &header, sizeof(struct PF_header)); write_e(db_fptr, &chunk->F, sizeof(struct PF_client)); @@ -100,7 +100,7 @@ int persist__chunk_client_msg_write_v6(FILE *db_fptr, struct P_client_msg *chunk chunk->F.id_len = htons(chunk->F.id_len); header.chunk = htonl(DB_CHUNK_CLIENT_MSG); - header.length = htonl(sizeof(struct PF_client_msg) + id_len + proplen); + header.length = htonl((uint32_t)sizeof(struct PF_client_msg) + id_len + proplen); write_e(db_fptr, &header, sizeof(struct PF_header)); write_e(db_fptr, &chunk->F, sizeof(struct PF_client_msg)); @@ -155,7 +155,7 @@ int persist__chunk_message_store_write_v6(FILE *db_fptr, struct P_msg_store *chu chunk->F.source_port = htons(chunk->F.source_port); header.chunk = htonl(DB_CHUNK_MSG_STORE); - header.length = htonl(sizeof(struct PF_msg_store) + + header.length = htonl((uint32_t)sizeof(struct PF_msg_store) + topic_len + payloadlen + source_id_len + source_username_len + proplen); @@ -203,7 +203,7 @@ int persist__chunk_retain_write_v6(FILE *db_fptr, struct P_retain *chunk) struct PF_header header; header.chunk = htonl(DB_CHUNK_RETAIN); - header.length = htonl(sizeof(struct PF_retain)); + header.length = htonl((uint32_t)sizeof(struct PF_retain)); write_e(db_fptr, &header, sizeof(struct PF_header)); write_e(db_fptr, &chunk->F, sizeof(struct PF_retain)); @@ -226,7 +226,7 @@ int persist__chunk_sub_write_v6(FILE *db_fptr, struct P_sub *chunk) chunk->F.topic_len = htons(chunk->F.topic_len); header.chunk = htonl(DB_CHUNK_SUB); - header.length = htonl(sizeof(struct PF_sub) + + header.length = htonl((uint32_t)sizeof(struct PF_sub) + id_len + topic_len); write_e(db_fptr, &header, sizeof(struct PF_header)); diff --git a/src/plugin_public.c b/src/plugin_public.c index 8ec808ac..17b79d4a 100644 --- a/src/plugin_public.c +++ b/src/plugin_public.c @@ -215,11 +215,11 @@ int mosquitto_broker_publish_copy( return MOSQ_ERR_INVAL; } - payload_out = calloc(1, payloadlen+1); + payload_out = calloc(1, (size_t)(payloadlen+1)); if(payload_out == NULL){ return MOSQ_ERR_NOMEM; } - memcpy(payload_out, payload, payloadlen); + memcpy(payload_out, payload, (size_t)payloadlen); return mosquitto_broker_publish( clientid, diff --git a/src/retain.c b/src/retain.c index 1b4664cf..9af8f010 100644 --- a/src/retain.c +++ b/src/retain.c @@ -27,7 +27,7 @@ Contributors: #include "utlist.h" -static struct mosquitto__retainhier *retain__add_hier_entry(struct mosquitto__retainhier *parent, struct mosquitto__retainhier **sibling, const char *topic, size_t len) +static struct mosquitto__retainhier *retain__add_hier_entry(struct mosquitto__retainhier *parent, struct mosquitto__retainhier **sibling, const char *topic, uint16_t len) { struct mosquitto__retainhier *child; @@ -40,14 +40,14 @@ static struct mosquitto__retainhier *retain__add_hier_entry(struct mosquitto__re } child->parent = parent; child->topic_len = len; - child->topic = mosquitto__malloc(len+1); + child->topic = mosquitto__malloc((size_t)len+1); if(!child->topic){ child->topic_len = 0; mosquitto__free(child); log__printf(NULL, MOSQ_LOG_ERR, "Error: Out of memory."); return NULL; }else{ - strncpy(child->topic, topic, child->topic_len+1); + strncpy(child->topic, topic, (size_t)child->topic_len+1); } HASH_ADD_KEYPTR(hh, *sibling, child->topic, child->topic_len, child); @@ -75,7 +75,7 @@ int retain__store(struct mosquitto_db *db, const char *topic, struct mosquitto_m struct mosquitto__retainhier *retainhier; struct mosquitto__retainhier *branch; int i; - int slen; + size_t slen; assert(stored); assert(split_topics); @@ -87,7 +87,7 @@ int retain__store(struct mosquitto_db *db, const char *topic, struct mosquitto_m slen = strlen(split_topics[i]); HASH_FIND(hh, retainhier->children, split_topics[i], slen, branch); if(branch == NULL){ - branch = retain__add_hier_entry(retainhier, &retainhier->children, split_topics[i], slen); + branch = retain__add_hier_entry(retainhier, &retainhier->children, split_topics[i], (uint16_t)slen); if(branch == NULL){ return MOSQ_ERR_NOMEM; } @@ -122,10 +122,10 @@ int retain__store(struct mosquitto_db *db, const char *topic, struct mosquitto_m } -static int retain__process(struct mosquitto_db *db, struct mosquitto__retainhier *branch, struct mosquitto *context, int sub_qos, uint32_t subscription_identifier, time_t now) +static int retain__process(struct mosquitto_db *db, struct mosquitto__retainhier *branch, struct mosquitto *context, uint8_t sub_qos, uint32_t subscription_identifier, time_t now) { int rc = 0; - int qos; + uint8_t qos; uint16_t mid; mosquitto_property *properties = NULL; struct mosquitto_msg_store *retained; @@ -188,7 +188,7 @@ static int retain__process(struct mosquitto_db *db, struct mosquitto__retainhier } -static int retain__search(struct mosquitto_db *db, struct mosquitto__retainhier *retainhier, char **split_topics, struct mosquitto *context, const char *sub, int sub_qos, uint32_t subscription_identifier, time_t now, int level) +static int retain__search(struct mosquitto_db *db, struct mosquitto__retainhier *retainhier, char **split_topics, struct mosquitto *context, const char *sub, uint8_t sub_qos, uint32_t subscription_identifier, time_t now, int level) { struct mosquitto__retainhier *branch, *branch_tmp; int flag = 0; @@ -247,7 +247,7 @@ static int retain__search(struct mosquitto_db *db, struct mosquitto__retainhier } -int retain__queue(struct mosquitto_db *db, struct mosquitto *context, const char *sub, int sub_qos, uint32_t subscription_identifier) +int retain__queue(struct mosquitto_db *db, struct mosquitto *context, const char *sub, uint8_t sub_qos, uint32_t subscription_identifier) { struct mosquitto__retainhier *retainhier; char *local_sub; diff --git a/src/security.c b/src/security.c index d411a2d7..ebf21652 100644 --- a/src/security.c +++ b/src/security.c @@ -667,7 +667,7 @@ static int acl__check_dollar(const char *topic, int access) } -int mosquitto_acl_check(struct mosquitto_db *db, struct mosquitto *context, const char *topic, long payloadlen, void* payload, int qos, bool retain, int access) +int mosquitto_acl_check(struct mosquitto_db *db, struct mosquitto *context, const char *topic, uint32_t payloadlen, void* payload, uint8_t qos, bool retain, int access) { int rc; int i; diff --git a/src/security_default.c b/src/security_default.c index 56730245..0420e384 100644 --- a/src/security_default.c +++ b/src/security_default.c @@ -362,7 +362,7 @@ static int mosquitto_acl_check_default(int event, void *event_data, void *userda struct mosquitto__acl *acl_root; bool result; int i; - int len, tlen, clen, ulen; + size_t len, tlen, clen, ulen; char *s; struct mosquitto__security_options *security_opts = NULL; struct mosquitto_db *db; @@ -447,10 +447,10 @@ static int mosquitto_acl_check_default(int event, void *event_data, void *userda if(ed->client->username){ ulen = strlen(ed->client->username); - len = tlen + acl_root->ccount*(clen-2) + acl_root->ucount*(ulen-2); + len = tlen + (size_t)acl_root->ccount*(clen-2) + (size_t)acl_root->ucount*(ulen-2); }else{ ulen = 0; - len = tlen + acl_root->ccount*(clen-2); + len = tlen + (size_t)acl_root->ccount*(clen-2); } local_acl = mosquitto__malloc(len+1); if(!local_acl) return 1; /* FIXME */ @@ -503,7 +503,7 @@ static int aclfile__parse(struct mosquitto_db *db, struct mosquitto__security_op char *access_s; int access; int rc = MOSQ_ERR_SUCCESS; - int slen; + size_t slen; int topic_pattern; char *saveptr = NULL; char *buf = NULL; @@ -513,7 +513,7 @@ static int aclfile__parse(struct mosquitto_db *db, struct mosquitto__security_op if(!security_opts) return MOSQ_ERR_INVAL; if(!security_opts->acl_file) return MOSQ_ERR_SUCCESS; - buf = mosquitto__malloc(buflen); + buf = mosquitto__malloc((size_t)buflen); if(buf == NULL){ log__printf(NULL, MOSQ_LOG_ERR, "Error: Out of memory."); return 1; @@ -740,7 +740,7 @@ static int pwfile__parse(const char *file, struct mosquitto__unpwd **root) char *buf; int buflen = 256; - buf = mosquitto__malloc(buflen); + buf = mosquitto__malloc((size_t)buflen); if(buf == NULL){ log__printf(NULL, MOSQ_LOG_ERR, "Error: Out of memory."); return 1; @@ -1074,7 +1074,7 @@ int mosquitto_security_apply_default(struct mosquitto_db *db) struct mosquitto__listener *listener; BIO *subject_bio; char *data_start; - long name_length; + size_t name_length; char *subject; #endif @@ -1194,7 +1194,7 @@ int mosquitto_security_apply_default(struct mosquitto_db *db) subject_bio = BIO_new(BIO_s_mem()); X509_NAME_print_ex(subject_bio, X509_get_subject_name(client_cert), 0, XN_FLAG_RFC2253); data_start = NULL; - name_length = BIO_get_mem_data(subject_bio, &data_start); + name_length = (size_t)BIO_get_mem_data(subject_bio, &data_start); subject = mosquitto__malloc(sizeof(char)*name_length+1); if(!subject){ BIO_free(subject_bio); @@ -1284,7 +1284,7 @@ int mosquitto_psk_key_get_default(struct mosquitto_db *db, struct mosquitto *con HASH_ITER(hh, psk_id_ref, u, tmp){ if(!strcmp(u->username, identity)){ - strncpy(key, u->password, max_key_len); + strncpy(key, u->password, (size_t)max_key_len); return MOSQ_ERR_SUCCESS; } } @@ -1328,9 +1328,9 @@ int pw__digest(const char *password, const unsigned char *salt, unsigned int sal #endif }else{ *hash_len = EVP_MAX_MD_SIZE; - PKCS5_PBKDF2_HMAC(password, strlen(password), - salt, salt_len, 20000, - digest, *hash_len, hash); + PKCS5_PBKDF2_HMAC(password, (int)strlen(password), + salt, (int)salt_len, 20000, + digest, (int)(*hash_len), hash); } return MOSQ_ERR_SUCCESS; diff --git a/src/send_auth.c b/src/send_auth.c index ffbbb046..38a05f99 100644 --- a/src/send_auth.c +++ b/src/send_auth.c @@ -23,7 +23,7 @@ Contributors: #include "property_mosq.h" #include "util_mosq.h" -int send__auth(struct mosquitto_db *db, struct mosquitto *context, int reason_code, const void *auth_data, uint16_t auth_data_len) +int send__auth(struct mosquitto_db *db, struct mosquitto *context, uint8_t reason_code, const void *auth_data, uint16_t auth_data_len) { struct mosquitto__packet *packet = NULL; int rc; diff --git a/src/send_connack.c b/src/send_connack.c index 66962897..b94e7b31 100644 --- a/src/send_connack.c +++ b/src/send_connack.c @@ -23,7 +23,7 @@ Contributors: #include "property_mosq.h" #include "util_mosq.h" -int send__connack(struct mosquitto_db *db, struct mosquitto *context, int ack, int reason_code, const mosquitto_property *properties) +int send__connack(struct mosquitto_db *db, struct mosquitto *context, uint8_t ack, uint8_t reason_code, const mosquitto_property *properties) { struct mosquitto__packet *packet = NULL; int rc; diff --git a/src/send_unsuback.c b/src/send_unsuback.c index 2ff3722f..342f61c3 100644 --- a/src/send_unsuback.c +++ b/src/send_unsuback.c @@ -39,7 +39,7 @@ int send__unsuback(struct mosquitto *mosq, uint16_t mid, int reason_code_count, if(mosq->protocol == mosq_p_mqtt5){ packet->remaining_length += property__get_remaining_length(properties); - packet->remaining_length += reason_code_count; + packet->remaining_length += (uint32_t)reason_code_count; } rc = packet__alloc(packet); @@ -52,7 +52,7 @@ int send__unsuback(struct mosquitto *mosq, uint16_t mid, int reason_code_count, if(mosq->protocol == mosq_p_mqtt5){ property__write_all(packet, properties, true); - packet__write_bytes(packet, reason_codes, reason_code_count); + packet__write_bytes(packet, reason_codes, (uint32_t)reason_code_count); } return packet__queue(mosq, packet); diff --git a/src/subs.c b/src/subs.c index 5062455b..89ff688f 100644 --- a/src/subs.c +++ b/src/subs.c @@ -58,11 +58,11 @@ Contributors: #include "utlist.h" -static int subs__send(struct mosquitto_db *db, struct mosquitto__subleaf *leaf, const char *topic, int qos, int retain, struct mosquitto_msg_store *stored) +static int subs__send(struct mosquitto_db *db, struct mosquitto__subleaf *leaf, const char *topic, uint8_t qos, int retain, struct mosquitto_msg_store *stored) { bool client_retain; uint16_t mid; - int client_qos, msg_qos; + uint8_t client_qos, msg_qos; mosquitto_property *properties = NULL; int rc2; @@ -105,7 +105,7 @@ static int subs__send(struct mosquitto_db *db, struct mosquitto__subleaf *leaf, } -static int subs__shared_process(struct mosquitto_db *db, struct mosquitto__subhier *hier, const char *topic, int qos, int retain, struct mosquitto_msg_store *stored) +static int subs__shared_process(struct mosquitto_db *db, struct mosquitto__subhier *hier, const char *topic, uint8_t qos, int retain, struct mosquitto_msg_store *stored) { int rc = 0, rc2; struct mosquitto__subshared *shared, *shared_tmp; @@ -124,7 +124,7 @@ static int subs__shared_process(struct mosquitto_db *db, struct mosquitto__subhi return rc; } -static int subs__process(struct mosquitto_db *db, struct mosquitto__subhier *hier, const char *source_id, const char *topic, int qos, int retain, struct mosquitto_msg_store *stored) +static int subs__process(struct mosquitto_db *db, struct mosquitto__subhier *hier, const char *source_id, const char *topic, uint8_t qos, int retain, struct mosquitto_msg_store *stored) { int rc = 0; int rc2; @@ -152,7 +152,7 @@ static int subs__process(struct mosquitto_db *db, struct mosquitto__subhier *hie } -static int sub__add_leaf(struct mosquitto *context, int qos, uint32_t identifier, int options, struct mosquitto__subleaf **head, struct mosquitto__subleaf **newleaf) +static int sub__add_leaf(struct mosquitto *context, uint8_t qos, uint32_t identifier, int options, struct mosquitto__subleaf **head, struct mosquitto__subleaf **newleaf) { struct mosquitto__subleaf *leaf; @@ -197,14 +197,14 @@ static void sub__remove_shared_leaf(struct mosquitto__subhier *subhier, struct m } -static int sub__add_shared(struct mosquitto_db *db, struct mosquitto *context, int qos, uint32_t identifier, int options, struct mosquitto__subhier *subhier, const char *sharename) +static int sub__add_shared(struct mosquitto_db *db, struct mosquitto *context, uint8_t qos, uint32_t identifier, int options, struct mosquitto__subhier *subhier, const char *sharename) { struct mosquitto__subleaf *newleaf; struct mosquitto__subshared *shared = NULL; struct mosquitto__subshared_ref **shared_subs; struct mosquitto__subshared_ref *shared_ref; int i; - unsigned int slen; + size_t slen; int rc; slen = strlen(sharename); @@ -250,7 +250,7 @@ static int sub__add_shared(struct mosquitto_db *db, struct mosquitto *context, i } } if(i == context->shared_sub_count){ - shared_subs = mosquitto__realloc(context->shared_subs, sizeof(struct mosquitto__subhier_ref *)*(context->shared_sub_count + 1)); + shared_subs = mosquitto__realloc(context->shared_subs, sizeof(struct mosquitto__subhier_ref *)*(size_t)(context->shared_sub_count + 1)); if(!shared_subs){ sub__remove_shared_leaf(subhier, shared, newleaf); return MOSQ_ERR_NOMEM; @@ -274,7 +274,7 @@ static int sub__add_shared(struct mosquitto_db *db, struct mosquitto *context, i } -static int sub__add_normal(struct mosquitto_db *db, struct mosquitto *context, int qos, uint32_t identifier, int options, struct mosquitto__subhier *subhier) +static int sub__add_normal(struct mosquitto_db *db, struct mosquitto *context, uint8_t qos, uint32_t identifier, int options, struct mosquitto__subhier *subhier) { struct mosquitto__subleaf *newleaf = NULL; struct mosquitto__subhier **subs; @@ -294,7 +294,7 @@ static int sub__add_normal(struct mosquitto_db *db, struct mosquitto *context, i } } if(i == context->sub_count){ - subs = mosquitto__realloc(context->subs, sizeof(struct mosquitto__subhier *)*(context->sub_count + 1)); + subs = mosquitto__realloc(context->subs, sizeof(struct mosquitto__subhier *)*(size_t)(context->sub_count + 1)); if(!subs){ DL_DELETE(subhier->subs, newleaf); mosquitto__free(newleaf); @@ -319,7 +319,7 @@ static int sub__add_normal(struct mosquitto_db *db, struct mosquitto *context, i } -static int sub__add_context(struct mosquitto_db *db, struct mosquitto *context, int qos, uint32_t identifier, int options, struct mosquitto__subhier *subhier, char *const *const topics, const char *sharename) +static int sub__add_context(struct mosquitto_db *db, struct mosquitto *context, uint8_t qos, uint32_t identifier, int options, struct mosquitto__subhier *subhier, char *const *const topics, const char *sharename) { struct mosquitto__subhier *branch; int topic_index = 0; @@ -328,10 +328,13 @@ static int sub__add_context(struct mosquitto_db *db, struct mosquitto *context, /* Find leaf node */ while(topics && topics[topic_index] != NULL){ topiclen = strlen(topics[topic_index]); + if(topiclen > UINT16_MAX){ + return MOSQ_ERR_INVAL; + } HASH_FIND(hh, subhier->children, topics[topic_index], topiclen, branch); if(!branch){ /* Not found */ - branch = sub__add_hier_entry(subhier, &subhier->children, topics[topic_index], topiclen); + branch = sub__add_hier_entry(subhier, &subhier->children, topics[topic_index], (uint16_t)topiclen); if(!branch) return MOSQ_ERR_NOMEM; } subhier = branch; @@ -459,7 +462,7 @@ static int sub__remove_recurse(struct mosquitto_db *db, struct mosquitto *contex } -static int sub__search(struct mosquitto_db *db, struct mosquitto__subhier *subhier, char **split_topics, const char *source_id, const char *topic, int qos, int retain, struct mosquitto_msg_store *stored) +static int sub__search(struct mosquitto_db *db, struct mosquitto__subhier *subhier, char **split_topics, const char *source_id, const char *topic, uint8_t qos, int retain, struct mosquitto_msg_store *stored) { /* FIXME - need to take into account source_id if the client is a bridge */ struct mosquitto__subhier *branch; @@ -531,7 +534,7 @@ static int sub__search(struct mosquitto_db *db, struct mosquitto__subhier *subhi } -struct mosquitto__subhier *sub__add_hier_entry(struct mosquitto__subhier *parent, struct mosquitto__subhier **sibling, const char *topic, size_t len) +struct mosquitto__subhier *sub__add_hier_entry(struct mosquitto__subhier *parent, struct mosquitto__subhier **sibling, const char *topic, uint16_t len) { struct mosquitto__subhier *child; @@ -558,7 +561,7 @@ struct mosquitto__subhier *sub__add_hier_entry(struct mosquitto__subhier *parent } -int sub__add(struct mosquitto_db *db, struct mosquitto *context, const char *sub, int qos, uint32_t identifier, int options, struct mosquitto__subhier **root) +int sub__add(struct mosquitto_db *db, struct mosquitto *context, const char *sub, uint8_t qos, uint32_t identifier, int options, struct mosquitto__subhier **root) { int rc = 0; struct mosquitto__subhier *subhier; @@ -575,9 +578,12 @@ int sub__add(struct mosquitto_db *db, struct mosquitto *context, const char *sub if(rc) return rc; topiclen = strlen(topics[0]); + if(topiclen > UINT16_MAX){ + return MOSQ_ERR_INVAL; + } HASH_FIND(hh, *root, topics[0], topiclen, subhier); if(!subhier){ - subhier = sub__add_hier_entry(NULL, root, topics[0], topiclen); + subhier = sub__add_hier_entry(NULL, root, topics[0], (uint16_t)topiclen); if(!subhier){ mosquitto__free(local_sub); mosquitto__free(topics); @@ -620,7 +626,7 @@ int sub__remove(struct mosquitto_db *db, struct mosquitto *context, const char * return rc; } -int sub__messages_queue(struct mosquitto_db *db, const char *source_id, const char *topic, int qos, int retain, struct mosquitto_msg_store **stored) +int sub__messages_queue(struct mosquitto_db *db, const char *source_id, const char *topic, uint8_t qos, int retain, struct mosquitto_msg_store **stored) { int rc = MOSQ_ERR_SUCCESS, rc2; struct mosquitto__subhier *subhier; diff --git a/src/sys_tree.c b/src/sys_tree.c index 741af11f..33eae9de 100644 --- a/src/sys_tree.c +++ b/src/sys_tree.c @@ -38,46 +38,46 @@ unsigned long g_msgs_sent = 0; unsigned long g_pub_msgs_received = 0; unsigned long g_pub_msgs_sent = 0; unsigned long g_msgs_dropped = 0; -int g_clients_expired = 0; +unsigned int g_clients_expired = 0; unsigned int g_socket_connections = 0; unsigned int g_connection_count = 0; void sys_tree__init(struct mosquitto_db *db) { char buf[64]; - int len; + uint32_t len; if(db->config->sys_interval == 0){ return; } /* Set static $SYS messages */ - len = snprintf(buf, 64, "mosquitto version %s", VERSION); + len = (uint32_t)snprintf(buf, 64, "mosquitto version %s", VERSION); db__messages_easy_queue(db, NULL, "$SYS/broker/version", SYS_TREE_QOS, len, buf, 1, 0, NULL); } static void sys_tree__update_clients(struct mosquitto_db *db, char *buf) { - static int client_count = -1; - static int clients_expired = -1; - static int client_max = 0; - static int disconnected_count = -1; - static int connected_count = -1; - int len; + static unsigned int client_count = UINT_MAX; + static unsigned int clients_expired = UINT_MAX; + static unsigned int client_max = 0; + static unsigned int disconnected_count = UINT_MAX; + static unsigned int connected_count = UINT_MAX; + uint32_t len; - int count_total, count_by_sock; + unsigned int count_total, count_by_sock; count_total = HASH_CNT(hh_id, db->contexts_by_id); count_by_sock = HASH_CNT(hh_sock, db->contexts_by_sock); if(client_count != count_total){ client_count = count_total; - len = snprintf(buf, BUFLEN, "%d", client_count); + len = (uint32_t)snprintf(buf, BUFLEN, "%d", client_count); db__messages_easy_queue(db, NULL, "$SYS/broker/clients/total", SYS_TREE_QOS, len, buf, 1, 60, NULL); if(client_count > client_max){ client_max = client_count; - len = snprintf(buf, BUFLEN, "%d", client_max); + len = (uint32_t)snprintf(buf, BUFLEN, "%d", client_max); db__messages_easy_queue(db, NULL, "$SYS/broker/clients/maximum", SYS_TREE_QOS, len, buf, 1, 60, NULL); } } @@ -91,19 +91,19 @@ static void sys_tree__update_clients(struct mosquitto_db *db, char *buf) * not last for long, so just cap at zero and ignore. */ disconnected_count = 0; } - len = snprintf(buf, BUFLEN, "%d", disconnected_count); + len = (uint32_t)snprintf(buf, BUFLEN, "%d", disconnected_count); db__messages_easy_queue(db, NULL, "$SYS/broker/clients/inactive", SYS_TREE_QOS, len, buf, 1, 60, NULL); db__messages_easy_queue(db, NULL, "$SYS/broker/clients/disconnected", SYS_TREE_QOS, len, buf, 1, 60, NULL); } if(connected_count != count_by_sock){ connected_count = count_by_sock; - len = snprintf(buf, BUFLEN, "%d", connected_count); + len = (uint32_t)snprintf(buf, BUFLEN, "%d", connected_count); db__messages_easy_queue(db, NULL, "$SYS/broker/clients/active", SYS_TREE_QOS, len, buf, 1, 60, NULL); db__messages_easy_queue(db, NULL, "$SYS/broker/clients/connected", SYS_TREE_QOS, len, buf, 1, 60, NULL); } if(g_clients_expired != clients_expired){ clients_expired = g_clients_expired; - len = snprintf(buf, BUFLEN, "%d", clients_expired); + len = (uint32_t)snprintf(buf, BUFLEN, "%d", clients_expired); db__messages_easy_queue(db, NULL, "$SYS/broker/clients/expired", SYS_TREE_QOS, len, buf, 1, 60, NULL); } } @@ -111,21 +111,21 @@ static void sys_tree__update_clients(struct mosquitto_db *db, char *buf) #ifdef REAL_WITH_MEMORY_TRACKING static void sys_tree__update_memory(struct mosquitto_db *db, char *buf) { - static unsigned long current_heap = -1; - static unsigned long max_heap = -1; + static unsigned long current_heap = ULONG_MAX; + static unsigned long max_heap = ULONG_MAX; unsigned long value_ul; - int len; + uint32_t len; value_ul = mosquitto__memory_used(); if(current_heap != value_ul){ current_heap = value_ul; - len = snprintf(buf, BUFLEN, "%lu", current_heap); + len = (uint32_t)snprintf(buf, BUFLEN, "%lu", current_heap); db__messages_easy_queue(db, NULL, "$SYS/broker/heap/current", SYS_TREE_QOS, len, buf, 1, 60, NULL); } value_ul =mosquitto__max_memory_used(); if(max_heap != value_ul){ max_heap = value_ul; - len = snprintf(buf, BUFLEN, "%lu", max_heap); + len = (uint32_t)snprintf(buf, BUFLEN, "%lu", max_heap); db__messages_easy_queue(db, NULL, "$SYS/broker/heap/maximum", SYS_TREE_QOS, len, buf, 1, 60, NULL); } } @@ -134,16 +134,16 @@ static void sys_tree__update_memory(struct mosquitto_db *db, char *buf) static void calc_load(struct mosquitto_db *db, char *buf, const char *topic, bool initial, double exponent, double interval, double *current) { double new_value; - int len; + uint32_t len; if (initial) { new_value = *current; - len = snprintf(buf, BUFLEN, "%.2f", new_value); + len = (uint32_t)snprintf(buf, BUFLEN, "%.2f", new_value); db__messages_easy_queue(db, NULL, topic, SYS_TREE_QOS, len, buf, 1, 60, NULL); } else { new_value = interval + exponent*((*current) - interval); if(fabs(new_value - (*current)) >= 0.01){ - len = snprintf(buf, BUFLEN, "%.2f", new_value); + len = (uint32_t)snprintf(buf, BUFLEN, "%.2f", new_value); db__messages_easy_queue(db, NULL, topic, SYS_TREE_QOS, len, buf, 1, 60, NULL); } } @@ -163,20 +163,20 @@ void sys_tree__update(struct mosquitto_db *db, int interval, time_t start_time) time_t uptime; char buf[BUFLEN]; - static int msg_store_count = -1; - static unsigned long msg_store_bytes = -1; - static unsigned long msgs_received = -1; - static unsigned long msgs_sent = -1; - static unsigned long publish_dropped = -1; - static unsigned long pub_msgs_received = -1; - static unsigned long pub_msgs_sent = -1; - static unsigned long long bytes_received = -1; - static unsigned long long bytes_sent = -1; - static unsigned long long pub_bytes_received = -1; - static unsigned long long pub_bytes_sent = -1; - static int subscription_count = -1; - static int shared_subscription_count = -1; - static int retained_count = -1; + static int msg_store_count = INT_MAX; + static unsigned long msg_store_bytes = ULONG_MAX; + static unsigned long msgs_received = ULONG_MAX; + static unsigned long msgs_sent = ULONG_MAX; + static unsigned long publish_dropped = ULONG_MAX; + static unsigned long pub_msgs_received = ULONG_MAX; + static unsigned long pub_msgs_sent = ULONG_MAX; + static unsigned long long bytes_received = ULLONG_MAX; + static unsigned long long bytes_sent = ULLONG_MAX; + static unsigned long long pub_bytes_received = ULLONG_MAX; + static unsigned long long pub_bytes_sent = ULLONG_MAX; + static int subscription_count = INT_MAX; + static int shared_subscription_count = INT_MAX; + static int retained_count = INT_MAX; static double msgs_received_load1 = 0; static double msgs_received_load5 = 0; @@ -217,14 +217,14 @@ void sys_tree__update(struct mosquitto_db *db, int interval, time_t start_time) double exponent; double i_mult; - int len; + uint32_t len; bool initial_publish; now = mosquitto_time(); if(interval && now - interval > last_update){ uptime = now - start_time; - len = snprintf(buf, BUFLEN, "%d seconds", (int)uptime); + len = (uint32_t)snprintf(buf, BUFLEN, "%d seconds", (int)uptime); db__messages_easy_queue(db, NULL, "$SYS/broker/uptime", SYS_TREE_QOS, len, buf, 1, 60, NULL); sys_tree__update_clients(db, buf); @@ -236,15 +236,15 @@ void sys_tree__update(struct mosquitto_db *db, int interval, time_t start_time) if(last_update > 0){ i_mult = 60.0/(double)(now-last_update); - msgs_received_interval = (g_msgs_received - msgs_received)*i_mult; - msgs_sent_interval = (g_msgs_sent - msgs_sent)*i_mult; - publish_dropped_interval = (g_msgs_dropped - publish_dropped)*i_mult; + msgs_received_interval = (double)(g_msgs_received - msgs_received)*i_mult; + msgs_sent_interval = (double)(g_msgs_sent - msgs_sent)*i_mult; + publish_dropped_interval = (double)(g_msgs_dropped - publish_dropped)*i_mult; - publish_received_interval = (g_pub_msgs_received - pub_msgs_received)*i_mult; - publish_sent_interval = (g_pub_msgs_sent - pub_msgs_sent)*i_mult; + publish_received_interval = (double)(g_pub_msgs_received - pub_msgs_received)*i_mult; + publish_sent_interval = (double)(g_pub_msgs_sent - pub_msgs_sent)*i_mult; - bytes_received_interval = (g_bytes_received - bytes_received)*i_mult; - bytes_sent_interval = (g_bytes_sent - bytes_sent)*i_mult; + bytes_received_interval = (double)(g_bytes_received - bytes_received)*i_mult; + bytes_sent_interval = (double)(g_bytes_sent - bytes_sent)*i_mult; socket_interval = g_socket_connections*i_mult; g_socket_connections = 0; @@ -252,7 +252,7 @@ void sys_tree__update(struct mosquitto_db *db, int interval, time_t start_time) g_connection_count = 0; /* 1 minute load */ - exponent = exp(-1.0*(now-last_update)/60.0); + exponent = exp(-1.0*(double)(now-last_update)/60.0); calc_load(db, buf, "$SYS/broker/load/messages/received/1min", initial_publish, exponent, msgs_received_interval, &msgs_received_load1); calc_load(db, buf, "$SYS/broker/load/messages/sent/1min", initial_publish, exponent, msgs_sent_interval, &msgs_sent_load1); @@ -265,7 +265,7 @@ void sys_tree__update(struct mosquitto_db *db, int interval, time_t start_time) calc_load(db, buf, "$SYS/broker/load/connections/1min", initial_publish, exponent, connection_interval, &connection_load1); /* 5 minute load */ - exponent = exp(-1.0*(now-last_update)/300.0); + exponent = exp(-1.0*(double)(now-last_update)/300.0); calc_load(db, buf, "$SYS/broker/load/messages/received/5min", initial_publish, exponent, msgs_received_interval, &msgs_received_load5); calc_load(db, buf, "$SYS/broker/load/messages/sent/5min", initial_publish, exponent, msgs_sent_interval, &msgs_sent_load5); @@ -278,7 +278,7 @@ void sys_tree__update(struct mosquitto_db *db, int interval, time_t start_time) calc_load(db, buf, "$SYS/broker/load/connections/5min", initial_publish, exponent, connection_interval, &connection_load5); /* 15 minute load */ - exponent = exp(-1.0*(now-last_update)/900.0); + exponent = exp(-1.0*(double)(now-last_update)/900.0); calc_load(db, buf, "$SYS/broker/load/messages/received/15min", initial_publish, exponent, msgs_received_interval, &msgs_received_load15); calc_load(db, buf, "$SYS/broker/load/messages/sent/15min", initial_publish, exponent, msgs_sent_interval, &msgs_sent_load15); @@ -293,32 +293,32 @@ void sys_tree__update(struct mosquitto_db *db, int interval, time_t start_time) if(db->msg_store_count != msg_store_count){ msg_store_count = db->msg_store_count; - len = snprintf(buf, BUFLEN, "%d", msg_store_count); + len = (uint32_t)snprintf(buf, BUFLEN, "%d", msg_store_count); db__messages_easy_queue(db, NULL, "$SYS/broker/messages/stored", SYS_TREE_QOS, len, buf, 1, 60, NULL); db__messages_easy_queue(db, NULL, "$SYS/broker/store/messages/count", SYS_TREE_QOS, len, buf, 1, 60, NULL); } if (db->msg_store_bytes != msg_store_bytes){ msg_store_bytes = db->msg_store_bytes; - len = snprintf(buf, BUFLEN, "%lu", msg_store_bytes); + len = (uint32_t)snprintf(buf, BUFLEN, "%lu", msg_store_bytes); db__messages_easy_queue(db, NULL, "$SYS/broker/store/messages/bytes", SYS_TREE_QOS, len, buf, 1, 60, NULL); } if(db->subscription_count != subscription_count){ subscription_count = db->subscription_count; - len = snprintf(buf, BUFLEN, "%d", subscription_count); + len = (uint32_t)snprintf(buf, BUFLEN, "%d", subscription_count); db__messages_easy_queue(db, NULL, "$SYS/broker/subscriptions/count", SYS_TREE_QOS, len, buf, 1, 60, NULL); } if(db->shared_subscription_count != shared_subscription_count){ shared_subscription_count = db->shared_subscription_count; - len = snprintf(buf, BUFLEN, "%d", shared_subscription_count); + len = (uint32_t)snprintf(buf, BUFLEN, "%d", shared_subscription_count); db__messages_easy_queue(db, NULL, "$SYS/broker/shared_subscriptions/count", SYS_TREE_QOS, len, buf, 1, 60, NULL); } if(db->retained_count != retained_count){ retained_count = db->retained_count; - len = snprintf(buf, BUFLEN, "%d", retained_count); + len = (uint32_t)snprintf(buf, BUFLEN, "%d", retained_count); db__messages_easy_queue(db, NULL, "$SYS/broker/retained messages/count", SYS_TREE_QOS, len, buf, 1, 60, NULL); } @@ -328,55 +328,55 @@ void sys_tree__update(struct mosquitto_db *db, int interval, time_t start_time) if(msgs_received != g_msgs_received){ msgs_received = g_msgs_received; - len = snprintf(buf, BUFLEN, "%lu", msgs_received); + len = (uint32_t)snprintf(buf, BUFLEN, "%lu", msgs_received); db__messages_easy_queue(db, NULL, "$SYS/broker/messages/received", SYS_TREE_QOS, len, buf, 1, 60, NULL); } if(msgs_sent != g_msgs_sent){ msgs_sent = g_msgs_sent; - len = snprintf(buf, BUFLEN, "%lu", msgs_sent); + len = (uint32_t)snprintf(buf, BUFLEN, "%lu", msgs_sent); db__messages_easy_queue(db, NULL, "$SYS/broker/messages/sent", SYS_TREE_QOS, len, buf, 1, 60, NULL); } if(publish_dropped != g_msgs_dropped){ publish_dropped = g_msgs_dropped; - len = snprintf(buf, BUFLEN, "%lu", publish_dropped); + len = (uint32_t)snprintf(buf, BUFLEN, "%lu", publish_dropped); db__messages_easy_queue(db, NULL, "$SYS/broker/publish/messages/dropped", SYS_TREE_QOS, len, buf, 1, 60, NULL); } if(pub_msgs_received != g_pub_msgs_received){ pub_msgs_received = g_pub_msgs_received; - len = snprintf(buf, BUFLEN, "%lu", pub_msgs_received); + len = (uint32_t)snprintf(buf, BUFLEN, "%lu", pub_msgs_received); db__messages_easy_queue(db, NULL, "$SYS/broker/publish/messages/received", SYS_TREE_QOS, len, buf, 1, 60, NULL); } if(pub_msgs_sent != g_pub_msgs_sent){ pub_msgs_sent = g_pub_msgs_sent; - len = snprintf(buf, BUFLEN, "%lu", pub_msgs_sent); + len = (uint32_t)snprintf(buf, BUFLEN, "%lu", pub_msgs_sent); db__messages_easy_queue(db, NULL, "$SYS/broker/publish/messages/sent", SYS_TREE_QOS, len, buf, 1, 60, NULL); } if(bytes_received != g_bytes_received){ bytes_received = g_bytes_received; - len = snprintf(buf, BUFLEN, "%llu", bytes_received); + len = (uint32_t)snprintf(buf, BUFLEN, "%llu", bytes_received); db__messages_easy_queue(db, NULL, "$SYS/broker/bytes/received", SYS_TREE_QOS, len, buf, 1, 60, NULL); } if(bytes_sent != g_bytes_sent){ bytes_sent = g_bytes_sent; - len = snprintf(buf, BUFLEN, "%llu", bytes_sent); + len = (uint32_t)snprintf(buf, BUFLEN, "%llu", bytes_sent); db__messages_easy_queue(db, NULL, "$SYS/broker/bytes/sent", SYS_TREE_QOS, len, buf, 1, 60, NULL); } if(pub_bytes_received != g_pub_bytes_received){ pub_bytes_received = g_pub_bytes_received; - len = snprintf(buf, BUFLEN, "%llu", pub_bytes_received); + len = (uint32_t)snprintf(buf, BUFLEN, "%llu", pub_bytes_received); db__messages_easy_queue(db, NULL, "$SYS/broker/publish/bytes/received", SYS_TREE_QOS, len, buf, 1, 60, NULL); } if(pub_bytes_sent != g_pub_bytes_sent){ pub_bytes_sent = g_pub_bytes_sent; - len = snprintf(buf, BUFLEN, "%llu", pub_bytes_sent); + len = (uint32_t)snprintf(buf, BUFLEN, "%llu", pub_bytes_sent); db__messages_easy_queue(db, NULL, "$SYS/broker/publish/bytes/sent", SYS_TREE_QOS, len, buf, 1, 60, NULL); } diff --git a/src/sys_tree.h b/src/sys_tree.h index 6b9804c4..da11afac 100644 --- a/src/sys_tree.h +++ b/src/sys_tree.h @@ -31,8 +31,8 @@ extern int g_clients_expired; extern unsigned int g_socket_connections; extern unsigned int g_connection_count; -#define G_BYTES_RECEIVED_INC(A) (g_bytes_received+=(A)) -#define G_BYTES_SENT_INC(A) (g_bytes_sent+=(A)) +#define G_BYTES_RECEIVED_INC(A) (g_bytes_received+=(uint64_t)(A)) +#define G_BYTES_SENT_INC(A) (g_bytes_sent+=(uint64_t)(A)) #define G_PUB_BYTES_RECEIVED_INC(A) (g_pub_bytes_received+=(A)) #define G_PUB_BYTES_SENT_INC(A) (g_pub_bytes_sent+=(A)) #define G_MSGS_RECEIVED_INC(A) (g_msgs_received+=(A)) diff --git a/src/topic_tok.c b/src/topic_tok.c index 244ed88c..8b3c8c79 100644 --- a/src/topic_tok.c +++ b/src/topic_tok.c @@ -61,7 +61,7 @@ int sub__topic_tokenise(const char *subtopic, char **local_sub, char ***topics, int count; int topic_index = 0; int i; - int len; + size_t len; len = strlen(subtopic); if(len == 0){ @@ -77,7 +77,7 @@ int sub__topic_tokenise(const char *subtopic, char **local_sub, char ***topics, saveptr = strchr(&saveptr[1], '/'); count++; } - *topics = mosquitto__calloc(count+3 /* 3=$shared,sharename,NULL */, sizeof(char *)); + *topics = mosquitto__calloc((size_t)(count+3) /* 3=$shared,sharename,NULL */, sizeof(char *)); if((*topics) == NULL){ mosquitto__free(*local_sub); return MOSQ_ERR_NOMEM; diff --git a/src/will_delay.c b/src/will_delay.c index b50b5edd..8a66f1fa 100644 --- a/src/will_delay.c +++ b/src/will_delay.c @@ -30,7 +30,7 @@ static time_t last_check = 0; static int will_delay__cmp(struct will_delay_list *i1, struct will_delay_list *i2) { - return i1->context->will_delay_interval - i2->context->will_delay_interval; + return (int)(i1->context->will_delay_interval - i2->context->will_delay_interval); } diff --git a/test/unit/Makefile b/test/unit/Makefile index e6a25b56..3faf173b 100644 --- a/test/unit/Makefile +++ b/test/unit/Makefile @@ -4,7 +4,7 @@ include ../../config.mk CPPFLAGS:=$(CPPFLAGS) -I../.. -I../../include -I../../lib -I../../src ifeq ($(WITH_BUNDLED_DEPS),yes) - CPPFLAGS:=$(CPPFLAGS) -I../../src/deps + CPPFLAGS:=$(CPPFLAGS) -I../../deps endif CFLAGS:=$(CFLAGS) -coverage -Wall -ggdb diff --git a/test/unit/datatype_read.c b/test/unit/datatype_read.c index 6001cbe3..a67db85a 100644 --- a/test/unit/datatype_read.c +++ b/test/unit/datatype_read.c @@ -5,7 +5,7 @@ static void byte_read_helper( uint8_t *payload, - int remaining_length, + uint32_t remaining_length, int rc_expected, uint8_t value_expected) { @@ -24,7 +24,7 @@ static void byte_read_helper( static void uint16_read_helper( uint8_t *payload, - int remaining_length, + uint32_t remaining_length, int rc_expected, uint16_t value_expected) { @@ -43,7 +43,7 @@ static void uint16_read_helper( static void uint32_read_helper( uint8_t *payload, - int remaining_length, + uint32_t remaining_length, int rc_expected, uint32_t value_expected) { @@ -62,14 +62,14 @@ static void uint32_read_helper( static void varint_read_helper( uint8_t *payload, - int remaining_length, + uint32_t remaining_length, int rc_expected, - int32_t value_expected, - int8_t bytes_expected) + uint32_t value_expected, + uint8_t bytes_expected) { struct mosquitto__packet packet; - int32_t value = -1; - int8_t bytes = -1; + uint32_t value = UINT32_MAX; + uint8_t bytes = UINT8_MAX; int rc; memset(&packet, 0, sizeof(struct mosquitto__packet)); @@ -84,14 +84,14 @@ static void varint_read_helper( static void binary_read_helper( uint8_t *payload, - int remaining_length, + uint32_t remaining_length, int rc_expected, const uint8_t *value_expected, int length_expected) { struct mosquitto__packet packet; uint8_t *value = NULL; - int length = -1; + uint16_t length = UINT16_MAX; int rc; memset(&packet, 0, sizeof(struct mosquitto__packet)); @@ -112,14 +112,14 @@ static void binary_read_helper( static void string_read_helper( uint8_t *payload, - int remaining_length, + uint32_t remaining_length, int rc_expected, const uint8_t *value_expected, - int length_expected) + uint16_t length_expected) { struct mosquitto__packet packet; uint8_t *value = NULL; - int length = -1; + uint16_t length = UINT16_MAX; int rc; memset(&packet, 0, sizeof(struct mosquitto__packet)); @@ -141,7 +141,7 @@ static void string_read_helper( static void bytes_read_helper( uint8_t *payload, - int remaining_length, + uint32_t remaining_length, int rc_expected, const uint8_t *value_expected, int count) @@ -154,7 +154,7 @@ static void bytes_read_helper( memset(&packet, 0, sizeof(struct mosquitto__packet)); packet.payload = payload; packet.remaining_length = remaining_length; - rc = packet__read_bytes(&packet, value, count); + rc = packet__read_bytes(&packet, value, (uint32_t)count); CU_ASSERT_EQUAL(rc, rc_expected); if(rc == MOSQ_ERR_SUCCESS){ CU_ASSERT_EQUAL(packet.pos, count); @@ -367,7 +367,7 @@ static void TEST_uint32_read_success(void) static void TEST_varint_read_empty(void) { /* Empty packet */ - varint_read_helper(NULL, 0, MOSQ_ERR_PROTOCOL, -1, -1); + varint_read_helper(NULL, 0, MOSQ_ERR_PROTOCOL, UINT32_MAX, UINT8_MAX); } @@ -383,20 +383,20 @@ static void TEST_varint_read_truncated(void) /* Varint bigger than packet */ memset(payload, 0, sizeof(payload)); payload[0] = 0x80; - varint_read_helper(payload, 1, MOSQ_ERR_PROTOCOL, -1, -1); + varint_read_helper(payload, 1, MOSQ_ERR_PROTOCOL, UINT32_MAX, UINT8_MAX); /* Varint bigger than packet */ memset(payload, 1, sizeof(payload)); payload[0] = 0x80; payload[1] = 0x80; - varint_read_helper(payload, 2, MOSQ_ERR_PROTOCOL, -1, -1); + varint_read_helper(payload, 2, MOSQ_ERR_PROTOCOL, UINT32_MAX, UINT8_MAX); /* Varint bigger than packet */ memset(payload, 0, sizeof(payload)); payload[0] = 0x80; payload[1] = 0x80; payload[2] = 0x80; - varint_read_helper(payload, 3, MOSQ_ERR_PROTOCOL, -1, -1); + varint_read_helper(payload, 3, MOSQ_ERR_PROTOCOL, UINT32_MAX, UINT8_MAX); /* Varint bigger than packet */ memset(payload, 0, sizeof(payload)); @@ -404,7 +404,7 @@ static void TEST_varint_read_truncated(void) payload[1] = 0x80; payload[2] = 0x80; payload[3] = 0x80; - varint_read_helper(payload, 4, MOSQ_ERR_PROTOCOL, -1, -1); + varint_read_helper(payload, 4, MOSQ_ERR_PROTOCOL, UINT32_MAX, UINT8_MAX); } @@ -486,7 +486,7 @@ static void TEST_varint_read_5_bytes(void) payload[2] = 0x80; payload[3] = 0x80; payload[4] = 0x01; - varint_read_helper(payload, 5, MOSQ_ERR_PROTOCOL, -1, -1); + varint_read_helper(payload, 5, MOSQ_ERR_PROTOCOL, UINT32_MAX, UINT8_MAX); } @@ -503,27 +503,27 @@ static void TEST_varint_read_overlong_encoding(void) memset(payload, 0, sizeof(payload)); payload[0] = 0x80; payload[1] = 0x00; - varint_read_helper(payload, 2, MOSQ_ERR_PROTOCOL, -1, -1); + varint_read_helper(payload, 2, MOSQ_ERR_PROTOCOL, UINT32_MAX, UINT8_MAX); /* Overlong encoding of 127 (1 byte value encoded as 2 bytes) */ memset(payload, 0, sizeof(payload)); payload[0] = 0xFF; payload[1] = 0x00; - varint_read_helper(payload, 2, MOSQ_ERR_PROTOCOL, -1, -1); + varint_read_helper(payload, 2, MOSQ_ERR_PROTOCOL, UINT32_MAX, UINT8_MAX); /* Overlong encoding of 128 (2 byte value encoded as 3 bytes) */ memset(payload, 0, sizeof(payload)); payload[0] = 0x80; payload[1] = 0x81; payload[2] = 0x00; - varint_read_helper(payload, 3, MOSQ_ERR_PROTOCOL, -1, -1); + varint_read_helper(payload, 3, MOSQ_ERR_PROTOCOL, UINT32_MAX, UINT8_MAX); /* Overlong encoding of 16,383 (2 byte value encoded as 3 bytes) */ memset(payload, 0, sizeof(payload)); payload[0] = 0xFF; payload[1] = 0xFF; payload[2] = 0x00; - varint_read_helper(payload, 3, MOSQ_ERR_PROTOCOL, -1, -1); + varint_read_helper(payload, 3, MOSQ_ERR_PROTOCOL, UINT32_MAX, UINT8_MAX); /* Overlong encoding of 16,384 (3 byte value encoded as 4 bytes) */ memset(payload, 0, sizeof(payload)); @@ -531,7 +531,7 @@ static void TEST_varint_read_overlong_encoding(void) payload[1] = 0x80; payload[2] = 0x81; payload[3] = 0x00; - varint_read_helper(payload, 4, MOSQ_ERR_PROTOCOL, -1, -1); + varint_read_helper(payload, 4, MOSQ_ERR_PROTOCOL, UINT32_MAX, UINT8_MAX); /* Overlong encoding of 2,097,151 (3 byte value encoded as 4 bytes) */ memset(payload, 0, sizeof(payload)); @@ -539,7 +539,7 @@ static void TEST_varint_read_overlong_encoding(void) payload[1] = 0xFF; payload[2] = 0xFF; payload[3] = 0x00; - varint_read_helper(payload, 4, MOSQ_ERR_PROTOCOL, -1, -1); + varint_read_helper(payload, 4, MOSQ_ERR_PROTOCOL, UINT32_MAX, UINT8_MAX); } @@ -554,7 +554,7 @@ static void TEST_varint_read_overlong_encoding(void) */ static void TEST_string_read_empty(void) { - string_read_helper(NULL, 0, MOSQ_ERR_PROTOCOL, NULL, -1); + string_read_helper(NULL, 0, MOSQ_ERR_PROTOCOL, NULL, UINT16_MAX); } /* This tests reading a UTF-8 Encoded String from an incoming packet. @@ -569,20 +569,20 @@ static void TEST_string_read_truncated(void) /* 1 byte packet */ memset(payload, 0, sizeof(payload)); payload[0] = 0x02; - string_read_helper(payload, 1, MOSQ_ERR_PROTOCOL, NULL, -1); + string_read_helper(payload, 1, MOSQ_ERR_PROTOCOL, NULL, UINT16_MAX); /* 2 byte packet */ memset(payload, 0, sizeof(payload)); payload[0] = 0x02; payload[1] = 0x02; - string_read_helper(payload, 2, MOSQ_ERR_PROTOCOL, NULL, -1); + string_read_helper(payload, 2, MOSQ_ERR_PROTOCOL, NULL, UINT16_MAX); /* 3 byte packet */ memset(payload, 0, sizeof(payload)); payload[0] = 0x00; payload[1] = 0x02; payload[2] = 'a'; - string_read_helper(payload, 3, MOSQ_ERR_PROTOCOL, NULL, -1); + string_read_helper(payload, 3, MOSQ_ERR_PROTOCOL, NULL, UINT16_MAX); /* 3 byte packet */ memset(payload, 0, sizeof(payload)); @@ -590,7 +590,7 @@ static void TEST_string_read_truncated(void) payload[1] = 0x03; payload[2] = 'a'; payload[3] = 'b'; - string_read_helper(payload, 4, MOSQ_ERR_PROTOCOL, NULL, -1); + string_read_helper(payload, 4, MOSQ_ERR_PROTOCOL, NULL, UINT16_MAX); } @@ -655,7 +655,7 @@ static void TEST_string_read_malformed_string(void) payload[6] = 0xED; /* U+D800 - single UTF-16 surrogate */ payload[7] = 0xA0; payload[8] = 0x80; - string_read_helper(payload, 9, MOSQ_ERR_MALFORMED_UTF8, NULL, -1); + string_read_helper(payload, 9, MOSQ_ERR_MALFORMED_UTF8, NULL, 0); } @@ -697,7 +697,7 @@ static void TEST_string_read_mqtt_1_5_4_3(void) */ static void TEST_binary_data_read_empty(void) { - binary_read_helper(NULL, 0, MOSQ_ERR_PROTOCOL, NULL, -1); + binary_read_helper(NULL, 0, MOSQ_ERR_PROTOCOL, NULL, UINT16_MAX); } @@ -713,20 +713,20 @@ static void TEST_binary_data_read_truncated(void) /* 1 byte packet */ memset(payload, 0, sizeof(payload)); payload[0] = 0x02; - binary_read_helper(payload, 1, MOSQ_ERR_PROTOCOL, NULL, -1); + binary_read_helper(payload, 1, MOSQ_ERR_PROTOCOL, NULL, UINT16_MAX); /* 2 byte packet */ memset(payload, 0, sizeof(payload)); payload[0] = 0x02; payload[1] = 0x02; - binary_read_helper(payload, 2, MOSQ_ERR_PROTOCOL, NULL, -1); + binary_read_helper(payload, 2, MOSQ_ERR_PROTOCOL, NULL, UINT16_MAX); /* 3 byte packet */ memset(payload, 0, sizeof(payload)); payload[0] = 0x00; payload[1] = 0x02; payload[2] = 'a'; - binary_read_helper(payload, 3, MOSQ_ERR_PROTOCOL, NULL, -1); + binary_read_helper(payload, 3, MOSQ_ERR_PROTOCOL, NULL, UINT16_MAX); /* 3 byte packet */ memset(payload, 0, sizeof(payload)); @@ -734,7 +734,7 @@ static void TEST_binary_data_read_truncated(void) payload[1] = 0x03; payload[2] = 'a'; payload[3] = 'b'; - binary_read_helper(payload, 4, MOSQ_ERR_PROTOCOL, NULL, -1); + binary_read_helper(payload, 4, MOSQ_ERR_PROTOCOL, NULL, UINT16_MAX); } @@ -771,10 +771,10 @@ static void TEST_bytes_read_truncated(void) static void TEST_bytes_read_success(void) { uint8_t payload[20]; - int i; + uint8_t i; for(i=0; i<20; i++){ - payload[i] = i*2; + payload[i] = (uint8_t)(i*2); } bytes_read_helper(payload, 20, MOSQ_ERR_SUCCESS, payload, 20); } diff --git a/test/unit/datatype_write.c b/test/unit/datatype_write.c index 9fb9a5b0..bb5483eb 100644 --- a/test/unit/datatype_write.c +++ b/test/unit/datatype_write.c @@ -22,12 +22,12 @@ static void TEST_byte_write(void) packet.packet_length = 256; for(i=0; i<256; i++){ - packet__write_byte(&packet, 255-i); + packet__write_byte(&packet, (uint8_t)(255-i)); } CU_ASSERT_EQUAL(packet.pos, 256); for(i=0; i<256; i++){ - CU_ASSERT_EQUAL(payload[i], 255-i); + CU_ASSERT_EQUAL(payload[i], (uint8_t)(255-i)); } } @@ -50,13 +50,13 @@ static void TEST_uint16_write(void) packet.packet_length = 650; for(i=0; i<325; i++){ - packet__write_uint16(&packet, 100*i); + packet__write_uint16(&packet, (uint16_t)(100*i)); } CU_ASSERT_EQUAL(packet.pos, 650); payload16 = (uint16_t *)payload; for(i=0; i<325; i++){ - CU_ASSERT_EQUAL(payload16[i], htons(100*i)); + CU_ASSERT_EQUAL(payload16[i], htons((uint16_t)(100*i))); } } @@ -83,13 +83,13 @@ static void TEST_uint32_write(void) packet.packet_length = 42000; for(i=0; i<10500; i++){ - packet__write_uint32(&packet, 1000*i); + packet__write_uint32(&packet, (uint32_t)(1000*i)); } CU_ASSERT_EQUAL(packet.pos, 42000); payload32 = (uint32_t *)payload; for(i=0; i<10500; i++){ - CU_ASSERT_EQUAL(payload32[i], htonl(1000*i)); + CU_ASSERT_EQUAL(payload32[i], htonl((uint32_t)(1000*i))); } free(payload); } diff --git a/test/unit/persist_read_stubs.c b/test/unit/persist_read_stubs.c index 93b93102..033f2246 100644 --- a/test/unit/persist_read_stubs.c +++ b/test/unit/persist_read_stubs.c @@ -95,7 +95,7 @@ error: return rc; } -int log__printf(struct mosquitto *mosq, int priority, const char *fmt, ...) +int log__printf(struct mosquitto *mosq, unsigned int priority, const char *fmt, ...) { return 0; } @@ -115,7 +115,7 @@ int send__pingreq(struct mosquitto *mosq) return MOSQ_ERR_SUCCESS; } -int mosquitto_acl_check(struct mosquitto_db *db, struct mosquitto *context, const char *topic, long payloadlen, void* payload, int qos, bool retain, int access) +int mosquitto_acl_check(struct mosquitto_db *db, struct mosquitto *context, const char *topic, uint32_t payloadlen, void* payload, uint8_t qos, bool retain, int access) { return MOSQ_ERR_SUCCESS; } @@ -126,7 +126,7 @@ int acl__find_acls(struct mosquitto_db *db, struct mosquitto *context) } -int sub__add(struct mosquitto_db *db, struct mosquitto *context, const char *sub, int qos, uint32_t identifier, int options, struct mosquitto__subhier **root) +int sub__add(struct mosquitto_db *db, struct mosquitto *context, const char *sub, uint8_t qos, uint32_t identifier, int options, struct mosquitto__subhier **root) { last_sub = strdup(sub); last_qos = qos; @@ -135,7 +135,7 @@ int sub__add(struct mosquitto_db *db, struct mosquitto *context, const char *sub return MOSQ_ERR_SUCCESS; } -int db__message_insert(struct mosquitto_db *db, struct mosquitto *context, uint16_t mid, enum mosquitto_msg_direction dir, int qos, bool retain, struct mosquitto_msg_store *stored, mosquitto_property *properties, bool update) +int db__message_insert(struct mosquitto_db *db, struct mosquitto *context, uint16_t mid, enum mosquitto_msg_direction dir, uint8_t qos, bool retain, struct mosquitto_msg_store *stored, mosquitto_property *properties, bool update) { return MOSQ_ERR_SUCCESS; } diff --git a/test/unit/persist_read_test.c b/test/unit/persist_read_test.c index 70da533f..6e8528ad 100644 --- a/test/unit/persist_read_test.c +++ b/test/unit/persist_read_test.c @@ -854,7 +854,7 @@ int init_persist_read_tests(void) int main(int argc, char *argv[]) { - int fails; + unsigned int fails; if(CU_initialize_registry() != CUE_SUCCESS){ printf("Error initializing CUnit registry.\n"); diff --git a/test/unit/persist_write_stubs.c b/test/unit/persist_write_stubs.c index 836a1763..bfd878ec 100644 --- a/test/unit/persist_write_stubs.c +++ b/test/unit/persist_write_stubs.c @@ -18,7 +18,7 @@ struct mosquitto *context__init(struct mosquitto_db *db, mosq_sock_t sock) return mosquitto__calloc(1, sizeof(struct mosquitto)); } -int log__printf(struct mosquitto *mosq, int priority, const char *fmt, ...) +int log__printf(struct mosquitto *mosq, unsigned int priority, const char *fmt, ...) { return 0; } @@ -38,7 +38,7 @@ int send__pingreq(struct mosquitto *mosq) return MOSQ_ERR_SUCCESS; } -int mosquitto_acl_check(struct mosquitto_db *db, struct mosquitto *context, const char *topic, long payloadlen, void* payload, int qos, bool retain, int access) +int mosquitto_acl_check(struct mosquitto_db *db, struct mosquitto *context, const char *topic, uint32_t payloadlen, void* payload, uint8_t qos, bool retain, int access) { return MOSQ_ERR_SUCCESS; } @@ -49,7 +49,7 @@ int acl__find_acls(struct mosquitto_db *db, struct mosquitto *context) } -int send__publish(struct mosquitto *mosq, uint16_t mid, const char *topic, uint32_t payloadlen, const void *payload, int qos, bool retain, bool dup, const mosquitto_property *cmsg_props, const mosquitto_property *store_props, uint32_t expiry_interval) +int send__publish(struct mosquitto *mosq, uint16_t mid, const char *topic, uint32_t payloadlen, const void *payload, uint8_t qos, bool retain, bool dup, const mosquitto_property *cmsg_props, const mosquitto_property *store_props, uint32_t expiry_interval) { return MOSQ_ERR_SUCCESS; } diff --git a/test/unit/persist_write_test.c b/test/unit/persist_write_test.c index 0cc45eac..ef16505a 100644 --- a/test/unit/persist_write_test.c +++ b/test/unit/persist_write_test.c @@ -27,7 +27,7 @@ static int file_read(const char *filename, uint8_t **data, size_t *len) if(!fptr) return 1; fseek(fptr, 0, SEEK_END); - *len = ftell(fptr); + *len = (size_t)ftell(fptr); *data = malloc(*len); if(!(*data)){ fclose(fptr); diff --git a/test/unit/property_add.c b/test/unit/property_add.c index 4f9359f9..f4f0f730 100644 --- a/test/unit/property_add.c +++ b/test/unit/property_add.c @@ -578,7 +578,8 @@ static void TEST_check_length(void) { mosquitto_property *proplist = NULL; int rc; - int len, varbytes; + unsigned int len; + int varbytes; int i; len = property__get_remaining_length(proplist); diff --git a/test/unit/property_read.c b/test/unit/property_read.c index e305bf3c..0cb3c0fc 100644 --- a/test/unit/property_read.c +++ b/test/unit/property_read.c @@ -8,9 +8,9 @@ static void byte_prop_read_helper( int command, uint8_t *payload, - int remaining_length, + uint32_t remaining_length, int rc_expected, - int identifier, + uint8_t identifier, uint8_t value_expected) { struct mosquitto__packet packet; @@ -34,7 +34,7 @@ static void byte_prop_read_helper( CU_ASSERT_PTR_EQUAL(properties, NULL); } -static void duplicate_byte_helper(int command, int identifier) +static void duplicate_byte_helper(int command, uint8_t identifier) { uint8_t payload[20]; @@ -48,7 +48,7 @@ static void duplicate_byte_helper(int command, int identifier) byte_prop_read_helper(command, payload, 5, MOSQ_ERR_DUPLICATE_PROPERTY, identifier, 1); } -static void bad_byte_helper(int command, int identifier) +static void bad_byte_helper(int command, uint8_t identifier) { uint8_t payload[20]; @@ -64,9 +64,9 @@ static void bad_byte_helper(int command, int identifier) static void int32_prop_read_helper( int command, uint8_t *payload, - int remaining_length, + uint32_t remaining_length, int rc_expected, - int identifier, + uint8_t identifier, uint32_t value_expected) { struct mosquitto__packet packet; @@ -90,7 +90,7 @@ static void int32_prop_read_helper( CU_ASSERT_PTR_EQUAL(properties, NULL); } -static void duplicate_int32_helper(int command, int identifier) +static void duplicate_int32_helper(int command, uint8_t identifier) { uint8_t payload[20]; @@ -114,9 +114,9 @@ static void duplicate_int32_helper(int command, int identifier) static void int16_prop_read_helper( int command, uint8_t *payload, - int remaining_length, + uint32_t remaining_length, int rc_expected, - int identifier, + uint8_t identifier, uint16_t value_expected) { struct mosquitto__packet packet; @@ -140,7 +140,7 @@ static void int16_prop_read_helper( CU_ASSERT_PTR_EQUAL(properties, NULL); } -static void duplicate_int16_helper(int command, int identifier) +static void duplicate_int16_helper(int command, uint8_t identifier) { uint8_t payload[20]; @@ -159,9 +159,9 @@ static void duplicate_int16_helper(int command, int identifier) static void string_prop_read_helper( int command, uint8_t *payload, - int remaining_length, + uint32_t remaining_length, int rc_expected, - int identifier, + uint8_t identifier, const char *value_expected) { struct mosquitto__packet packet; @@ -186,7 +186,7 @@ static void string_prop_read_helper( CU_ASSERT_PTR_EQUAL(properties, NULL); } -static void duplicate_string_helper(int command, int identifier) +static void duplicate_string_helper(int command, uint8_t identifier) { uint8_t payload[20]; @@ -204,7 +204,7 @@ static void duplicate_string_helper(int command, int identifier) string_prop_read_helper(command, payload, 9, MOSQ_ERR_DUPLICATE_PROPERTY, identifier, ""); } -static void bad_string_helper(int identifier) +static void bad_string_helper(uint8_t identifier) { uint8_t payload[20]; @@ -223,9 +223,9 @@ static void bad_string_helper(int identifier) static void binary_prop_read_helper( int command, uint8_t *payload, - int remaining_length, + uint32_t remaining_length, int rc_expected, - int identifier, + uint8_t identifier, const uint8_t *value_expected, int len_expected) { @@ -243,7 +243,7 @@ static void binary_prop_read_helper( if(properties){ CU_ASSERT_EQUAL(properties->identifier, identifier); CU_ASSERT_EQUAL(properties->value.bin.len, len_expected); - CU_ASSERT_EQUAL(memcmp(properties->value.bin.v, value_expected, len_expected), 0); + CU_ASSERT_EQUAL(memcmp(properties->value.bin.v, value_expected, (size_t)len_expected), 0); CU_ASSERT_PTR_EQUAL(properties->next, NULL); CU_ASSERT_EQUAL(property__get_length_all(properties), 1+2+len_expected); mosquitto_property_free_all(&properties); @@ -251,7 +251,7 @@ static void binary_prop_read_helper( CU_ASSERT_PTR_EQUAL(properties, NULL); } -static void duplicate_binary_helper(int command, int identifier) +static void duplicate_binary_helper(int command, uint8_t identifier) { uint8_t payload[20]; @@ -271,9 +271,9 @@ static void duplicate_binary_helper(int command, int identifier) static void string_pair_prop_read_helper( uint8_t *payload, - int remaining_length, + uint32_t remaining_length, int rc_expected, - int identifier, + uint8_t identifier, const char *name_expected, const char *value_expected, bool expect_multiple) @@ -308,9 +308,9 @@ static void string_pair_prop_read_helper( static void varint_prop_read_helper( uint8_t *payload, - int remaining_length, + uint32_t remaining_length, int rc_expected, - int identifier, + uint8_t identifier, uint32_t value_expected) { struct mosquitto__packet packet; diff --git a/test/unit/property_user_read.c b/test/unit/property_user_read.c index 902296bc..61703427 100644 --- a/test/unit/property_user_read.c +++ b/test/unit/property_user_read.c @@ -190,7 +190,7 @@ static void read_varint_helper(const mosquitto_property *proplist, int identifie CU_ASSERT_EQUAL(value, expected_value); } -static void read_binary_helper(const mosquitto_property *proplist, int identifier, void *expected_value, int expected_length) +static void read_binary_helper(const mosquitto_property *proplist, int identifier, void *expected_value, uint16_t expected_length) { const mosquitto_property *prop; void *value = NULL; diff --git a/test/unit/property_write.c b/test/unit/property_write.c index 2618fee7..bbda06d4 100644 --- a/test/unit/property_write.c +++ b/test/unit/property_write.c @@ -7,7 +7,7 @@ static void byte_prop_write_helper( int command, - int remaining_length, + uint32_t remaining_length, int rc_expected, int identifier, uint8_t value_expected) @@ -51,7 +51,7 @@ static void byte_prop_write_helper( static void int32_prop_write_helper( int command, - int remaining_length, + uint32_t remaining_length, int rc_expected, int identifier, uint32_t value_expected) @@ -95,7 +95,7 @@ static void int32_prop_write_helper( static void int16_prop_write_helper( int command, - int remaining_length, + uint32_t remaining_length, int rc_expected, int identifier, uint16_t value_expected) @@ -138,7 +138,7 @@ static void int16_prop_write_helper( static void string_prop_write_helper( int command, - int remaining_length, + uint32_t remaining_length, int rc_expected, int identifier, const char *value_expected) @@ -155,7 +155,7 @@ static void string_prop_write_helper( CU_ASSERT_PTR_NOT_NULL(property.value.s.v); if(!property.value.s.v) return; - property.value.s.len = strlen(value_expected); + property.value.s.len = (uint16_t)strlen(value_expected); memset(&packet, 0, sizeof(struct mosquitto__packet)); packet.remaining_length = property__get_length_all(&property)+1; @@ -188,11 +188,11 @@ static void string_prop_write_helper( static void binary_prop_write_helper( int command, - int remaining_length, + uint32_t remaining_length, int rc_expected, int identifier, const uint8_t *value_expected, - int len_expected) + uint16_t len_expected) { mosquitto_property property; struct mosquitto__packet packet; @@ -238,7 +238,7 @@ static void binary_prop_write_helper( } static void string_pair_prop_write_helper( - int remaining_length, + uint32_t remaining_length, int rc_expected, int identifier, const char *name_expected, @@ -256,13 +256,13 @@ static void string_pair_prop_write_helper( property.value.s.v = strdup(value_expected); CU_ASSERT_PTR_NOT_NULL(property.value.s.v); if(!property.value.s.v) return; - property.value.s.len = strlen(value_expected); + property.value.s.len = (uint16_t)strlen(value_expected); property.name.v = strdup(name_expected); CU_ASSERT_PTR_NOT_NULL(property.name.v); if(!property.name.v) return; - property.name.len = strlen(name_expected); + property.name.len = (uint16_t)strlen(name_expected); memset(&packet, 0, sizeof(struct mosquitto__packet)); packet.remaining_length = property__get_length_all(&property)+1; @@ -300,7 +300,7 @@ static void string_pair_prop_write_helper( } static void varint_prop_write_helper( - int remaining_length, + uint32_t remaining_length, int rc_expected, int identifier, uint32_t value_expected) diff --git a/test/unit/stubs.c b/test/unit/stubs.c index 094a9ec3..897cd77b 100644 --- a/test/unit/stubs.c +++ b/test/unit/stubs.c @@ -5,7 +5,7 @@ struct mosquitto_db{ }; -int log__printf(struct mosquitto *mosq, int priority, const char *fmt, ...) +int log__printf(struct mosquitto *mosq, unsigned int priority, const char *fmt, ...) { return 0; } diff --git a/test/unit/subs_stubs.c b/test/unit/subs_stubs.c index a31470c5..76bf4d35 100644 --- a/test/unit/subs_stubs.c +++ b/test/unit/subs_stubs.c @@ -20,7 +20,7 @@ struct mosquitto *context__init(struct mosquitto_db *db, mosq_sock_t sock) } -int db__message_insert(struct mosquitto_db *db, struct mosquitto *context, uint16_t mid, enum mosquitto_msg_direction dir, int qos, bool retain, struct mosquitto_msg_store *stored, mosquitto_property *properties) +int db__message_insert(struct mosquitto_db *db, struct mosquitto *context, uint16_t mid, enum mosquitto_msg_direction dir, uint8_t qos, bool retain, struct mosquitto_msg_store *stored, mosquitto_property *properties) { return MOSQ_ERR_SUCCESS; } @@ -35,7 +35,7 @@ void db__msg_store_ref_inc(struct mosquitto_msg_store *store) } #endif -int log__printf(struct mosquitto *mosq, int priority, const char *fmt, ...) +int log__printf(struct mosquitto *mosq, unsigned int priority, const char *fmt, ...) { return 0; } @@ -56,7 +56,7 @@ int send__pingreq(struct mosquitto *mosq) return MOSQ_ERR_SUCCESS; } -int mosquitto_acl_check(struct mosquitto_db *db, struct mosquitto *context, const char *topic, long payloadlen, void* payload, int qos, bool retain, int access) +int mosquitto_acl_check(struct mosquitto_db *db, struct mosquitto *context, const char *topic, uint32_tn payloadlen, void* payload, uint8_t qos, bool retain, int access) { return MOSQ_ERR_SUCCESS; } @@ -68,7 +68,7 @@ int acl__find_acls(struct mosquitto_db *db, struct mosquitto *context) #endif -int send__publish(struct mosquitto *mosq, uint16_t mid, const char *topic, uint32_t payloadlen, const void *payload, int qos, bool retain, bool dup, const mosquitto_property *cmsg_props, const mosquitto_property *store_props, uint32_t expiry_interval) +int send__publish(struct mosquitto *mosq, uint16_t mid, const char *topic, uint32_t payloadlen, const void *payload, uint8_t qos, bool retain, bool dup, const mosquitto_property *cmsg_props, const mosquitto_property *store_props, uint32_t expiry_interval) { return MOSQ_ERR_SUCCESS; } @@ -88,7 +88,7 @@ int send__pubrel(struct mosquitto *mosq, uint16_t mid, const mosquitto_property return MOSQ_ERR_SUCCESS; } -int mosquitto_acl_check(struct mosquitto_db *db, struct mosquitto *context, const char *topic, long payloadlen, void* payload, int qos, bool retain, int access) +int mosquitto_acl_check(struct mosquitto_db *db, struct mosquitto *context, const char *topic, uint32_t payloadlen, void* payload, uint8_t qos, bool retain, int access) { return MOSQ_ERR_SUCCESS; } @@ -128,7 +128,7 @@ void retain__clean(struct mosquitto_db *db, struct mosquitto__retainhier **retai { } -int retain__queue(struct mosquitto_db *db, struct mosquitto *context, const char *sub, int sub_qos, uint32_t subscription_identifier) +int retain__queue(struct mosquitto_db *db, struct mosquitto *context, const char *sub, uint8_t sub_qos, uint32_t subscription_identifier) { return MOSQ_ERR_SUCCESS; } diff --git a/test/unit/utf8.c b/test/unit/utf8.c index 1982da87..a05ac9c4 100644 --- a/test/unit/utf8.c +++ b/test/unit/utf8.c @@ -17,7 +17,7 @@ static void utf8_helper_len(const char *text, int len, int expected) static void utf8_helper(const char *text, int expected) { - utf8_helper_len(text, strlen(text), expected); + utf8_helper_len(text, (int)strlen(text), expected); } @@ -37,17 +37,17 @@ static void TEST_utf8_valid(void) static void TEST_utf8_truncated(void) { - char buf[4]; + uint8_t buf[4]; /* As per boundary condition tests, but less one character */ buf[0] = 0xC2; buf[1] = 0; - utf8_helper(buf, MOSQ_ERR_MALFORMED_UTF8); + utf8_helper((char *)buf, MOSQ_ERR_MALFORMED_UTF8); buf[0] = 0xE0; buf[1] = 0xA0; buf[2] = 0; - utf8_helper(buf, MOSQ_ERR_MALFORMED_UTF8); + utf8_helper((char *)buf, MOSQ_ERR_MALFORMED_UTF8); buf[0] = 0xF0; buf[1] = 0x90; buf[2] = 0x80; buf[3] = 0; - utf8_helper(buf, MOSQ_ERR_MALFORMED_UTF8); + utf8_helper((char *)buf, MOSQ_ERR_MALFORMED_UTF8); } @@ -67,7 +67,7 @@ static void TEST_utf8_boundary_conditions(void) /* Non character */ utf8_helper("2.2.3 3 bytes (U-0000FFFF): \"￿\"", MOSQ_ERR_MALFORMED_UTF8); /* Non character */ - utf8_helper("2.2.4 4 bytes (U-0010FFFF): \"\"", MOSQ_ERR_MALFORMED_UTF8); + utf8_helper("2.2.4 4 bytes (U-0010FFFF): \"����\"", MOSQ_ERR_MALFORMED_UTF8); /* 2.3 Other boundary conditions */ @@ -77,101 +77,101 @@ static void TEST_utf8_boundary_conditions(void) /* Non character */ utf8_helper("2.3.4 U-0010FFFF = f4 8f bf bf = \"􏿿\"", MOSQ_ERR_MALFORMED_UTF8); /* This used to be valid in pre-2003 utf-8 */ - utf8_helper("2.3.5 U-00110000 = f4 90 80 80 = \"\"", MOSQ_ERR_MALFORMED_UTF8); + utf8_helper("2.3.5 U-00110000 = f4 90 80 80 = \"����\"", MOSQ_ERR_MALFORMED_UTF8); } static void TEST_utf8_malformed_sequences(void) { - char buf[100]; + uint8_t buf[100]; int i; /* 3 Malformed sequences */ /* 3.1 Unexpected continuation bytes */ - utf8_helper("3.1.1 First continuation byte 0x80: \"\"", MOSQ_ERR_MALFORMED_UTF8); - utf8_helper("3.1.2 Last continuation byte 0xbf: \"\"", MOSQ_ERR_MALFORMED_UTF8); - utf8_helper("3.1.3 2 continuation bytes: \"\"", MOSQ_ERR_MALFORMED_UTF8); - utf8_helper("3.1.4 3 continuation bytes: \"\"", MOSQ_ERR_MALFORMED_UTF8); - utf8_helper("3.1.5 4 continuation bytes: \"\"", MOSQ_ERR_MALFORMED_UTF8); - utf8_helper("3.1.6 5 continuation bytes: \"\"", MOSQ_ERR_MALFORMED_UTF8); - utf8_helper("3.1.7 6 continuation bytes: \"\"", MOSQ_ERR_MALFORMED_UTF8); - utf8_helper("3.1.8 7 continuation bytes: \"\"", MOSQ_ERR_MALFORMED_UTF8); + utf8_helper("3.1.1 First continuation byte 0x80: \"�\"", MOSQ_ERR_MALFORMED_UTF8); + utf8_helper("3.1.2 Last continuation byte 0xbf: \"�\"", MOSQ_ERR_MALFORMED_UTF8); + utf8_helper("3.1.3 2 continuation bytes: \"��\"", MOSQ_ERR_MALFORMED_UTF8); + utf8_helper("3.1.4 3 continuation bytes: \"���\"", MOSQ_ERR_MALFORMED_UTF8); + utf8_helper("3.1.5 4 continuation bytes: \"����\"", MOSQ_ERR_MALFORMED_UTF8); + utf8_helper("3.1.6 5 continuation bytes: \"�����\"", MOSQ_ERR_MALFORMED_UTF8); + utf8_helper("3.1.7 6 continuation bytes: \"������\"", MOSQ_ERR_MALFORMED_UTF8); + utf8_helper("3.1.8 7 continuation bytes: \"�������\"", MOSQ_ERR_MALFORMED_UTF8); /* 3.1.9 Sequence of all 64 possible continuation bytes (0x80-0xbf): */ memset(buf, 0, sizeof(buf)); for(i=0x80; i<0x90; i++){ - buf[i-0x80] = i; + buf[i-0x80] = (uint8_t)i; } - utf8_helper(buf, MOSQ_ERR_MALFORMED_UTF8); + utf8_helper((char *)buf, MOSQ_ERR_MALFORMED_UTF8); memset(buf, 0, sizeof(buf)); for(i=0x90; i<0xa0; i++){ - buf[i-0x90] = i; + buf[i-0x90] = (uint8_t)i; } - utf8_helper(buf, MOSQ_ERR_MALFORMED_UTF8); + utf8_helper((char *)buf, MOSQ_ERR_MALFORMED_UTF8); for(i=0x80; i<0xA0; i++){ - buf[0] = i; + buf[0] = (uint8_t)i; buf[1] = 0; - utf8_helper(buf, MOSQ_ERR_MALFORMED_UTF8); + utf8_helper((char *)buf, MOSQ_ERR_MALFORMED_UTF8); } for(i=0xA0; i<0xC0; i++){ - buf[0] = i; + buf[0] = (uint8_t)i; buf[1] = 0; - utf8_helper(buf, MOSQ_ERR_MALFORMED_UTF8); + utf8_helper((char *)buf, MOSQ_ERR_MALFORMED_UTF8); } /* 3.2 Lonely start characters */ /* 3.2.1 All 32 first bytes of 2-byte sequences (0xc0-0xdf), each followed by a space character: */ - utf8_helper(" ", MOSQ_ERR_MALFORMED_UTF8); + utf8_helper("� � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � ", MOSQ_ERR_MALFORMED_UTF8); for(i=0xC0; i<0xE0; i++){ - buf[0] = i; + buf[0] = (uint8_t)i; buf[1] = ' '; buf[2] = 0; - utf8_helper(buf, MOSQ_ERR_MALFORMED_UTF8); + utf8_helper((char *)buf, MOSQ_ERR_MALFORMED_UTF8); } /* 3.2.2 All 16 first bytes of 3-byte sequences (0xe0-0xef), each followed by a space character: */ - utf8_helper("\" \"", MOSQ_ERR_MALFORMED_UTF8); + utf8_helper("\"� � � � � � � � � � � � � � � � \"", MOSQ_ERR_MALFORMED_UTF8); for(i=0xe0; i<0xf0; i++){ - buf[0] = i; + buf[0] = (uint8_t)i; buf[1] = ' '; buf[2] = 0; - utf8_helper(buf, MOSQ_ERR_MALFORMED_UTF8); + utf8_helper((char *)buf, MOSQ_ERR_MALFORMED_UTF8); } /* 3.2.3 All 8 first bytes of 4-byte sequences (0xf0-0xf7), each followed by a space character: */ - utf8_helper("\" \"", MOSQ_ERR_MALFORMED_UTF8); + utf8_helper("\"� � � � � � � � \"", MOSQ_ERR_MALFORMED_UTF8); for(i=0xF0; i<0xF8; i++){ - buf[0] = i; + buf[0] = (uint8_t)i; buf[1] = ' '; buf[2] = 0; - utf8_helper(buf, MOSQ_ERR_MALFORMED_UTF8); + utf8_helper((char *)buf, MOSQ_ERR_MALFORMED_UTF8); } /* 3.2.4 All 4 first bytes of 5-byte sequences (0xf8-0xfb), each followed by a space character: */ - utf8_helper("\" \"", MOSQ_ERR_MALFORMED_UTF8); + utf8_helper("\"� � � � \"", MOSQ_ERR_MALFORMED_UTF8); for(i=0xF8; i<0xFC; i++){ - buf[0] = i; + buf[0] = (uint8_t)i; buf[1] = ' '; buf[2] = 0; - utf8_helper(buf, MOSQ_ERR_MALFORMED_UTF8); + utf8_helper((char *)buf, MOSQ_ERR_MALFORMED_UTF8); } /* 3.2.5 All 2 first bytes of 6-byte sequences (0xfc-0xfd), each followed by a space character: */ - utf8_helper("\" \"", MOSQ_ERR_MALFORMED_UTF8); - utf8_helper(" ", MOSQ_ERR_MALFORMED_UTF8); - utf8_helper(" ", MOSQ_ERR_MALFORMED_UTF8); + utf8_helper("\"� � \"", MOSQ_ERR_MALFORMED_UTF8); + utf8_helper("� ", MOSQ_ERR_MALFORMED_UTF8); + utf8_helper("� ", MOSQ_ERR_MALFORMED_UTF8); for(i=0xFC; i<0xFE; i++){ - buf[0] = i; + buf[0] = (uint8_t)i; buf[1] = ' '; buf[2] = 0; - utf8_helper(buf, MOSQ_ERR_MALFORMED_UTF8); + utf8_helper((char *)buf, MOSQ_ERR_MALFORMED_UTF8); } /* 3.3 Sequences with last continuation byte missing @@ -180,31 +180,31 @@ static void TEST_utf8_malformed_sequences(void) malformed sequence, i.e., you should see only a single replacement character in each of the next 10 tests. (Characters as in section 2) */ - utf8_helper("3.3.1 2-byte sequence with last byte missing (U+0000): \"\"", MOSQ_ERR_MALFORMED_UTF8); - utf8_helper("3.3.2 3-byte sequence with last byte missing (U+0000): \"\"", MOSQ_ERR_MALFORMED_UTF8); - utf8_helper("3.3.3 4-byte sequence with last byte missing (U+0000): \"\"", MOSQ_ERR_MALFORMED_UTF8); - utf8_helper("3.3.4 5-byte sequence with last byte missing (U+0000): \"\"", MOSQ_ERR_MALFORMED_UTF8); - utf8_helper("3.3.5 6-byte sequence with last byte missing (U+0000): \"\"", MOSQ_ERR_MALFORMED_UTF8); - utf8_helper("3.3.6 2-byte sequence with last byte missing (U-000007FF): \"\"", MOSQ_ERR_MALFORMED_UTF8); - utf8_helper("3.3.7 3-byte sequence with last byte missing (U-0000FFFF): \"\"", MOSQ_ERR_MALFORMED_UTF8); - utf8_helper("3.3.8 4-byte sequence with last byte missing (U-001FFFFF): \"\"", MOSQ_ERR_MALFORMED_UTF8); - utf8_helper("3.3.9 5-byte sequence with last byte missing (U-03FFFFFF): \"\"", MOSQ_ERR_MALFORMED_UTF8); - utf8_helper("3.3.10 6-byte sequence with last byte missing (U-7FFFFFFF): \"\"", MOSQ_ERR_MALFORMED_UTF8); + utf8_helper("3.3.1 2-byte sequence with last byte missing (U+0000): \"�\"", MOSQ_ERR_MALFORMED_UTF8); + utf8_helper("3.3.2 3-byte sequence with last byte missing (U+0000): \"��\"", MOSQ_ERR_MALFORMED_UTF8); + utf8_helper("3.3.3 4-byte sequence with last byte missing (U+0000): \"���\"", MOSQ_ERR_MALFORMED_UTF8); + utf8_helper("3.3.4 5-byte sequence with last byte missing (U+0000): \"����\"", MOSQ_ERR_MALFORMED_UTF8); + utf8_helper("3.3.5 6-byte sequence with last byte missing (U+0000): \"�����\"", MOSQ_ERR_MALFORMED_UTF8); + utf8_helper("3.3.6 2-byte sequence with last byte missing (U-000007FF): \"�\"", MOSQ_ERR_MALFORMED_UTF8); + utf8_helper("3.3.7 3-byte sequence with last byte missing (U-0000FFFF): \"�\"", MOSQ_ERR_MALFORMED_UTF8); + utf8_helper("3.3.8 4-byte sequence with last byte missing (U-001FFFFF): \"���\"", MOSQ_ERR_MALFORMED_UTF8); + utf8_helper("3.3.9 5-byte sequence with last byte missing (U-03FFFFFF): \"����\"", MOSQ_ERR_MALFORMED_UTF8); + utf8_helper("3.3.10 6-byte sequence with last byte missing (U-7FFFFFFF): \"�����\"", MOSQ_ERR_MALFORMED_UTF8); /* 3.4 Concatenation of incomplete sequences All the 10 sequences of 3.3 concatenated, you should see 10 malformed sequences being signalled:*/ - utf8_helper("\"\"", MOSQ_ERR_MALFORMED_UTF8); + utf8_helper("\"�����������������������������\"", MOSQ_ERR_MALFORMED_UTF8); /* 3.5 Impossible bytes The following two bytes cannot appear in a correct UTF-8 string */ - utf8_helper("3.5.1 fe = \"\"", MOSQ_ERR_MALFORMED_UTF8); - utf8_helper("3.5.2 ff = \"\"", MOSQ_ERR_MALFORMED_UTF8); - utf8_helper("3.5.3 fe fe ff ff = \"\"", MOSQ_ERR_MALFORMED_UTF8); + utf8_helper("3.5.1 fe = \"�\"", MOSQ_ERR_MALFORMED_UTF8); + utf8_helper("3.5.2 ff = \"�\"", MOSQ_ERR_MALFORMED_UTF8); + utf8_helper("3.5.3 fe fe ff ff = \"����\"", MOSQ_ERR_MALFORMED_UTF8); } static void TEST_utf8_overlong_encoding(void) @@ -237,11 +237,11 @@ static void TEST_utf8_overlong_encoding(void) a replacement character. If you see a slash below, you do not have a safe UTF-8 decoder! */ - utf8_helper("4.1.1 U+002F = c0 af = \"\"", MOSQ_ERR_MALFORMED_UTF8); - utf8_helper("4.1.2 U+002F = e0 80 af = \"\"", MOSQ_ERR_MALFORMED_UTF8); - utf8_helper("4.1.3 U+002F = f0 80 80 af = \"\"", MOSQ_ERR_MALFORMED_UTF8); - utf8_helper("4.1.4 U+002F = f8 80 80 80 af = \"\"", MOSQ_ERR_MALFORMED_UTF8); - utf8_helper("4.1.5 U+002F = fc 80 80 80 80 af = \"\"", MOSQ_ERR_MALFORMED_UTF8); + utf8_helper("4.1.1 U+002F = c0 af = \"��\"", MOSQ_ERR_MALFORMED_UTF8); + utf8_helper("4.1.2 U+002F = e0 80 af = \"���\"", MOSQ_ERR_MALFORMED_UTF8); + utf8_helper("4.1.3 U+002F = f0 80 80 af = \"����\"", MOSQ_ERR_MALFORMED_UTF8); + utf8_helper("4.1.4 U+002F = f8 80 80 80 af = \"�����\"", MOSQ_ERR_MALFORMED_UTF8); + utf8_helper("4.1.5 U+002F = fc 80 80 80 80 af = \"������\"", MOSQ_ERR_MALFORMED_UTF8); /* 4.2 Maximum overlong sequences @@ -250,11 +250,11 @@ static void TEST_utf8_overlong_encoding(void) is a boundary test for safe UTF-8 decoders. All five characters should be rejected like malformed UTF-8 sequences. */ - utf8_helper("4.2.1 U-0000007F = c1 bf = \"\"", MOSQ_ERR_MALFORMED_UTF8); - utf8_helper("4.2.2 U-000007FF = e0 9f bf = \"\"", MOSQ_ERR_MALFORMED_UTF8); - utf8_helper("4.2.3 U-0000FFFF = f0 8f bf bf = \"\"", MOSQ_ERR_MALFORMED_UTF8); - utf8_helper("4.2.4 U-001FFFFF = f8 87 bf bf bf = \"\"", MOSQ_ERR_MALFORMED_UTF8); - utf8_helper("4.2.5 U-03FFFFFF = fc 83 bf bf bf bf = \"\"", MOSQ_ERR_MALFORMED_UTF8); + utf8_helper("4.2.1 U-0000007F = c1 bf = \"��\"", MOSQ_ERR_MALFORMED_UTF8); + utf8_helper("4.2.2 U-000007FF = e0 9f bf = \"���\"", MOSQ_ERR_MALFORMED_UTF8); + utf8_helper("4.2.3 U-0000FFFF = f0 8f bf bf = \"����\"", MOSQ_ERR_MALFORMED_UTF8); + utf8_helper("4.2.4 U-001FFFFF = f8 87 bf bf bf = \"�����\"", MOSQ_ERR_MALFORMED_UTF8); + utf8_helper("4.2.5 U-03FFFFFF = fc 83 bf bf bf bf = \"������\"", MOSQ_ERR_MALFORMED_UTF8); /* 4.3 Overlong representation of the NUL character @@ -262,11 +262,11 @@ static void TEST_utf8_overlong_encoding(void) UTF-8 sequences and should not be treated like the ASCII NUL character. */ - utf8_helper("4.3.1 U+0000 = c0 80 = \"\"", MOSQ_ERR_MALFORMED_UTF8); - utf8_helper("4.3.2 U+0000 = e0 80 80 = \"\"", MOSQ_ERR_MALFORMED_UTF8); - utf8_helper("4.3.3 U+0000 = f0 80 80 80 = \"\"", MOSQ_ERR_MALFORMED_UTF8); - utf8_helper("4.3.4 U+0000 = f8 80 80 80 80 = \"\"", MOSQ_ERR_MALFORMED_UTF8); - utf8_helper("4.3.5 U+0000 = fc 80 80 80 80 80 = \"\"", MOSQ_ERR_MALFORMED_UTF8); + utf8_helper("4.3.1 U+0000 = c0 80 = \"��\"", MOSQ_ERR_MALFORMED_UTF8); + utf8_helper("4.3.2 U+0000 = e0 80 80 = \"���\"", MOSQ_ERR_MALFORMED_UTF8); + utf8_helper("4.3.3 U+0000 = f0 80 80 80 = \"����\"", MOSQ_ERR_MALFORMED_UTF8); + utf8_helper("4.3.4 U+0000 = f8 80 80 80 80 = \"�����\"", MOSQ_ERR_MALFORMED_UTF8); + utf8_helper("4.3.5 U+0000 = fc 80 80 80 80 80 = \"������\"", MOSQ_ERR_MALFORMED_UTF8); } @@ -281,24 +281,24 @@ static void TEST_utf8_illegal_code_positions(void) /* 5.1 Single UTF-16 surrogates */ - utf8_helper("5.1.1 U+D800 = ed a0 80 = \"\"", MOSQ_ERR_MALFORMED_UTF8); - utf8_helper("5.1.2 U+DB7F = ed ad bf = \"\"", MOSQ_ERR_MALFORMED_UTF8); - utf8_helper("5.1.3 U+DB80 = ed ae 80 = \"\"", MOSQ_ERR_MALFORMED_UTF8); - utf8_helper("5.1.4 U+DBFF = ed af bf = \"\"", MOSQ_ERR_MALFORMED_UTF8); - utf8_helper("5.1.5 U+DC00 = ed b0 80 = \"\"", MOSQ_ERR_MALFORMED_UTF8); - utf8_helper("5.1.6 U+DF80 = ed be 80 = \"\"", MOSQ_ERR_MALFORMED_UTF8); - utf8_helper("5.1.7 U+DFFF = ed bf bf = \"\"", MOSQ_ERR_MALFORMED_UTF8); + utf8_helper("5.1.1 U+D800 = ed a0 80 = \"���\"", MOSQ_ERR_MALFORMED_UTF8); + utf8_helper("5.1.2 U+DB7F = ed ad bf = \"���\"", MOSQ_ERR_MALFORMED_UTF8); + utf8_helper("5.1.3 U+DB80 = ed ae 80 = \"���\"", MOSQ_ERR_MALFORMED_UTF8); + utf8_helper("5.1.4 U+DBFF = ed af bf = \"���\"", MOSQ_ERR_MALFORMED_UTF8); + utf8_helper("5.1.5 U+DC00 = ed b0 80 = \"���\"", MOSQ_ERR_MALFORMED_UTF8); + utf8_helper("5.1.6 U+DF80 = ed be 80 = \"���\"", MOSQ_ERR_MALFORMED_UTF8); + utf8_helper("5.1.7 U+DFFF = ed bf bf = \"���\"", MOSQ_ERR_MALFORMED_UTF8); /* 5.2 Paired UTF-16 surrogates */ - utf8_helper("5.2.1 U+D800 U+DC00 = ed a0 80 ed b0 80 = \"\"", MOSQ_ERR_MALFORMED_UTF8); - utf8_helper("5.2.2 U+D800 U+DFFF = ed a0 80 ed bf bf = \"\"", MOSQ_ERR_MALFORMED_UTF8); - utf8_helper("5.2.3 U+DB7F U+DC00 = ed ad bf ed b0 80 = \"\"", MOSQ_ERR_MALFORMED_UTF8); - utf8_helper("5.2.4 U+DB7F U+DFFF = ed ad bf ed bf bf = \"\"", MOSQ_ERR_MALFORMED_UTF8); - utf8_helper("5.2.5 U+DB80 U+DC00 = ed ae 80 ed b0 80 = \"\"", MOSQ_ERR_MALFORMED_UTF8); - utf8_helper("5.2.6 U+DB80 U+DFFF = ed ae 80 ed bf bf = \"\"", MOSQ_ERR_MALFORMED_UTF8); - utf8_helper("5.2.7 U+DBFF U+DC00 = ed af bf ed b0 80 = \"\"", MOSQ_ERR_MALFORMED_UTF8); - utf8_helper("5.2.8 U+DBFF U+DFFF = ed af bf ed bf bf = \"\"", MOSQ_ERR_MALFORMED_UTF8); + utf8_helper("5.2.1 U+D800 U+DC00 = ed a0 80 ed b0 80 = \"������\"", MOSQ_ERR_MALFORMED_UTF8); + utf8_helper("5.2.2 U+D800 U+DFFF = ed a0 80 ed bf bf = \"������\"", MOSQ_ERR_MALFORMED_UTF8); + utf8_helper("5.2.3 U+DB7F U+DC00 = ed ad bf ed b0 80 = \"������\"", MOSQ_ERR_MALFORMED_UTF8); + utf8_helper("5.2.4 U+DB7F U+DFFF = ed ad bf ed bf bf = \"������\"", MOSQ_ERR_MALFORMED_UTF8); + utf8_helper("5.2.5 U+DB80 U+DC00 = ed ae 80 ed b0 80 = \"������\"", MOSQ_ERR_MALFORMED_UTF8); + utf8_helper("5.2.6 U+DB80 U+DFFF = ed ae 80 ed bf bf = \"������\"", MOSQ_ERR_MALFORMED_UTF8); + utf8_helper("5.2.7 U+DBFF U+DC00 = ed af bf ed b0 80 = \"������\"", MOSQ_ERR_MALFORMED_UTF8); + utf8_helper("5.2.8 U+DBFF U+DFFF = ed af bf ed bf bf = \"������\"", MOSQ_ERR_MALFORMED_UTF8); /* 5.3 Noncharacter code positions @@ -400,27 +400,27 @@ static void TEST_utf8_illegal_code_positions(void) void TEST_utf8_control_characters(void) { - char buf[10]; + uint8_t buf[10]; int i; /* U+0001 to U+001F are single byte control characters */ for(i=0x01; i<0x20; i++){ - buf[0] = i; + buf[0] = (uint8_t)i; buf[1] = '\0'; - utf8_helper(buf, MOSQ_ERR_MALFORMED_UTF8); + utf8_helper((char *)buf, MOSQ_ERR_MALFORMED_UTF8); } /* U+007F is a single byte control character */ buf[0] = 0x7F; buf[1] = '\0'; - utf8_helper(buf, MOSQ_ERR_MALFORMED_UTF8); + utf8_helper((char *)buf, MOSQ_ERR_MALFORMED_UTF8); /* U+007F to U+009F are two byte control characters */ for(i=0x80; i<0xA0; i++){ buf[0] = 0xC2; - buf[1] = i-0x80; + buf[1] = (uint8_t)(i-0x80); buf[2] = '\0'; - utf8_helper(buf, MOSQ_ERR_MALFORMED_UTF8); + utf8_helper((char *)buf, MOSQ_ERR_MALFORMED_UTF8); } } @@ -428,20 +428,20 @@ void TEST_utf8_control_characters(void) void TEST_utf8_mqtt_1_5_4_2(void) { - char buf[10] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', '\0'}; + uint8_t buf[10] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', '\0'}; - utf8_helper_len(buf, 9, MOSQ_ERR_SUCCESS); + utf8_helper_len((char *)buf, 9, MOSQ_ERR_SUCCESS); buf[3] = '\0'; - utf8_helper_len(buf, 9, MOSQ_ERR_MALFORMED_UTF8); + utf8_helper_len((char *)buf, 9, MOSQ_ERR_MALFORMED_UTF8); } void TEST_utf8_mqtt_1_5_4_3(void) { - char buf[10] = {'a', 'b', 0xEF, 0xBB, 0xBF, 'f', 'g', 'h', 'i', '\0'}; + uint8_t buf[10] = {'a', 'b', 0xEF, 0xBB, 0xBF, 'f', 'g', 'h', 'i', '\0'}; - utf8_helper_len(buf, 9, MOSQ_ERR_SUCCESS); + utf8_helper_len((char *)buf, 9, MOSQ_ERR_SUCCESS); }