Formatting: Forbid single line if statements

These hide cases from coverage checks
This commit is contained in:
Roger A. Light
2025-09-17 12:48:02 +01:00
parent 48b76e49f8
commit 7ea73f6d60
203 changed files with 3126 additions and 1044 deletions
+57 -19
View File
@@ -126,16 +126,26 @@ static int dump__cfg_chunk_process(FILE *db_fd, uint32_t length)
return rc;
}
if(do_print) printf("DB_CHUNK_CFG:\n");
if(do_print) printf("\tLength: %d\n", length);
if(do_print) printf("\tShutdown: %d\n", chunk.shutdown);
if(do_print) printf("\tDB ID size: %d\n", chunk.dbid_size);
if(do_print){
printf("DB_CHUNK_CFG:\n");
}
if(do_print){
printf("\tLength: %d\n", length);
}
if(do_print){
printf("\tShutdown: %d\n", chunk.shutdown);
}
if(do_print){
printf("\tDB ID size: %d\n", chunk.dbid_size);
}
if(chunk.dbid_size != sizeof(dbid_t)){
fprintf(stderr, "Error: Incompatible database configuration (dbid size is %d bytes, expected %zu)",
chunk.dbid_size, sizeof(dbid_t));
return MOSQ_ERR_INVAL;
}
if(do_print) printf("\tLast DB ID: %" PRIu64 "\n", chunk.last_db_id);
if(do_print){
printf("\tLast DB ID: %" PRIu64 "\n", chunk.last_db_id);
}
return 0;
}
@@ -327,8 +337,12 @@ static int dump__retain_chunk_process(FILE *db_fd, uint32_t length)
int rc;
retain_count++;
if(do_print) printf("DB_CHUNK_RETAIN:\n");
if(do_print) printf("\tLength: %d\n", length);
if(do_print){
printf("DB_CHUNK_RETAIN:\n");
}
if(do_print){
printf("\tLength: %d\n", length);
}
if(db_version == 6 || db_version == 5){
rc = persist__chunk_retain_read_v56(db_fd, &chunk);
@@ -344,7 +358,9 @@ static int dump__retain_chunk_process(FILE *db_fd, uint32_t length)
json_add_retained_msg(&chunk);
}
if(do_print) printf("\tStore ID: %" PRIu64 "\n", chunk.F.store_id);
if(do_print){
printf("\tStore ID: %" PRIu64 "\n", chunk.F.store_id);
}
return 0;
}
@@ -479,42 +495,62 @@ int main(int argc, char *argv[])
}
read_e(fd, &header, 15);
if(!memcmp(header, magic, 15)){
if(do_print) printf("Mosquitto DB dump\n");
if(do_print){
printf("Mosquitto DB dump\n");
}
/* Restore DB as normal */
read_e(fd, &crc, sizeof(uint32_t));
if(do_print) printf("CRC: %d\n", crc);
if(do_print){
printf("CRC: %d\n", crc);
}
read_e(fd, &i32temp, sizeof(uint32_t));
db_version = ntohl(i32temp);
if(do_print) printf("DB version: %d\n", db_version);
if(do_print){
printf("DB version: %d\n", db_version);
}
if(db_version > MOSQ_DB_VERSION){
if(do_print) printf("Warning: mosquitto_db_dump does not support this DB version, continuing but expecting errors.\n");
if(do_print){
printf("Warning: mosquitto_db_dump does not support this DB version, continuing but expecting errors.\n");
}
}
while(persist__chunk_header_read(fd, &chunk, &length) == MOSQ_ERR_SUCCESS){
switch(chunk){
case DB_CHUNK_CFG:
if(dump__cfg_chunk_process(fd, length)) goto error;
if(dump__cfg_chunk_process(fd, length)){
goto error;
}
break;
case DB_CHUNK_BASE_MSG:
if(dump__base_msg_chunk_process(fd, length)) goto error;
if(dump__base_msg_chunk_process(fd, length)){
goto error;
}
break;
case DB_CHUNK_CLIENT_MSG:
if(dump__client_msg_chunk_process(fd, length)) goto error;
if(dump__client_msg_chunk_process(fd, length)){
goto error;
}
break;
case DB_CHUNK_RETAIN:
if(dump__retain_chunk_process(fd, length)) goto error;
if(dump__retain_chunk_process(fd, length)){
goto error;
}
break;
case DB_CHUNK_SUB:
if(dump__sub_chunk_process(fd, length)) goto error;
if(dump__sub_chunk_process(fd, length)){
goto error;
}
break;
case DB_CHUNK_CLIENT:
if(dump__client_chunk_process(fd, length)) goto error;
if(dump__client_chunk_process(fd, length)){
goto error;
}
break;
default:
@@ -553,6 +589,8 @@ int main(int argc, char *argv[])
return rc;
error:
cleanup_msg_store();
if(fd) fclose(fd);
if(fd){
fclose(fd);
}
return 1;
}
+3 -1
View File
@@ -30,7 +30,9 @@ static void print__properties(mosquitto_property *properties)
{
int i;
if(properties == NULL) return;
if(properties == NULL){
return;
}
printf("\tProperties:\n");
+3 -1
View File
@@ -250,7 +250,9 @@ int broker__main(int argc, char *argv[], struct mosq_ctrl *ctrl)
return MOSQ_ERR_NOMEM;
}
j_tree = cJSON_CreateObject();
if(j_tree == NULL) return MOSQ_ERR_NOMEM;
if(j_tree == NULL){
return MOSQ_ERR_NOMEM;
}
j_commands = cJSON_AddArrayToObject(j_tree, "commands");
if(j_commands == NULL){
cJSON_Delete(j_tree);
+6 -2
View File
@@ -140,7 +140,9 @@ int client_request_response(struct mosq_ctrl *ctrl)
mosq = mosquitto_new(ctrl->cfg.id, true, ctrl);
rc = client_opts_set(mosq, &ctrl->cfg);
if(rc) goto cleanup;
if(rc){
goto cleanup;
}
mosquitto_connect_v5_callback_set(mosq, on_connect);
mosquitto_subscribe_v5_callback_set(mosq, on_subscribe);
@@ -148,7 +150,9 @@ int client_request_response(struct mosq_ctrl *ctrl)
mosquitto_message_v5_callback_set(mosq, on_message);
rc = client_connect(mosq, &ctrl->cfg);
if(rc) goto cleanup;
if(rc){
goto cleanup;
}
start = time(NULL);
while(run && start+10 > time(NULL)){
+6 -2
View File
@@ -345,7 +345,9 @@ bool ctrl_shell_callback_final(char *line)
return false;
}
}else if(!strcasecmp(line, "return")){
if(data.mod_cleanup) data.mod_cleanup();
if(data.mod_cleanup){
data.mod_cleanup();
}
ctrl_shell__post_connect_init();
}else{
return false;
@@ -387,7 +389,9 @@ static void calc_generator_arg(int start)
}
text_heap = strdup(rl_line_buffer);
if(!text_heap) return;
if(!text_heap){
return;
}
text_heap[start] = '\0';
text_arg = strtok_r(text_heap, " ", &saveptr);
while(text_arg){
+3 -1
View File
@@ -36,7 +36,9 @@ static void command_tree_create(void)
struct completion_tree_cmd *cmd;
struct completion_tree_arg_list *help_arg_list;
if(commands_broker) return;
if(commands_broker){
return;
}
commands_broker = calloc(1, sizeof(struct completion_tree_root));
+3 -1
View File
@@ -75,7 +75,9 @@ int ctrl_shell__connect(void)
void ctrl_shell__disconnect(void)
{
if(!data.mosq) return;
if(!data.mosq){
return;
}
mosquitto_disconnect(data.mosq);
mosquitto_loop_stop(data.mosq, false);
@@ -31,7 +31,9 @@ void completion_tree_arg_list_args_free(struct completion_tree_arg_list *arg_lis
{
struct completion_tree_arg *arg, *next;
if(!arg_list) return;
if(!arg_list){
return;
}
arg = arg_list->args;
while(arg){
@@ -45,8 +47,12 @@ void completion_tree_arg_list_args_free(struct completion_tree_arg_list *arg_lis
void completion_tree_arg_list_free(struct completion_tree_arg_list *arg_list)
{
if(!arg_list) return;
if(arg_list->is_shared) return;
if(!arg_list){
return;
}
if(arg_list->is_shared){
return;
}
completion_tree_arg_list_args_free(arg_list);
free(arg_list);
@@ -55,7 +61,9 @@ void completion_tree_arg_list_free(struct completion_tree_arg_list *arg_list)
void completion_tree_cmd_free(struct completion_tree_cmd *cmd)
{
if(!cmd) return;
if(!cmd){
return;
}
for(int i=0; i<cmd->arg_list_count; i++){
completion_tree_arg_list_free(cmd->arg_lists[i]);
@@ -69,7 +77,9 @@ void completion_tree_free(struct completion_tree_root *tree)
{
struct completion_tree_cmd *cmd, *next;
if(!tree) return;
if(!tree){
return;
}
cmd = tree->commands;
while(cmd){
@@ -85,7 +95,9 @@ struct completion_tree_cmd *completion_tree_cmd_add(struct completion_tree_root
struct completion_tree_cmd *new_node;
new_node = calloc(1, sizeof(struct completion_tree_cmd) + strlen(name) + 1);
if(!new_node) return NULL;
if(!new_node){
return NULL;
}
strcpy(new_node->name, name);
@@ -108,7 +120,9 @@ void completion_tree_cmd_append_arg_list(struct completion_tree_cmd *cmd, struct
struct completion_tree_arg_list **arg_list;
arg_list = realloc(cmd->arg_lists, (size_t)(cmd->arg_list_count+1)*sizeof(struct completion_tree_arg_list *));
if(!arg_list) return;
if(!arg_list){
return;
}
cmd->arg_lists = arg_list;
@@ -118,11 +132,15 @@ void completion_tree_cmd_append_arg_list(struct completion_tree_cmd *cmd, struct
struct completion_tree_arg_list *completion_tree_cmd_add_arg_list(struct completion_tree_cmd *cmd)
{
if(!cmd) return NULL;
if(!cmd){
return NULL;
}
struct completion_tree_arg_list *new_list;
new_list = completion_tree_cmd_new_arg_list();
if(!new_list) return NULL;
if(!new_list){
return NULL;
}
completion_tree_cmd_append_arg_list(cmd, new_list);
@@ -132,12 +150,16 @@ struct completion_tree_arg_list *completion_tree_cmd_add_arg_list(struct complet
void completion_tree_arg_list_add_arg(struct completion_tree_arg_list *arg_list, const char *name)
{
if(!arg_list || !name) return;
if(!arg_list || !name){
return;
}
struct completion_tree_arg *new_node;
new_node = calloc(1, sizeof(struct completion_tree_arg) + strlen(name) + 1);
if(!new_node) return;
if(!new_node){
return;
}
strcpy(new_node->name, name);
+3 -1
View File
@@ -46,7 +46,9 @@ static void command_tree_create(void)
completion_tree_arg_list_args_free(tree_groups);
completion_tree_arg_list_args_free(tree_roles);
if(commands_dynsec) return;
if(commands_dynsec){
return;
}
commands_dynsec = calloc(1, sizeof(struct completion_tree_root));
@@ -34,7 +34,9 @@ static void command_tree_create(void)
struct completion_tree_cmd *cmd;
struct completion_tree_arg_list *help_arg_list;
if(commands_post_connect) return;
if(commands_post_connect){
return;
}
commands_post_connect = calloc(1, sizeof(struct completion_tree_root));
+3 -1
View File
@@ -35,7 +35,9 @@ static void command_tree_create(void)
struct completion_tree_cmd *cmd;
struct completion_tree_arg_list *help_arg_list;
if(commands_pre_connect) return;
if(commands_pre_connect){
return;
}
commands_pre_connect = calloc(1, sizeof(struct completion_tree_root));
+9 -3
View File
@@ -520,7 +520,9 @@ static cJSON *init_add_acl_to_role(cJSON *j_acls, const char *type, const char *
cJSON *j_acl;
j_acl = cJSON_CreateObject();
if(j_acl == NULL) return NULL;
if(j_acl == NULL){
return NULL;
}
if(cJSON_AddStringToObject(j_acl, "acltype", type) == NULL
|| cJSON_AddStringToObject(j_acl, "topic", topic) == NULL
@@ -631,7 +633,9 @@ static cJSON *init_create(const char *username, const char *password, const char
cJSON *j_default_access;
tree = cJSON_CreateObject();
if(tree == NULL) return NULL;
if(tree == NULL){
return NULL;
}
if((j_clients = cJSON_AddArrayToObject(tree, "clients")) == NULL
|| (j_roles = cJSON_AddArrayToObject(tree, "roles")) == NULL
@@ -789,7 +793,9 @@ int dynsec__main(int argc, char *argv[], struct mosq_ctrl *ctrl)
return MOSQ_ERR_NOMEM;
}
j_tree = cJSON_CreateObject();
if(j_tree == NULL) return MOSQ_ERR_NOMEM;
if(j_tree == NULL){
return MOSQ_ERR_NOMEM;
}
j_commands = cJSON_AddArrayToObject(j_tree, "commands");
if(j_commands == NULL){
cJSON_Delete(j_tree);
+16 -6
View File
@@ -93,11 +93,15 @@ int ctrl_config_parse(struct mosq_config *cfg, int *argc, char **argv[])
/* Deal with real argc/argv */
rc = client_config_line_proc(cfg, argc, argv);
if(rc) return rc;
if(rc){
return rc;
}
/* Load options from config file - this must be after `-o` has been processed */
rc = client_config_load(cfg);
if(rc) return rc;
if(rc){
return rc;
}
#ifdef WITH_TLS
if((cfg->certfile && !cfg->keyfile) || (cfg->keyfile && !cfg->certfile)){
@@ -559,8 +563,10 @@ int client_config_load(struct mosq_config *cfg)
return 1;
}
while(fgets(line, sizeof(line), fptr)){
if(line[0] == '#') continue; /* Comments */
if(line[0] == '#'){
/* Comments */
continue;
}
while(line[strlen(line)-1] == 10 || line[strlen(line)-1] == 13){
line[strlen(line)-1] = 0;
}
@@ -727,9 +733,13 @@ static int mosquitto__urldecode(char *str)
{
size_t i, j;
size_t len;
if(!str) return 0;
if(!str){
return 0;
}
if(!strchr(str, '%')) return 0;
if(!strchr(str, '%')){
return 0;
}
len = strlen(str);
for(i=0; i<len; i++){
+6 -2
View File
@@ -100,7 +100,9 @@ static FILE *mpw_tmpfile(void)
umask(077);
fd = mkstemp((char *)tmpfile_path);
if(fd < 0) return NULL;
if(fd < 0){
return NULL;
}
unlink((char *)tmpfile_path);
return fdopen(fd, "w+");
@@ -336,7 +338,9 @@ static int copy_contents(FILE *src, FILE *dest)
#ifdef WIN32
_chsize(fileno(dest), 0);
#else
if(ftruncate(fileno(dest), 0)) return 1;
if(ftruncate(fileno(dest), 0)){
return 1;
}
#endif
while(!feof(src)){
+20 -8
View File
@@ -413,8 +413,10 @@ int client_config_load(struct mosq_config *cfg, int pub_or_sub, int argc, char *
}
if(fptr){
while(fgets(line, 1024, fptr)){
if(line[0] == '#') continue; /* Comments */
if(line[0] == '#'){
/* Comments */
continue;
}
while(line[strlen(line)-1] == 10 || line[strlen(line)-1] == 13){
line[strlen(line)-1] = 0;
}
@@ -441,7 +443,9 @@ int client_config_load(struct mosq_config *cfg, int pub_or_sub, int argc, char *
/* Deal with real argc/argv */
rc = client_config_line_proc(cfg, pub_or_sub, argc, argv);
if(rc) return rc;
if(rc){
return rc;
}
if(cfg->will_payload && !cfg->will_topic){
fprintf(stderr, "Error: Will payload given, but no will topic given.\n");
@@ -799,8 +803,9 @@ int client_config_line_proc(struct mosq_config *cfg, int pub_or_sub, int argc, c
}
*topic++ = 0;
if(cfg_add_topic(cfg, pub_or_sub, topic, "-L topic"))
if(cfg_add_topic(cfg, pub_or_sub, topic, "-L topic")){
return 1;
}
tmp = strchr(url, '@');
if(tmp){
@@ -1088,8 +1093,9 @@ int client_config_line_proc(struct mosq_config *cfg, int pub_or_sub, int argc, c
fprintf(stderr, "Error: -t argument given but no topic specified.\n\n");
return 1;
}else{
if(cfg_add_topic(cfg, pub_or_sub, argv[i + 1], "-t"))
if(cfg_add_topic(cfg, pub_or_sub, argv[i + 1], "-t")){
return 1;
}
i++;
}
}else if(!strcmp(argv[i], "-T") || !strcmp(argv[i], "--filter-out")){
@@ -1543,9 +1549,13 @@ static int mosquitto__urldecode(char *str)
{
size_t i, j;
size_t len;
if(!str) return 0;
if(!str){
return 0;
}
if(!strchr(str, '%')) return 0;
if(!strchr(str, '%')){
return 0;
}
len = strlen(str);
for(i=0; i<len; i++){
@@ -1774,7 +1784,9 @@ void err_printf(const struct mosq_config *cfg, const char *fmt, ...)
{
va_list va;
if(cfg->quiet) return;
if(cfg->quiet){
return;
}
va_start(va, fmt);
vfprintf(stderr, fmt, va);
+6 -2
View File
@@ -296,7 +296,9 @@ static int pub_stdin_line_loop(struct mosquitto *mosq)
if(pos != 0){
rc = my_publish(mosq, &mid_sent, cfg.topic, buf_len_actual, line_buf, cfg.qos, cfg.retain);
if(rc){
if(cfg.qos>0) return rc;
if(cfg.qos>0){
return rc;
}
}
}
if(feof(stdin)){
@@ -535,7 +537,9 @@ int main(int argc, char *argv[])
mosquitto_lib_init();
if(pub_shared_init()) return 1;
if(pub_shared_init()){
return 1;
}
rc = client_config_load(&cfg, CLIENT_PUB, argc, argv);
if(rc){
+6 -2
View File
@@ -88,8 +88,12 @@ static void my_message_callback(struct mosquitto *mosq, void *obj, const struct
UNUSED(obj);
UNUSED(properties);
if(process_messages == false) return;
if(message->retain && cfg.no_retain) return;
if(process_messages == false){
return;
}
if(message->retain && cfg.no_retain){
return;
}
mosquitto_time_ns(&publish_recv_time.tv_sec, &publish_recv_time.tv_nsec);
+18 -6
View File
@@ -100,7 +100,9 @@ static void my_message_callback(struct mosquitto *mosq, void *obj, const struct
message_rate_msg_count++;
if(process_messages == false) return;
if(process_messages == false){
return;
}
if(cfg.retained_only && !message->retain && process_messages){
process_messages = false;
@@ -110,11 +112,15 @@ static void my_message_callback(struct mosquitto *mosq, void *obj, const struct
return;
}
if(message->retain && cfg.no_retain) return;
if(message->retain && cfg.no_retain){
return;
}
if(cfg.filter_outs){
for(i=0; i<cfg.filter_out_count; i++){
mosquitto_topic_matches_sub(cfg.filter_outs[i], message->topic, &res);
if(res) return;
if(res){
return;
}
}
}
@@ -180,12 +186,18 @@ static void my_subscribe_callback(struct mosquitto *mosq, void *obj, int mid, in
bool should_print = cfg.debug && !cfg.quiet;
UNUSED(obj);
if(should_print) printf("Subscribed (mid: %d): %d", mid, granted_qos[0]);
if(should_print){
printf("Subscribed (mid: %d): %d", mid, granted_qos[0]);
}
for(i=1; i<qos_count; i++){
if(should_print) printf(", %d", granted_qos[i]);
if(should_print){
printf(", %d", granted_qos[i]);
}
some_sub_allowed |= (granted_qos[i] < 128);
}
if(should_print) printf("\n");
if(should_print){
printf("\n");
}
if(some_sub_allowed == false){
mosquitto_disconnect_v5(mosq, 0, cfg.disconnect_props);
+6 -2
View File
@@ -211,7 +211,9 @@ static int json_print_properties(cJSON *root, const mosquitto_property *properti
case MQTT_PROP_CONTENT_TYPE:
case MQTT_PROP_RESPONSE_TOPIC:
mosquitto_property_read_string(prop, identifier, &strvalue, false);
if(strvalue == NULL) return MOSQ_ERR_NOMEM;
if(strvalue == NULL){
return MOSQ_ERR_NOMEM;
}
tmp = cJSON_CreateString(strvalue);
free(strvalue);
strvalue = NULL;
@@ -219,7 +221,9 @@ static int json_print_properties(cJSON *root, const mosquitto_property *properti
case MQTT_PROP_CORRELATION_DATA:
mosquitto_property_read_binary(prop, MQTT_PROP_CORRELATION_DATA, (void **)&binvalue, &i16value, false);
if(binvalue == NULL) return MOSQ_ERR_NOMEM;
if(binvalue == NULL){
return MOSQ_ERR_NOMEM;
}
tmp = cJSON_CreateString(binvalue);
free(binvalue);
binvalue = NULL;
+6 -2
View File
@@ -48,7 +48,9 @@ int fuzz_packet_read_base(const uint8_t *data, size_t size, int (*packet_func)(s
memset(&secopts, 0, sizeof(secopts));
context = context__init();
if(!context) return 1;
if(!context){
return 1;
}
listener.security_options = &secopts;
context->listener = &listener;
context->bridge = &bridge;
@@ -58,7 +60,9 @@ int fuzz_packet_read_base(const uint8_t *data, size_t size, int (*packet_func)(s
size -= 2;
data_heap = (uint8_t *)malloc(size);
if(!data_heap) return 1;
if(!data_heap){
return 1;
}
memcpy(data_heap, &data[2], size);
+24 -8
View File
@@ -50,9 +50,15 @@ int mosquitto_publish_v5(struct mosquitto *mosq, int *mid, const char *topic, in
size_t tlen = 0;
uint32_t remaining_length;
if(!mosq || qos<0 || qos>2) return MOSQ_ERR_INVAL;
if(mosq->protocol != mosq_p_mqtt5 && properties) return MOSQ_ERR_NOT_SUPPORTED;
if(qos > mosq->max_qos) return MOSQ_ERR_QOS_NOT_SUPPORTED;
if(!mosq || qos<0 || qos>2){
return MOSQ_ERR_INVAL;
}
if(mosq->protocol != mosq_p_mqtt5 && properties){
return MOSQ_ERR_NOT_SUPPORTED;
}
if(qos > mosq->max_qos){
return MOSQ_ERR_QOS_NOT_SUPPORTED;
}
if(!mosq->retain_available){
retain = false;
@@ -68,11 +74,15 @@ int mosquitto_publish_v5(struct mosquitto *mosq, int *mid, const char *topic, in
outgoing_properties = &local_property;
}
rc = mosquitto_property_check_all(CMD_PUBLISH, outgoing_properties);
if(rc) return rc;
if(rc){
return rc;
}
}
if(!topic || STREMPTY(topic)){
if(topic) topic = NULL;
if(topic){
topic = NULL;
}
if(mosq->protocol == mosq_p_mqtt5){
p = outgoing_properties;
@@ -92,8 +102,12 @@ int mosquitto_publish_v5(struct mosquitto *mosq, int *mid, const char *topic, in
}
}else{
tlen = strlen(topic);
if(mosquitto_validate_utf8(topic, (int)tlen)) return MOSQ_ERR_MALFORMED_UTF8;
if(payloadlen < 0 || payloadlen > (int)MQTT_MAX_PAYLOAD) return MOSQ_ERR_PAYLOAD_SIZE;
if(mosquitto_validate_utf8(topic, (int)tlen)){
return MOSQ_ERR_MALFORMED_UTF8;
}
if(payloadlen < 0 || payloadlen > (int)MQTT_MAX_PAYLOAD){
return MOSQ_ERR_PAYLOAD_SIZE;
}
if(mosquitto_pub_topic_check(topic) != MOSQ_ERR_SUCCESS){
return MOSQ_ERR_INVAL;
}
@@ -119,7 +133,9 @@ int mosquitto_publish_v5(struct mosquitto *mosq, int *mid, const char *topic, in
}else{
if(outgoing_properties){
rc = mosquitto_property_copy_all(&properties_copy, outgoing_properties);
if(rc) return rc;
if(rc){
return rc;
}
}
message = mosquitto_calloc(1, sizeof(struct mosquitto_message_all));
if(!message){
+24 -8
View File
@@ -48,11 +48,21 @@ int mosquitto_subscribe_multiple(struct mosquitto *mosq, int *mid, int sub_count
uint32_t remaining_length = 0;
int slen;
if(!mosq || !sub_count || !sub) return MOSQ_ERR_INVAL;
if(mosq->protocol != mosq_p_mqtt5 && properties) return MOSQ_ERR_NOT_SUPPORTED;
if(qos < 0 || qos > 2) return MOSQ_ERR_INVAL;
if((options & 0x30) == 0x30 || (options & 0xC0) != 0) return MOSQ_ERR_INVAL;
if(!net__is_connected(mosq)) return MOSQ_ERR_NO_CONN;
if(!mosq || !sub_count || !sub){
return MOSQ_ERR_INVAL;
}
if(mosq->protocol != mosq_p_mqtt5 && properties){
return MOSQ_ERR_NOT_SUPPORTED;
}
if(qos < 0 || qos > 2){
return MOSQ_ERR_INVAL;
}
if((options & 0x30) == 0x30 || (options & 0xC0) != 0){
return MOSQ_ERR_INVAL;
}
if(!net__is_connected(mosq)){
return MOSQ_ERR_NO_CONN;
}
if(properties){
if(properties->client_generated){
@@ -64,16 +74,22 @@ int mosquitto_subscribe_multiple(struct mosquitto *mosq, int *mid, int sub_count
outgoing_properties = &local_property;
}
rc = mosquitto_property_check_all(CMD_SUBSCRIBE, outgoing_properties);
if(rc) return rc;
if(rc){
return rc;
}
}
for(i=0; i<sub_count; i++){
if(mosquitto_sub_topic_check(sub[i])) return MOSQ_ERR_INVAL;
if(mosquitto_sub_topic_check(sub[i])){
return MOSQ_ERR_INVAL;
}
slen = (int)strlen(sub[i]);
if(slen == 0){
return MOSQ_ERR_INVAL;
}
if(mosquitto_validate_utf8(sub[i], slen)) return MOSQ_ERR_MALFORMED_UTF8;
if(mosquitto_validate_utf8(sub[i], slen)){
return MOSQ_ERR_MALFORMED_UTF8;
}
remaining_length += 2+(uint32_t)slen + 1;
}
+21 -7
View File
@@ -50,9 +50,15 @@ int mosquitto_unsubscribe_multiple(struct mosquitto *mosq, int *mid, int sub_cou
uint32_t remaining_length = 0;
int slen;
if(!mosq) return MOSQ_ERR_INVAL;
if(mosq->protocol != mosq_p_mqtt5 && properties) return MOSQ_ERR_NOT_SUPPORTED;
if(!net__is_connected(mosq)) return MOSQ_ERR_NO_CONN;
if(!mosq){
return MOSQ_ERR_INVAL;
}
if(mosq->protocol != mosq_p_mqtt5 && properties){
return MOSQ_ERR_NOT_SUPPORTED;
}
if(!net__is_connected(mosq)){
return MOSQ_ERR_NO_CONN;
}
if(properties){
if(properties->client_generated){
@@ -64,14 +70,22 @@ int mosquitto_unsubscribe_multiple(struct mosquitto *mosq, int *mid, int sub_cou
outgoing_properties = &local_property;
}
rc = mosquitto_property_check_all(CMD_UNSUBSCRIBE, outgoing_properties);
if(rc) return rc;
if(rc){
return rc;
}
}
for(i=0; i<sub_count; i++){
if(mosquitto_sub_topic_check(sub[i])) return MOSQ_ERR_INVAL;
if(mosquitto_sub_topic_check(sub[i])){
return MOSQ_ERR_INVAL;
}
slen = (int)strlen(sub[i]);
if(slen == 0) return MOSQ_ERR_INVAL;
if(mosquitto_validate_utf8(sub[i], slen)) return MOSQ_ERR_MALFORMED_UTF8;
if(slen == 0){
return MOSQ_ERR_INVAL;
}
if(mosquitto_validate_utf8(sub[i], slen)){
return MOSQ_ERR_MALFORMED_UTF8;
}
remaining_length += 2U + (uint32_t)slen;
}
+6 -2
View File
@@ -33,7 +33,9 @@ int alias__add_l2r(struct mosquitto *mosq, const char *topic, uint16_t *alias)
if(mosq->alias_count_l2r < mosq->alias_max_l2r){
aliases_new = mosquitto_realloc(mosq->aliases_l2r, sizeof(struct mosquitto__alias)*(size_t)(mosq->alias_count_l2r+1));
if(!aliases_new) return MOSQ_ERR_NOMEM;
if(!aliases_new){
return MOSQ_ERR_NOMEM;
}
mosq->aliases_l2r = aliases_new;
mosq->alias_count_l2r++;
@@ -72,7 +74,9 @@ int alias__add_r2l(struct mosquitto *mosq, const char *topic, uint16_t alias)
/* New alias */
aliases_new = mosquitto_realloc(mosq->aliases_r2l, sizeof(struct mosquitto__alias)*(size_t)(mosq->alias_count_r2l+1));
if(!aliases_new) return MOSQ_ERR_NOMEM;
if(!aliases_new){
return MOSQ_ERR_NOMEM;
}
mosq->aliases_r2l = aliases_new;
mosq->alias_count_r2l++;
+57 -19
View File
@@ -51,9 +51,15 @@ static int mosquitto__connect_init(struct mosquitto *mosq, const char *host, int
int i;
int rc;
if(!mosq) return MOSQ_ERR_INVAL;
if(!host || port < 0 || port > UINT16_MAX) return MOSQ_ERR_INVAL;
if(keepalive != 0 && (keepalive < 5 || keepalive > UINT16_MAX)) return MOSQ_ERR_INVAL;
if(!mosq){
return MOSQ_ERR_INVAL;
}
if(!host || port < 0 || port > UINT16_MAX){
return MOSQ_ERR_INVAL;
}
if(keepalive != 0 && (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,7 +74,9 @@ static int mosquitto__connect_init(struct mosquitto *mosq, const char *host, int
mosq->id[4] = '-';
rc = mosquitto_getrandom(&mosq->id[5], 18);
if(rc) return rc;
if(rc){
return rc;
}
for(i=5; i<23; i++){
mosq->id[i] = alphanum[(mosq->id[i]&0x7F)%(sizeof(alphanum)-1)];
@@ -77,7 +85,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;
if(!mosq->host){
return MOSQ_ERR_NOMEM;
}
mosq->port = (uint16_t)port;
mosq->keepalive = (uint16_t)keepalive;
@@ -108,21 +118,29 @@ int mosquitto_connect_bind_v5(struct mosquitto *mosq, const char *host, int port
if(bind_address){
rc = mosquitto_string_option(mosq, MOSQ_OPT_BIND_ADDRESS, bind_address);
if(rc) return rc;
if(rc){
return rc;
}
}
mosquitto_property_free_all(&mosq->connect_properties);
if(properties){
rc = mosquitto_property_check_all(CMD_CONNECT, properties);
if(rc) return rc;
if(rc){
return rc;
}
rc = mosquitto_property_copy_all(&mosq->connect_properties, properties);
if(rc) return rc;
if(rc){
return rc;
}
mosq->connect_properties->client_generated = true;
}
rc = mosquitto__connect_init(mosq, host, port, keepalive);
if(rc) return rc;
if(rc){
return rc;
}
mosquitto__set_state(mosq, mosq_cs_new);
@@ -142,11 +160,15 @@ int mosquitto_connect_bind_async(struct mosquitto *mosq, const char *host, int p
if(bind_address){
rc = mosquitto_string_option(mosq, MOSQ_OPT_BIND_ADDRESS, bind_address);
if(rc) return rc;
if(rc){
return rc;
}
}
rc = mosquitto__connect_init(mosq, host, port, keepalive);
if(rc) return rc;
if(rc){
return rc;
}
return mosquitto__reconnect(mosq, false);
}
@@ -198,11 +220,17 @@ static int mosquitto__reconnect(struct mosquitto *mosq, bool blocking)
mosquitto_property local_property;
int rc;
if(!mosq) return MOSQ_ERR_INVAL;
if(!mosq->host) return MOSQ_ERR_INVAL;
if(!mosq){
return MOSQ_ERR_INVAL;
}
if(!mosq->host){
return MOSQ_ERR_INVAL;
}
if(mosq->connect_properties){
if(mosq->protocol != mosq_p_mqtt5) return MOSQ_ERR_NOT_SUPPORTED;
if(mosq->protocol != mosq_p_mqtt5){
return MOSQ_ERR_NOT_SUPPORTED;
}
if(mosq->connect_properties->client_generated){
outgoing_properties = mosq->connect_properties;
@@ -213,7 +241,9 @@ static int mosquitto__reconnect(struct mosquitto *mosq, bool blocking)
outgoing_properties = &local_property;
}
rc = mosquitto_property_check_all(CMD_CONNECT, outgoing_properties);
if(rc) return rc;
if(rc){
return rc;
}
}
COMPAT_pthread_mutex_lock(&mosq->msgtime_mutex);
@@ -287,9 +317,15 @@ int mosquitto_disconnect_v5(struct mosquitto *mosq, int reason_code, const mosqu
const mosquitto_property *outgoing_properties = NULL;
mosquitto_property local_property;
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(!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){
@@ -301,7 +337,9 @@ int mosquitto_disconnect_v5(struct mosquitto *mosq, int reason_code, const mosqu
outgoing_properties = &local_property;
}
rc = mosquitto_property_check_all(CMD_DISCONNECT, outgoing_properties);
if(rc) return rc;
if(rc){
return rc;
}
}
mosquitto__set_state(mosq, mosq_cs_disconnected);
+18 -6
View File
@@ -32,27 +32,39 @@ int mosquitto_ext_auth_continue(struct mosquitto *context, const char *auth_meth
mosquitto_property *properties = NULL;
rc = mosquitto_property_copy_all(&properties, input_props);
if(rc) return rc;
if(rc){
return rc;
}
if(!context || context->protocol != mosq_p_mqtt5 || !auth_method) return MOSQ_ERR_PROTOCOL;
if(!context || context->protocol != mosq_p_mqtt5 || !auth_method){
return MOSQ_ERR_PROTOCOL;
}
remaining_length = 1;
rc = mosquitto_property_add_string(&properties, MQTT_PROP_AUTHENTICATION_METHOD, auth_method);
if(rc) goto error;
if(rc){
goto error;
}
if(auth_data != NULL && auth_data_len > 0){
rc = mosquitto_property_add_binary(&properties, MQTT_PROP_AUTHENTICATION_DATA, auth_data, auth_data_len);
if(rc) goto error;
if(rc){
goto error;
}
}
remaining_length += mosquitto_property_get_remaining_length(properties);
rc = packet__check_oversize(context, remaining_length);
if(rc) goto error;
if(rc){
goto error;
}
rc = packet__alloc(&packet, CMD_AUTH, remaining_length);
if(rc) goto error;
if(rc){
goto error;
}
packet__write_byte(packet, MQTT_RC_CONTINUE_AUTHENTICATION);
property__write_all(packet, properties, true);
+9 -3
View File
@@ -39,7 +39,9 @@ int handle__auth(struct mosquitto *mosq)
uint16_t auth_data_len = 0;
mosquitto_property *properties = NULL;
if(!mosq) return MOSQ_ERR_INVAL;
if(!mosq){
return MOSQ_ERR_INVAL;
}
log__printf(mosq, MOSQ_LOG_DEBUG, "Client %s received AUTH", SAFE_PRINT(mosq->id));
if(mosq->protocol != mosq_p_mqtt5){
@@ -49,10 +51,14 @@ int handle__auth(struct mosquitto *mosq)
return MOSQ_ERR_MALFORMED_PACKET;
}
if(packet__read_byte(&mosq->in_packet, &reason_code)) return 1;
if(packet__read_byte(&mosq->in_packet, &reason_code)){
return 1;
}
rc = property__read_all(CMD_AUTH, &mosq->in_packet, &properties);
if(rc) return rc;
if(rc){
return rc;
}
mosquitto_property_read_string(properties, MQTT_PROP_AUTHENTICATION_METHOD, &auth_method, false);
mosquitto_property_read_binary(properties, MQTT_PROP_AUTHENTICATION_DATA, &auth_data, &auth_data_len, false);
+6 -2
View File
@@ -54,7 +54,9 @@ int handle__connack(struct mosquitto *mosq)
}
rc = packet__read_byte(&mosq->in_packet, &connect_flags);
if(rc) return rc;
if(rc){
return rc;
}
if((mosq->protocol == mosq_p_mqtt311 || mosq->protocol == mosq_p_mqtt5) && (connect_flags & 0xFE)){
log__printf(mosq, MOSQ_LOG_DEBUG, "Client %s received CONNACK with invalid connect flags (%d)", mosq->id, connect_flags);
return MOSQ_ERR_PROTOCOL;
@@ -65,7 +67,9 @@ int handle__connack(struct mosquitto *mosq)
}
rc = packet__read_byte(&mosq->in_packet, &reason_code);
if(rc) return rc;
if(rc){
return rc;
}
if(mosq->protocol == mosq_p_mqtt5){
rc = property__read_all(CMD_CONNACK, &mosq->in_packet, &properties);
+6 -2
View File
@@ -49,11 +49,15 @@ int handle__disconnect(struct mosquitto *mosq)
}
rc = packet__read_byte(&mosq->in_packet, &reason_code);
if(rc) return rc;
if(rc){
return rc;
}
if(mosq->in_packet.remaining_length > 2){
rc = property__read_all(CMD_DISCONNECT, &mosq->in_packet, &properties);
if(rc) return rc;
if(rc){
return rc;
}
mosquitto_property_free_all(&properties);
}
+6 -2
View File
@@ -62,7 +62,9 @@ int handle__pubackcomp(struct mosquitto *mosq, const char *type)
COMPAT_pthread_mutex_unlock(&mosq->msgs_out.mutex);
rc = packet__read_uint16(&mosq->in_packet, &mid);
if(rc) return rc;
if(rc){
return rc;
}
if(type[3] == 'A'){ /* pubAck or pubComp */
if(mosq->in_packet.command != CMD_PUBACK){
return MOSQ_ERR_MALFORMED_PACKET;
@@ -86,7 +88,9 @@ int handle__pubackcomp(struct mosquitto *mosq, const char *type)
if(mosq->in_packet.remaining_length > 3){
rc = property__read_all(CMD_PUBACK, &mosq->in_packet, &properties);
if(rc) return rc;
if(rc){
return rc;
}
}
if(type[3] == 'A'){ /* pubAck or pubComp */
if(reason_code != MQTT_RC_SUCCESS
+3 -1
View File
@@ -103,7 +103,9 @@ int handle__publish(struct mosquitto *mosq)
}
message = mosquitto_calloc(1, sizeof(struct mosquitto_message_all));
if(!message) return MOSQ_ERR_NOMEM;
if(!message){
return MOSQ_ERR_NOMEM;
}
header = mosq->in_packet.command;
+15 -5
View File
@@ -55,12 +55,18 @@ int handle__pubrec(struct mosquitto *mosq)
}
rc = packet__read_uint16(&mosq->in_packet, &mid);
if(rc) return rc;
if(mid == 0) return MOSQ_ERR_PROTOCOL;
if(rc){
return rc;
}
if(mid == 0){
return MOSQ_ERR_PROTOCOL;
}
if(mosq->protocol == mosq_p_mqtt5 && mosq->in_packet.remaining_length > 2){
rc = packet__read_byte(&mosq->in_packet, &reason_code);
if(rc) return rc;
if(rc){
return rc;
}
if(reason_code != MQTT_RC_SUCCESS
&& reason_code != MQTT_RC_NO_MATCHING_SUBSCRIBERS
@@ -77,7 +83,9 @@ int handle__pubrec(struct mosquitto *mosq)
if(mosq->in_packet.remaining_length > 3){
rc = property__read_all(CMD_PUBREC, &mosq->in_packet, &properties);
if(rc) return rc;
if(rc){
return rc;
}
/* Immediately free, we don't do anything with Reason String or User Property at the moment */
mosquitto_property_free_all(&properties);
@@ -123,7 +131,9 @@ int handle__pubrec(struct mosquitto *mosq)
return rc;
}
rc = send__pubrel(mosq, mid, NULL);
if(rc) return rc;
if(rc){
return rc;
}
return MOSQ_ERR_SUCCESS;
}
+15 -5
View File
@@ -63,12 +63,18 @@ int handle__pubrel(struct mosquitto *mosq)
}
}
rc = packet__read_uint16(&mosq->in_packet, &mid);
if(rc) return rc;
if(mid == 0) return MOSQ_ERR_PROTOCOL;
if(rc){
return rc;
}
if(mid == 0){
return MOSQ_ERR_PROTOCOL;
}
if(mosq->protocol == mosq_p_mqtt5 && mosq->in_packet.remaining_length > 2){
rc = packet__read_byte(&mosq->in_packet, &reason_code);
if(rc) return rc;
if(rc){
return rc;
}
if(reason_code != MQTT_RC_SUCCESS && reason_code != MQTT_RC_PACKET_ID_NOT_FOUND){
return MOSQ_ERR_PROTOCOL;
@@ -76,7 +82,9 @@ int handle__pubrel(struct mosquitto *mosq)
if(mosq->in_packet.remaining_length > 3){
rc = property__read_all(CMD_PUBREL, &mosq->in_packet, &properties);
if(rc) return rc;
if(rc){
return rc;
}
/* Immediately free, we don't do anything with Reason String or
* User Property at the moment */
mosquitto_property_free_all(&properties);
@@ -99,7 +107,9 @@ int handle__pubrel(struct mosquitto *mosq)
}
rc = send__pubcomp(mosq, mid, NULL);
if(rc) return rc;
if(rc){
return rc;
}
#else
log__printf(mosq, MOSQ_LOG_DEBUG, "Client %s received PUBREL (Mid: %d)", SAFE_PRINT(mosq->id), mid);
+9 -3
View File
@@ -64,12 +64,18 @@ int handle__suback(struct mosquitto *mosq)
log__printf(mosq, MOSQ_LOG_DEBUG, "Client %s received SUBACK", SAFE_PRINT(mosq->id));
#endif
rc = packet__read_uint16(&mosq->in_packet, &mid);
if(rc) return rc;
if(mid == 0) return MOSQ_ERR_PROTOCOL;
if(rc){
return rc;
}
if(mid == 0){
return MOSQ_ERR_PROTOCOL;
}
if(mosq->protocol == mosq_p_mqtt5){
rc = property__read_all(CMD_SUBACK, &mosq->in_packet, &properties);
if(rc) return rc;
if(rc){
return rc;
}
}
qos_count = (int)(mosq->in_packet.remaining_length - mosq->in_packet.pos);
+9 -3
View File
@@ -66,12 +66,18 @@ int handle__unsuback(struct mosquitto *mosq)
log__printf(mosq, MOSQ_LOG_DEBUG, "Client %s received UNSUBACK", SAFE_PRINT(mosq->id));
#endif
rc = packet__read_uint16(&mosq->in_packet, &mid);
if(rc) return rc;
if(mid == 0) return MOSQ_ERR_PROTOCOL;
if(rc){
return rc;
}
if(mid == 0){
return MOSQ_ERR_PROTOCOL;
}
if(mosq->protocol == mosq_p_mqtt5){
rc = property__read_all(CMD_UNSUBACK, &mosq->in_packet, &properties);
if(rc) return rc;
if(rc){
return rc;
}
uint8_t byte;
reason_code_count = (int)(mosq->in_packet.remaining_length - mosq->in_packet.pos);
+3 -1
View File
@@ -63,7 +63,9 @@ int http_c__context_init(struct mosquitto *context)
}
packet = mosquitto_calloc(1, sizeof(struct mosquitto__packet) + 1024 + WS_PACKET_OFFSET);
if(!packet) return MOSQ_ERR_NOMEM;
if(!packet){
return MOSQ_ERR_NOMEM;
}
path = context->wsd.http_path?context->wsd.http_path:"/mqtt";
+21 -7
View File
@@ -46,9 +46,15 @@ void mosquitto__destroy(struct mosquitto *mosq);
int mosquitto_lib_version(int *major, int *minor, int *revision)
{
if(major) *major = LIBMOSQUITTO_MAJOR;
if(minor) *minor = LIBMOSQUITTO_MINOR;
if(revision) *revision = LIBMOSQUITTO_REVISION;
if(major){
*major = LIBMOSQUITTO_MAJOR;
}
if(minor){
*minor = LIBMOSQUITTO_MINOR;
}
if(revision){
*revision = LIBMOSQUITTO_REVISION;
}
return LIBMOSQUITTO_VERSION_NUMBER;
}
@@ -152,7 +158,9 @@ struct mosquitto *mosquitto_new(const char *id, bool clean_start, void *userdata
int mosquitto_reinitialise(struct mosquitto *mosq, const char *id, bool clean_start, void *userdata)
{
if(!mosq) return MOSQ_ERR_INVAL;
if(!mosq){
return MOSQ_ERR_INVAL;
}
if(clean_start == false && id == NULL){
return MOSQ_ERR_INVAL;
@@ -260,7 +268,9 @@ int mosquitto_reinitialise(struct mosquitto *mosq, const char *id, bool clean_st
void mosquitto__destroy(struct mosquitto *mosq)
{
if(!mosq) return;
if(!mosq){
return;
}
#ifdef WITH_THREADING
# ifdef HAVE_PTHREAD_CANCEL
@@ -347,7 +357,9 @@ void mosquitto__destroy(struct mosquitto *mosq)
void mosquitto_destroy(struct mosquitto *mosq)
{
if(!mosq) return;
if(!mosq){
return;
}
mosquitto__destroy(mosq);
mosquitto_FREE(mosq);
@@ -356,7 +368,9 @@ void mosquitto_destroy(struct mosquitto *mosq)
int mosquitto_socket(struct mosquitto *mosq)
{
if(!mosq) return INVALID_SOCKET;
if(!mosq){
return INVALID_SOCKET;
}
return mosq->sock;
}
+24 -8
View File
@@ -54,7 +54,9 @@ int mosquitto_loop(struct mosquitto *mosq, int timeout, int max_packets)
time_t now;
time_t timeout_ms;
if(!mosq || max_packets < 1) return MOSQ_ERR_INVAL;
if(!mosq || max_packets < 1){
return MOSQ_ERR_INVAL;
}
#ifndef WIN32
if(mosq->sock >= FD_SETSIZE || mosq->sockpairR >= FD_SETSIZE){
return MOSQ_ERR_INVAL;
@@ -245,7 +247,9 @@ int mosquitto_loop_forever(struct mosquitto *mosq, int timeout, int max_packets)
int rc = MOSQ_ERR_SUCCESS;
unsigned long reconnect_delay;
if(!mosq) return MOSQ_ERR_INVAL;
if(!mosq){
return MOSQ_ERR_INVAL;
}
mosq->reconnects = 0;
@@ -302,7 +306,9 @@ int mosquitto_loop_forever(struct mosquitto *mosq, int timeout, int max_packets)
}
rc = interruptible_sleep(mosq, (time_t)reconnect_delay);
if(rc) return rc;
if(rc){
return rc;
}
if(mosquitto__get_request_disconnect(mosq)){
run = 0;
@@ -318,8 +324,12 @@ int mosquitto_loop_forever(struct mosquitto *mosq, int timeout, int max_packets)
int mosquitto_loop_misc(struct mosquitto *mosq)
{
if(!mosq) return MOSQ_ERR_INVAL;
if(!net__is_connected(mosq)) return MOSQ_ERR_NO_CONN;
if(!mosq){
return MOSQ_ERR_INVAL;
}
if(!net__is_connected(mosq)){
return MOSQ_ERR_NO_CONN;
}
return mosquitto__check_keepalive(mosq);
}
@@ -345,7 +355,9 @@ int mosquitto_loop_read(struct mosquitto *mosq, int max_packets)
{
int rc = MOSQ_ERR_SUCCESS;
int i;
if(max_packets < 1) return MOSQ_ERR_INVAL;
if(max_packets < 1){
return MOSQ_ERR_INVAL;
}
COMPAT_pthread_mutex_lock(&mosq->msgs_out.mutex);
max_packets = mosq->msgs_out.queue_len;
@@ -355,7 +367,9 @@ int mosquitto_loop_read(struct mosquitto *mosq, int max_packets)
max_packets += mosq->msgs_in.queue_len;
COMPAT_pthread_mutex_unlock(&mosq->msgs_in.mutex);
if(max_packets < 1) max_packets = 1;
if(max_packets < 1){
max_packets = 1;
}
/* Queue len here tells us how many messages are awaiting processing and
* have QoS > 0. We should try to deal with that many in this loop in order
* to keep up. */
@@ -392,7 +406,9 @@ int mosquitto_loop_write(struct mosquitto *mosq, int max_packets)
{
int rc = MOSQ_ERR_SUCCESS;
int i;
if(max_packets < 1) return MOSQ_ERR_INVAL;
if(max_packets < 1){
return MOSQ_ERR_INVAL;
}
for(i=0; i<max_packets; i++){
rc = packet__write(mosq);
+15 -5
View File
@@ -34,7 +34,9 @@ void message__cleanup(struct mosquitto_message_all **message)
{
struct mosquitto_message_all *msg;
if(!message || !*message) return;
if(!message || !*message){
return;
}
msg = *message;
@@ -64,11 +66,15 @@ void message__cleanup_all(struct mosquitto *mosq)
int mosquitto_message_copy(struct mosquitto_message *dst, const struct mosquitto_message *src)
{
if(!dst || !src) return MOSQ_ERR_INVAL;
if(!dst || !src){
return MOSQ_ERR_INVAL;
}
dst->mid = src->mid;
dst->topic = mosquitto_strdup(src->topic);
if(!dst->topic) return MOSQ_ERR_NOMEM;
if(!dst->topic){
return MOSQ_ERR_NOMEM;
}
dst->qos = src->qos;
dst->retain = src->retain;
if(src->payloadlen){
@@ -105,7 +111,9 @@ void mosquitto_message_free(struct mosquitto_message **message)
{
struct mosquitto_message *msg;
if(!message || !*message) return;
if(!message || !*message){
return;
}
msg = *message;
@@ -117,7 +125,9 @@ void mosquitto_message_free(struct mosquitto_message **message)
void mosquitto_message_free_contents(struct mosquitto_message *message)
{
if(!message) return;
if(!message){
return;
}
mosquitto_FREE(message->topic);
mosquitto_FREE(message->payload);
+27 -9
View File
@@ -181,7 +181,9 @@ void net__cleanup(void)
void net__init_tls(void)
{
if(is_tls_initialized) return;
if(is_tls_initialized){
return;
}
#if !defined(OPENSSL_NO_ENGINE) && OPENSSL_API_LEVEL < 30000
ENGINE_load_builtin_engines();
@@ -295,12 +297,16 @@ static unsigned int psk_client_callback(SSL *ssl, const char *hint,
UNUSED(hint);
mosq = SSL_get_ex_data(ssl, tls_ex_index_mosq);
if(!mosq) return 0;
if(!mosq){
return 0;
}
snprintf(identity, max_identity_len, "%s", mosq->tls_psk_identity);
len = mosquitto__hex2bin(mosq->tls_psk, psk, (int)max_psk_len);
if(len < 0) return 0;
if(len < 0){
return 0;
}
return (unsigned int)len;
}
#endif
@@ -366,7 +372,9 @@ int net__try_connect_step2(struct mosquitto *mosq, uint16_t port, mosq_sock_t *s
for(rp = ainfo; rp != NULL; rp = rp->ai_next){
*sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
if(*sock == INVALID_SOCKET) continue;
if(*sock == INVALID_SOCKET){
continue;
}
if(rp->ai_family == AF_INET){
((struct sockaddr_in *)rp->ai_addr)->sin_port = htons(port);
@@ -449,7 +457,9 @@ static int net__try_connect_tcp(const char *host, uint16_t port, mosq_sock_t *so
for(rp = ainfo; rp != NULL; rp = rp->ai_next){
*sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
if(*sock == INVALID_SOCKET) continue;
if(*sock == INVALID_SOCKET){
continue;
}
if(rp->ai_family == AF_INET){
((struct sockaddr_in *)rp->ai_addr)->sin_port = htons(port);
@@ -533,7 +543,9 @@ static int net__try_connect_unix(const char *host, mosq_sock_t *sock)
return MOSQ_ERR_ERRNO;
}
rc = net__socket_nonblock(&s);
if(rc) return rc;
if(rc){
return rc;
}
rc = connect(s, (struct sockaddr *)&addr, sizeof(struct sockaddr_un));
if(rc < 0){
@@ -950,10 +962,14 @@ int net__socket_connect(struct mosquitto *mosq, const char *host, uint16_t port,
{
int rc, rc2;
if(!mosq || !host) return MOSQ_ERR_INVAL;
if(!mosq || !host){
return MOSQ_ERR_INVAL;
}
rc = net__try_connect(host, port, &mosq->sock, bind_address, blocking);
if(rc > 0) return rc;
if(rc > 0){
return rc;
}
if(mosq->tcp_nodelay && port){
int flag = 1;
@@ -967,7 +983,9 @@ int net__socket_connect(struct mosquitto *mosq, const char *host, uint16_t port,
#endif
{
rc2 = net__socket_connect_step3(mosq, host);
if(rc2) return rc2;
if(rc2){
return rc2;
}
}
return rc;
+14 -5
View File
@@ -125,8 +125,9 @@ int mosquitto__verify_ocsp_status_cb(SSL *ssl, void *arg)
ASN1_GENERALIZEDTIME *rev, *thisupd, *nextupd;
single = OCSP_resp_get0(br, i);
if(!single)
if(!single){
continue;
}
cert_status = OCSP_single_get0_status(single, &crl_reason, &rev, &thisupd, &nextupd);
@@ -156,13 +157,21 @@ int mosquitto__verify_ocsp_status_cb(SSL *ssl, void *arg)
}
}
if(br!=NULL) OCSP_BASICRESP_free(br);
if(rsp!=NULL) OCSP_RESPONSE_free(rsp);
if(br!=NULL){
OCSP_BASICRESP_free(br);
}
if(rsp!=NULL){
OCSP_RESPONSE_free(rsp);
}
return 1; /* OK */
end:
if(br!=NULL) OCSP_BASICRESP_free(br);
if(rsp!=NULL) OCSP_RESPONSE_free(rsp);
if(br!=NULL){
OCSP_BASICRESP_free(br);
}
if(rsp!=NULL){
OCSP_RESPONSE_free(rsp);
}
return 0; /* Not OK */
}
#endif
+24 -8
View File
@@ -119,7 +119,9 @@ static ssize_t read_ws_opcode(struct mosquitto *mosq)
mosq->wsd.payloadlen_bytes = UINT8_MAX;
len = net__read(mosq, &hbuf, 1);
if(len <= 0) return len;
if(len <= 0){
return len;
}
if((hbuf & 0x70) != 0x00){
mosq->wsd.disconnect_reason = 0xEA;
@@ -166,7 +168,9 @@ static ssize_t read_ws_payloadlen_short(struct mosquitto *mosq)
uint8_t plen;
len = net__read(mosq, &hbuf, 1);
if(len <= 0) return len;
if(len <= 0){
return len;
}
mosq->wsd.mask = (hbuf & 0x80) >> 7;
plen = hbuf & 0x7F;
@@ -192,7 +196,9 @@ static ssize_t read_ws_payloadlen_extended(struct mosquitto *mosq)
ssize_t len;
len = net__read(mosq, hbuf, mosq->wsd.payloadlen_bytes);
if(len <= 0) return len;
if(len <= 0){
return len;
}
for(ssize_t i=0; i<len; i++){
mosq->wsd.payloadlen = (mosq->wsd.payloadlen << 8) + hbuf[i];
}
@@ -207,7 +213,9 @@ static ssize_t read_ws_mask(struct mosquitto *mosq)
ssize_t len;
len = net__read(mosq, &mosq->wsd.maskingkey[4-mosq->wsd.mask_bytes], mosq->wsd.mask_bytes);
if(len <= 0) return len;
if(len <= 0){
return len;
}
mosq->wsd.mask_bytes -= (uint8_t)len;
if(mosq->wsd.mask_bytes > 0){
errno = EAGAIN;
@@ -225,22 +233,30 @@ ssize_t net__read_ws(struct mosquitto *mosq, void *buf, size_t count)
if(mosq->wsd.payloadlen == 0){
if(mosq->wsd.opcode == UINT8_MAX){
len = read_ws_opcode(mosq);
if(len <= 0) return len;
if(len <= 0){
return len;
}
}
if(mosq->wsd.mask == UINT8_MAX){
len = read_ws_payloadlen_short(mosq);
if(len <= 0) return len;
if(len <= 0){
return len;
}
}
if(mosq->wsd.payloadlen_bytes > 0){
len = read_ws_payloadlen_extended(mosq);
if(len <= 0) return len;
if(len <= 0){
return len;
}
}
if(mosq->wsd.mask == 1 && mosq->wsd.mask_bytes > 0){
len = read_ws_mask(mosq);
if(len <= 0) return len;
if(len <= 0){
return len;
}
}
if(mosq->wsd.opcode == WS_CLOSE && mosq->wsd.payloadlen == 1){
+69 -23
View File
@@ -48,11 +48,15 @@ int mosquitto_will_set_v5(struct mosquitto *mosq, const char *topic, int payload
{
int rc;
if(!mosq) return MOSQ_ERR_INVAL;
if(!mosq){
return MOSQ_ERR_INVAL;
}
if(properties){
rc = mosquitto_property_check_all(CMD_WILL, properties);
if(rc) return rc;
if(rc){
return rc;
}
}
return will__set(mosq, topic, payloadlen, payload, qos, retain, properties);
@@ -61,7 +65,9 @@ int mosquitto_will_set_v5(struct mosquitto *mosq, const char *topic, int payload
int mosquitto_will_clear(struct mosquitto *mosq)
{
if(!mosq) return MOSQ_ERR_INVAL;
if(!mosq){
return MOSQ_ERR_INVAL;
}
return will__clear(mosq);
}
@@ -70,7 +76,9 @@ int mosquitto_username_pw_set(struct mosquitto *mosq, const char *username, cons
{
size_t slen;
if(!mosq) return MOSQ_ERR_INVAL;
if(!mosq){
return MOSQ_ERR_INVAL;
}
if(mosq->protocol == mosq_p_mqtt311 || mosq->protocol == mosq_p_mqtt31){
if(password != NULL && username == NULL){
@@ -90,7 +98,9 @@ int mosquitto_username_pw_set(struct mosquitto *mosq, const char *username, cons
return MOSQ_ERR_MALFORMED_UTF8;
}
mosq->username = mosquitto_strdup(username);
if(!mosq->username) return MOSQ_ERR_NOMEM;
if(!mosq->username){
return MOSQ_ERR_NOMEM;
}
}
if(password){
@@ -106,9 +116,13 @@ int mosquitto_username_pw_set(struct mosquitto *mosq, const char *username, cons
int mosquitto_reconnect_delay_set(struct mosquitto *mosq, unsigned int reconnect_delay, unsigned int reconnect_delay_max, bool reconnect_exponential_backoff)
{
if(!mosq) return MOSQ_ERR_INVAL;
if(!mosq){
return MOSQ_ERR_INVAL;
}
if(reconnect_delay == 0) reconnect_delay = 1;
if(reconnect_delay == 0){
reconnect_delay = 1;
}
mosq->reconnect_delay = reconnect_delay;
mosq->reconnect_delay_max = reconnect_delay_max;
@@ -123,7 +137,9 @@ int mosquitto_tls_set(struct mosquitto *mosq, const char *cafile, const char *ca
#ifdef WITH_TLS
FILE *fptr;
if(!mosq || (!cafile && !capath) || (certfile && !keyfile) || (!certfile && keyfile)) return MOSQ_ERR_INVAL;
if(!mosq || (!cafile && !capath) || (certfile && !keyfile) || (!certfile && keyfile)){
return MOSQ_ERR_INVAL;
}
mosquitto_FREE(mosq->tls_cafile);
if(cafile){
@@ -209,7 +225,9 @@ int mosquitto_tls_set(struct mosquitto *mosq, const char *cafile, const char *ca
int mosquitto_tls_opts_set(struct mosquitto *mosq, int cert_reqs, const char *tls_version, const char *ciphers)
{
#ifdef WITH_TLS
if(!mosq) return MOSQ_ERR_INVAL;
if(!mosq){
return MOSQ_ERR_INVAL;
}
mosq->tls_cert_reqs = cert_reqs;
if(tls_version){
@@ -218,19 +236,25 @@ int mosquitto_tls_opts_set(struct mosquitto *mosq, int cert_reqs, const char *tl
mosquitto_FREE(mosq->tls_version);
mosq->tls_version = mosquitto_strdup(tls_version);
if(!mosq->tls_version) return MOSQ_ERR_NOMEM;
if(!mosq->tls_version){
return MOSQ_ERR_NOMEM;
}
}else{
return MOSQ_ERR_INVAL;
}
}else{
mosquitto_FREE(mosq->tls_version);
mosq->tls_version = mosquitto_strdup("tlsv1.2");
if(!mosq->tls_version) return MOSQ_ERR_NOMEM;
if(!mosq->tls_version){
return MOSQ_ERR_NOMEM;
}
}
if(ciphers){
mosquitto_FREE(mosq->tls_ciphers);
mosq->tls_ciphers = mosquitto_strdup(ciphers);
if(!mosq->tls_ciphers) return MOSQ_ERR_NOMEM;
if(!mosq->tls_ciphers){
return MOSQ_ERR_NOMEM;
}
}else{
mosquitto_FREE(mosq->tls_ciphers);
mosq->tls_ciphers = NULL;
@@ -242,10 +266,14 @@ int mosquitto_tls_opts_set(struct mosquitto *mosq, int cert_reqs, const char *tl
if(ciphers){
if(!strcasecmp(mosq->tls_version, "tlsv1.3")){
mosq->tls_13_ciphers = mosquitto_strdup(ciphers);
if(!mosq->tls_13_ciphers) return MOSQ_ERR_NOMEM;
if(!mosq->tls_13_ciphers){
return MOSQ_ERR_NOMEM;
}
}else{
mosq->tls_ciphers = mosquitto_strdup(ciphers);
if(!mosq->tls_ciphers) return MOSQ_ERR_NOMEM;
if(!mosq->tls_ciphers){
return MOSQ_ERR_NOMEM;
}
}
}
@@ -264,7 +292,9 @@ int mosquitto_tls_opts_set(struct mosquitto *mosq, int cert_reqs, const char *tl
int mosquitto_tls_insecure_set(struct mosquitto *mosq, bool value)
{
#ifdef WITH_TLS
if(!mosq) return MOSQ_ERR_INVAL;
if(!mosq){
return MOSQ_ERR_INVAL;
}
mosq->tls_insecure = value;
return MOSQ_ERR_SUCCESS;
#else
@@ -283,7 +313,9 @@ int mosquitto_string_option(struct mosquitto *mosq, enum mosq_opt_t option, cons
char *str;
#endif
if(!mosq) return MOSQ_ERR_INVAL;
if(!mosq){
return MOSQ_ERR_INVAL;
}
switch(option){
case MOSQ_OPT_TLS_ENGINE:
@@ -311,7 +343,9 @@ int mosquitto_string_option(struct mosquitto *mosq, enum mosq_opt_t option, cons
case MOSQ_OPT_TLS_KEYFORM:
#if defined(WITH_TLS) && !defined(OPENSSL_NO_ENGINE) && OPENSSL_API_LEVEL < 30000
if(!value) return MOSQ_ERR_INVAL;
if(!value){
return MOSQ_ERR_INVAL;
}
if(!strcasecmp(value, "pem")){
mosq->tls_keyform = mosq_k_pem;
}else if(!strcasecmp(value, "engine")){
@@ -391,14 +425,18 @@ int mosquitto_string_option(struct mosquitto *mosq, enum mosq_opt_t option, cons
int mosquitto_tls_psk_set(struct mosquitto *mosq, const char *psk, const char *identity, const char *ciphers)
{
#ifdef FINAL_WITH_TLS_PSK
if(!mosq || !psk || !identity) return MOSQ_ERR_INVAL;
if(!mosq || !psk || !identity){
return MOSQ_ERR_INVAL;
}
/* Check for hex only digits */
if(strspn(psk, "0123456789abcdefABCDEF") < strlen(psk)){
return MOSQ_ERR_INVAL;
}
mosq->tls_psk = mosquitto_strdup(psk);
if(!mosq->tls_psk) return MOSQ_ERR_NOMEM;
if(!mosq->tls_psk){
return MOSQ_ERR_NOMEM;
}
mosq->tls_psk_identity = mosquitto_strdup(identity);
if(!mosq->tls_psk_identity){
@@ -407,7 +445,9 @@ int mosquitto_tls_psk_set(struct mosquitto *mosq, const char *psk, const char *i
}
if(ciphers){
mosq->tls_ciphers = mosquitto_strdup(ciphers);
if(!mosq->tls_ciphers) return MOSQ_ERR_NOMEM;
if(!mosq->tls_ciphers){
return MOSQ_ERR_NOMEM;
}
}else{
mosq->tls_ciphers = NULL;
}
@@ -428,7 +468,9 @@ int mosquitto_opts_set(struct mosquitto *mosq, enum mosq_opt_t option, void *val
{
int ival;
if(!mosq) return MOSQ_ERR_INVAL;
if(!mosq){
return MOSQ_ERR_INVAL;
}
switch(option){
case MOSQ_OPT_PROTOCOL_VERSION:
@@ -448,7 +490,9 @@ int mosquitto_opts_set(struct mosquitto *mosq, enum mosq_opt_t option, void *val
int mosquitto_int_option(struct mosquitto *mosq, enum mosq_opt_t option, int value)
{
if(!mosq) return MOSQ_ERR_INVAL;
if(!mosq){
return MOSQ_ERR_INVAL;
}
switch(option){
case MOSQ_OPT_DISABLE_SOCKETPAIR:
@@ -560,7 +604,9 @@ int mosquitto_int_option(struct mosquitto *mosq, enum mosq_opt_t option, int val
int mosquitto_void_option(struct mosquitto *mosq, enum mosq_opt_t option, void *value)
{
if(!mosq) return MOSQ_ERR_INVAL;
if(!mosq){
return MOSQ_ERR_INVAL;
}
switch(option){
case MOSQ_OPT_SSL_CTX:
+27 -9
View File
@@ -43,7 +43,9 @@ Contributors:
int packet__read_byte(struct mosquitto__packet_in *packet, uint8_t *byte)
{
assert(packet);
if(packet->pos+1 > packet->remaining_length) return MOSQ_ERR_MALFORMED_PACKET;
if(packet->pos+1 > packet->remaining_length){
return MOSQ_ERR_MALFORMED_PACKET;
}
*byte = packet->payload[packet->pos];
packet->pos++;
@@ -65,7 +67,9 @@ void packet__write_byte(struct mosquitto__packet *packet, uint8_t byte)
int packet__read_bytes(struct mosquitto__packet_in *packet, void *bytes, uint32_t count)
{
assert(packet);
if(packet->pos+count > packet->remaining_length) return MOSQ_ERR_MALFORMED_PACKET;
if(packet->pos+count > packet->remaining_length){
return MOSQ_ERR_MALFORMED_PACKET;
}
memcpy(bytes, &(packet->payload[packet->pos]), count);
packet->pos += count;
@@ -93,7 +97,9 @@ int packet__read_binary(struct mosquitto__packet_in *packet, uint8_t **data, uin
assert(packet);
rc = packet__read_uint16(packet, &slen);
if(rc) return rc;
if(rc){
return rc;
}
if(slen == 0){
*data = NULL;
@@ -101,7 +107,9 @@ int packet__read_binary(struct mosquitto__packet_in *packet, uint8_t **data, uin
return MOSQ_ERR_SUCCESS;
}
if(packet->pos+slen > packet->remaining_length) return MOSQ_ERR_MALFORMED_PACKET;
if(packet->pos+slen > packet->remaining_length){
return MOSQ_ERR_MALFORMED_PACKET;
}
*data = mosquitto_malloc(slen+1U);
if(*data){
@@ -122,8 +130,12 @@ int packet__read_string(struct mosquitto__packet_in *packet, char **str, uint16_
int rc;
rc = packet__read_binary(packet, (uint8_t **)str, length);
if(rc) return rc;
if(*length == 0) return MOSQ_ERR_SUCCESS;
if(rc){
return rc;
}
if(*length == 0){
return MOSQ_ERR_SUCCESS;
}
if(mosquitto_validate_utf8(*str, *length)){
mosquitto_FREE(*str);
@@ -148,7 +160,9 @@ int packet__read_uint16(struct mosquitto__packet_in *packet, uint16_t *word)
uint16_t val;
assert(packet);
if(packet->pos+2 > packet->remaining_length) return MOSQ_ERR_MALFORMED_PACKET;
if(packet->pos+2 > packet->remaining_length){
return MOSQ_ERR_MALFORMED_PACKET;
}
memcpy(&val, &packet->payload[packet->pos], sizeof(uint16_t));
packet->pos += sizeof(uint16_t);
@@ -176,7 +190,9 @@ int packet__read_uint32(struct mosquitto__packet_in *packet, uint32_t *word)
uint32_t val = 0;
assert(packet);
if(packet->pos+4 > packet->remaining_length) return MOSQ_ERR_MALFORMED_PACKET;
if(packet->pos+4 > packet->remaining_length){
return MOSQ_ERR_MALFORMED_PACKET;
}
memcpy(&val, &packet->payload[packet->pos], sizeof(uint32_t));
packet->pos += sizeof(uint32_t);
@@ -220,7 +236,9 @@ int packet__read_varint(struct mosquitto__packet_in *packet, uint32_t *word, uin
return MOSQ_ERR_MALFORMED_PACKET;
}else{
*word = lword;
if(bytes) (*bytes) = lbytes;
if(bytes){
(*bytes) = lbytes;
}
return MOSQ_ERR_SUCCESS;
}
}
+24 -8
View File
@@ -68,11 +68,15 @@ int packet__alloc(struct mosquitto__packet **packet, uint8_t command, uint32_t r
remaining_bytes[remaining_count] = byte;
remaining_count++;
}while(remaining_length > 0 && remaining_count < 5);
if(remaining_count == 5) return MOSQ_ERR_PAYLOAD_SIZE;
if(remaining_count == 5){
return MOSQ_ERR_PAYLOAD_SIZE;
}
packet_length = remaining_length_stored + 1 + (uint8_t)remaining_count;
(*packet) = mosquitto_malloc(sizeof(struct mosquitto__packet) + packet_length + WS_PACKET_OFFSET);
if((*packet) == NULL) return MOSQ_ERR_NOMEM;
if((*packet) == NULL){
return MOSQ_ERR_NOMEM;
}
/* Clear memory for everything but the payload - that will be set to valid
* values when the actual payload is copied in. */
@@ -94,7 +98,9 @@ int packet__alloc(struct mosquitto__packet **packet, uint8_t command, uint32_t r
void packet__cleanup(struct mosquitto__packet_in *packet)
{
if(!packet) return;
if(!packet){
return;
}
/* Free data and reset values */
packet->command = 0;
@@ -228,7 +234,9 @@ int packet__check_oversize(struct mosquitto *mosq, uint32_t remaining_length)
{
uint32_t len;
if(mosq->maximum_packet_size == 0) return MOSQ_ERR_SUCCESS;
if(mosq->maximum_packet_size == 0){
return MOSQ_ERR_SUCCESS;
}
len = remaining_length + mosquitto_varint_bytes(remaining_length);
if(len > mosq->maximum_packet_size){
@@ -267,7 +275,9 @@ int packet__write(struct mosquitto *mosq)
struct mosquitto__packet *packet, *next_packet;
enum mosquitto_client_state state;
if(!mosq) return MOSQ_ERR_INVAL;
if(!mosq){
return MOSQ_ERR_INVAL;
}
if(!net__is_connected(mosq)){
return MOSQ_ERR_NO_CONN;
}
@@ -391,7 +401,9 @@ static int packet__read_single(struct mosquitto *mosq, enum mosquitto_client_sta
if(!mosq->in_packet.command){
if(mosq->in_packet.packet_buffer_to_process == 0){
rc = read_header(mosq, local__read);
if(rc) return rc;
if(rc){
return rc;
}
}
if(mosq->in_packet.packet_buffer_to_process > 0){
@@ -429,7 +441,9 @@ static int packet__read_single(struct mosquitto *mosq, enum mosquitto_client_sta
do{
if(mosq->in_packet.packet_buffer_to_process == 0){
rc = read_header(mosq, local__read);
if(rc) return rc;
if(rc){
return rc;
}
}
if(mosq->in_packet.packet_buffer_to_process > 0){
@@ -650,7 +664,9 @@ int packet__read(struct mosquitto *mosq)
return MOSQ_ERR_SUCCESS;
}
rc = packet__read_single(mosq, state, local__read);
if(rc) return rc;
if(rc){
return rc;
}
}while(mosq->in_packet.packet_buffer_to_process > 0);
return MOSQ_ERR_SUCCESS;
+36 -12
View File
@@ -44,7 +44,9 @@ static int property__read(struct mosquitto__packet_in *packet, uint32_t *len, mo
char *str1, *str2;
uint16_t slen1, slen2;
if(!property) return MOSQ_ERR_INVAL;
if(!property){
return MOSQ_ERR_INVAL;
}
rc = packet__read_varint(packet, &property_identifier, NULL);
if(rc){
@@ -66,7 +68,9 @@ static int property__read(struct mosquitto__packet_in *packet, uint32_t *len, mo
case MQTT_PROP_SUBSCRIPTION_ID_AVAILABLE:
case MQTT_PROP_SHARED_SUB_AVAILABLE:
rc = packet__read_byte(packet, &byte);
if(rc) return rc;
if(rc){
return rc;
}
*len -= 1; /* byte */
property->value.i8 = byte;
property->property_type = MQTT_PROP_TYPE_BYTE;
@@ -77,7 +81,9 @@ static int property__read(struct mosquitto__packet_in *packet, uint32_t *len, mo
case MQTT_PROP_TOPIC_ALIAS_MAXIMUM:
case MQTT_PROP_TOPIC_ALIAS:
rc = packet__read_uint16(packet, &uint16);
if(rc) return rc;
if(rc){
return rc;
}
*len -= 2; /* uint16 */
property->value.i16 = uint16;
property->property_type = MQTT_PROP_TYPE_INT16;
@@ -88,7 +94,9 @@ static int property__read(struct mosquitto__packet_in *packet, uint32_t *len, mo
case MQTT_PROP_WILL_DELAY_INTERVAL:
case MQTT_PROP_MAXIMUM_PACKET_SIZE:
rc = packet__read_uint32(packet, &uint32);
if(rc) return rc;
if(rc){
return rc;
}
*len -= 4; /* uint32 */
property->value.i32 = uint32;
property->property_type = MQTT_PROP_TYPE_INT32;
@@ -96,7 +104,9 @@ static int property__read(struct mosquitto__packet_in *packet, uint32_t *len, mo
case MQTT_PROP_SUBSCRIPTION_IDENTIFIER:
rc = packet__read_varint(packet, &varint, &byte_count);
if(rc) return rc;
if(rc){
return rc;
}
*len -= byte_count;
property->value.varint = varint;
property->property_type = MQTT_PROP_TYPE_VARINT;
@@ -110,7 +120,9 @@ static int property__read(struct mosquitto__packet_in *packet, uint32_t *len, mo
case MQTT_PROP_SERVER_REFERENCE:
case MQTT_PROP_REASON_STRING:
rc = packet__read_string(packet, &str1, &slen1);
if(rc) return rc;
if(rc){
return rc;
}
*len = (*len) - 2 - slen1; /* uint16, string len */
property->value.s.v = str1;
property->value.s.len = slen1;
@@ -120,7 +132,9 @@ static int property__read(struct mosquitto__packet_in *packet, uint32_t *len, mo
case MQTT_PROP_AUTHENTICATION_DATA:
case MQTT_PROP_CORRELATION_DATA:
rc = packet__read_binary(packet, (uint8_t **)&str1, &slen1);
if(rc) return rc;
if(rc){
return rc;
}
*len = (*len) - 2 - slen1; /* uint16, binary len */
property->value.bin.v = str1;
property->value.bin.len = slen1;
@@ -129,7 +143,9 @@ static int property__read(struct mosquitto__packet_in *packet, uint32_t *len, mo
case MQTT_PROP_USER_PROPERTY:
rc = packet__read_string(packet, &str1, &slen1);
if(rc) return rc;
if(rc){
return rc;
}
*len = (*len) - 2 - slen1; /* uint16, string len */
rc = packet__read_string(packet, &str2, &slen2);
@@ -164,7 +180,9 @@ int property__read_all(int command, struct mosquitto__packet_in *packet, mosquit
mosquitto_property *p, *tail = NULL;
rc = packet__read_varint(packet, &proplen, NULL);
if(rc) return rc;
if(rc){
return rc;
}
*properties = NULL;
@@ -207,7 +225,9 @@ static int property__write(struct mosquitto__packet *packet, const mosquitto_pro
int rc;
rc = packet__write_varint(packet, (uint32_t)mosquitto_property_identifier(property));
if(rc) return rc;
if(rc){
return rc;
}
switch(property->property_type){
case MQTT_PROP_TYPE_BYTE:
@@ -257,13 +277,17 @@ int property__write_all(struct mosquitto__packet *packet, const mosquitto_proper
if(write_len){
rc = packet__write_varint(packet, mosquitto_property_get_length_all(properties));
if(rc) return rc;
if(rc){
return rc;
}
}
p = properties;
while(p){
rc = property__write(packet, p);
if(rc) return rc;
if(rc){
return rc;
}
p = p->next;
}
+6 -2
View File
@@ -51,7 +51,9 @@ int send__connect(struct mosquitto *mosq, uint16_t keepalive, bool clean_session
assert(mosq);
if(mosq->protocol == mosq_p_mqtt31 && !mosq->id) return MOSQ_ERR_PROTOCOL;
if(mosq->protocol == mosq_p_mqtt31 && !mosq->id){
return MOSQ_ERR_PROTOCOL;
}
#if defined(WITH_BROKER) && defined(WITH_BRIDGE)
if(mosq->bridge){
@@ -73,7 +75,9 @@ int send__connect(struct mosquitto *mosq, uint16_t keepalive, bool clean_session
/* Generate properties from options */
if(!mosquitto_property_read_int16(properties, MQTT_PROP_RECEIVE_MAXIMUM, &receive_maximum, false)){
rc = mosquitto_property_add_int16(&local_props, MQTT_PROP_RECEIVE_MAXIMUM, mosq->msgs_in.inflight_maximum);
if(rc) return rc;
if(rc){
return rc;
}
}else{
mosq->msgs_in.inflight_maximum = receive_maximum;
mosq->msgs_in.inflight_quota = receive_maximum;
+36 -12
View File
@@ -55,7 +55,9 @@ int send__publish(struct mosquitto *mosq, uint16_t mid, const char *topic, uint3
#endif
assert(mosq);
if(!net__is_connected(mosq)) return MOSQ_ERR_NO_CONN;
if(!net__is_connected(mosq)){
return MOSQ_ERR_NO_CONN;
}
#ifdef WITH_BROKER
bool payload_changed = false;
@@ -73,9 +75,15 @@ int send__publish(struct mosquitto *mosq, uint16_t mid, const char *topic, uint3
rc = plugin__handle_message_out(mosq, &tmp_msg);
if(tmp_msg.payload != payload) payload_changed = true;
if(tmp_msg.topic != topic) topic_changed = true;
if(tmp_msg.properties != store_props) properties_changed = true;
if(tmp_msg.payload != payload){
payload_changed = true;
}
if(tmp_msg.topic != topic){
topic_changed = true;
}
if(tmp_msg.properties != store_props){
properties_changed = true;
}
topic = tmp_msg.topic;
payloadlen = tmp_msg.payloadlen;
@@ -95,9 +103,15 @@ int send__publish(struct mosquitto *mosq, uint16_t mid, const char *topic, uint3
"Rejected PUBLISH to %s, quota exceeded.", mosq->id);
}
if(payload_changed) mosquitto_free((void *)payload);
if(topic_changed) mosquitto_free((char *)topic);
if(properties_changed) mosquitto_property_free_all((mosquitto_property **)&store_props);
if(payload_changed){
mosquitto_free((void *)payload);
}
if(topic_changed){
mosquitto_free((void *)topic);
}
if(properties_changed){
mosquitto_property_free_all((mosquitto_property **)&store_props);
}
return MOSQ_ERR_SUCCESS;
}
@@ -131,7 +145,9 @@ int send__publish(struct mosquitto *mosq, uint16_t mid, const char *topic, uint3
}
if(match){
mapped_topic = mosquitto_strdup(topic);
if(!mapped_topic) return MOSQ_ERR_NOMEM;
if(!mapped_topic){
return MOSQ_ERR_NOMEM;
}
if(cur_topic->local_prefix){
/* This prefix needs removing. */
if(!strncmp(cur_topic->local_prefix, mapped_topic, strlen(cur_topic->local_prefix))){
@@ -175,9 +191,15 @@ int send__publish(struct mosquitto *mosq, uint16_t mid, const char *topic, uint3
#ifdef WITH_BROKER
rc = send__real_publish(mosq, mid, topic, payloadlen, payload, qos, retain, dup, subscription_identifier, store_props, expiry_interval);
if(payload_changed) mosquitto_free((void *)payload);
if(topic_changed) mosquitto_free((char *)topic);
if(properties_changed) mosquitto_property_free_all((mosquitto_property **)&store_props);
if(payload_changed){
mosquitto_free((void *)payload);
}
if(topic_changed){
mosquitto_free((void *)topic);
}
if(properties_changed){
mosquitto_property_free_all((mosquitto_property **)&store_props);
}
return rc;
#else
return send__real_publish(mosq, mid, topic, payloadlen, payload, qos, retain, dup, subscription_identifier, store_props, expiry_interval);
@@ -223,7 +245,9 @@ int send__real_publish(struct mosquitto *mosq, uint16_t mid, const char *topic,
}else{
packetlen = 2 + payloadlen;
}
if(qos > 0) packetlen += 2; /* For message id */
if(qos > 0){
packetlen += 2; /* For message id */
}
if(mosq->protocol == mosq_p_mqtt5){
proplen = 0;
proplen += mosquitto_property_get_length_all(store_props);
+3 -1
View File
@@ -68,7 +68,9 @@ int send__subscribe(struct mosquitto *mosq, int *mid, int topic_count, char *con
/* Variable header */
local_mid = mosquitto__mid_generate(mosq);
if(mid) *mid = (int)local_mid;
if(mid){
*mid = (int)local_mid;
}
packet__write_uint16(packet, local_mid);
if(mosq->protocol == mosq_p_mqtt5){
+3 -1
View File
@@ -67,7 +67,9 @@ int send__unsubscribe(struct mosquitto *mosq, int *mid, int topic_count, char *c
/* Variable header */
local_mid = mosquitto__mid_generate(mosq);
if(mid) *mid = (int)local_mid;
if(mid){
*mid = (int)local_mid;
}
packet__write_uint16(packet, local_mid);
if(mosq->protocol == mosq_p_mqtt5){

Some files were not shown because too many files have changed in this diff Show More