diff --git a/ft/tag_test.robot b/ft/tag_test.robot index 7a0649b9b..5ca206dfc 100644 --- a/ft/tag_test.robot +++ b/ft/tag_test.robot @@ -133,8 +133,8 @@ Update tag, it should return success. Delete tag from non-existent group, it should return success ${res}= Del Tags modbus-node group1 "tag1" - Check Response Status ${res} 200 - Check Error Code ${res} ${NEU_ERR_SUCCESS} + Check Response Status ${res} 404 + Check Error Code ${res} ${NEU_ERR_GROUP_NOT_EXIST} Update non-existent tag, it should return failure. ${res}= Update Tags modbus-node group ${tag4} diff --git a/include/neuron/define.h b/include/neuron/define.h index c07ba4139..fe016a525 100644 --- a/include/neuron/define.h +++ b/include/neuron/define.h @@ -62,6 +62,35 @@ #define NEU_LOG_LEVEL_ERROR "error" #define NEU_LOG_LEVEL_FATAL "fatal" +#define CHECK_NODE_NAME_LENGTH_ERR \ + do { \ + NEU_JSON_RESPONSE_ERROR(NEU_ERR_NODE_NAME_TOO_LONG, { \ + neu_http_response(aio, NEU_ERR_NODE_NAME_TOO_LONG, result_error); \ + }); \ + } while (0) + +#define CHECK_GROUP_NAME_LENGTH_ERR \ + do { \ + NEU_JSON_RESPONSE_ERROR(NEU_ERR_GROUP_NAME_TOO_LONG, { \ + neu_http_response(aio, NEU_ERR_GROUP_NAME_TOO_LONG, result_error); \ + }); \ + } while (0) + +#define CHECK_TAG_NAME_LENGTH_ERR \ + do { \ + NEU_JSON_RESPONSE_ERROR(NEU_ERR_TAG_NAME_TOO_LONG, { \ + neu_http_response(aio, NEU_ERR_TAG_NAME_TOO_LONG, result_error); \ + }); \ + } while (0) + +#define CHECK_GROUP_INTERVAL_ERR \ + do { \ + NEU_JSON_RESPONSE_ERROR(NEU_ERR_GROUP_PARAMETER_INVALID, { \ + neu_http_response(aio, NEU_ERR_GROUP_PARAMETER_INVALID, \ + result_error); \ + }); \ + } while (0) + extern int default_log_level; extern bool disable_jwt; extern char host_port[32]; diff --git a/plugins/restful/adapter_handle.c b/plugins/restful/adapter_handle.c index 8e2ec122d..48260d913 100644 --- a/plugins/restful/adapter_handle.c +++ b/plugins/restful/adapter_handle.c @@ -43,11 +43,7 @@ void handle_add_adapter(nng_aio *aio) NEU_PROCESS_HTTP_REQUEST_VALIDATE_JWT( aio, neu_json_add_node_req_t, neu_json_decode_add_node_req, { if (strlen(req->name) >= NEU_NODE_NAME_LEN) { - NEU_JSON_RESPONSE_ERROR(NEU_ERR_NODE_NAME_TOO_LONG, { - neu_http_response(aio, NEU_ERR_NODE_NAME_TOO_LONG, - result_error); - }); - + CHECK_NODE_NAME_LENGTH_ERR; } else { int ret = 0; neu_reqresp_head_t header = { 0 }; @@ -121,18 +117,22 @@ void handle_del_adapter(nng_aio *aio) NEU_PROCESS_HTTP_REQUEST_VALIDATE_JWT( aio, neu_json_del_node_req_t, neu_json_decode_del_node_req, { - int ret = 0; - neu_reqresp_head_t header = { 0 }; - neu_req_del_node_t cmd = { 0 }; - - header.ctx = aio; - header.type = NEU_REQ_DEL_NODE; - strcpy(cmd.node, req->name); - ret = neu_plugin_op(plugin, header, &cmd); - if (ret != 0) { - NEU_JSON_RESPONSE_ERROR(NEU_ERR_IS_BUSY, { - neu_http_response(aio, NEU_ERR_IS_BUSY, result_error); - }); + if (strlen(req->name) >= NEU_NODE_NAME_LEN) { + CHECK_NODE_NAME_LENGTH_ERR; + } else { + int ret = 0; + neu_reqresp_head_t header = { 0 }; + neu_req_del_node_t cmd = { 0 }; + + header.ctx = aio; + header.type = NEU_REQ_DEL_NODE; + strcpy(cmd.node, req->name); + ret = neu_plugin_op(plugin, header, &cmd); + if (ret != 0) { + NEU_JSON_RESPONSE_ERROR(NEU_ERR_IS_BUSY, { + neu_http_response(aio, NEU_ERR_IS_BUSY, result_error); + }); + } } }) } @@ -210,20 +210,24 @@ void handle_set_node_setting(nng_aio *aio) NEU_PROCESS_HTTP_REQUEST_VALIDATE_JWT( aio, neu_json_node_setting_req_t, neu_json_decode_node_setting_req, { - int ret = 0; - neu_reqresp_head_t header = { 0 }; - neu_req_node_setting_t cmd = { 0 }; - - header.ctx = aio; - header.type = NEU_REQ_NODE_SETTING; - strcpy(cmd.node, req->node); - cmd.setting = req->setting; - req->setting = NULL; // ownership moved - ret = neu_plugin_op(plugin, header, &cmd); - if (ret != 0) { - NEU_JSON_RESPONSE_ERROR(NEU_ERR_IS_BUSY, { - neu_http_response(aio, NEU_ERR_IS_BUSY, result_error); - }); + if (strlen(req->node) >= NEU_NODE_NAME_LEN) { + CHECK_NODE_NAME_LENGTH_ERR; + } else { + int ret = 0; + neu_reqresp_head_t header = { 0 }; + neu_req_node_setting_t cmd = { 0 }; + + header.ctx = aio; + header.type = NEU_REQ_NODE_SETTING; + strcpy(cmd.node, req->node); + cmd.setting = req->setting; + req->setting = NULL; // ownership moved + ret = neu_plugin_op(plugin, header, &cmd); + if (ret != 0) { + NEU_JSON_RESPONSE_ERROR(NEU_ERR_IS_BUSY, { + neu_http_response(aio, NEU_ERR_IS_BUSY, result_error); + }); + } } }) } @@ -287,20 +291,24 @@ void handle_node_ctl(nng_aio *aio) NEU_PROCESS_HTTP_REQUEST_VALIDATE_JWT( aio, neu_json_node_ctl_req_t, neu_json_decode_node_ctl_req, { - int ret = 0; - neu_reqresp_head_t header = { 0 }; - neu_req_node_ctl_t cmd = { 0 }; + if (strlen(req->node) >= NEU_NODE_NAME_LEN) { + CHECK_NODE_NAME_LENGTH_ERR; + } else { + int ret = 0; + neu_reqresp_head_t header = { 0 }; + neu_req_node_ctl_t cmd = { 0 }; - header.ctx = aio; - header.type = NEU_REQ_NODE_CTL; - strcpy(cmd.node, req->node); - cmd.ctl = req->cmd; + header.ctx = aio; + header.type = NEU_REQ_NODE_CTL; + strcpy(cmd.node, req->node); + cmd.ctl = req->cmd; - ret = neu_plugin_op(plugin, header, &cmd); - if (ret != 0) { - NEU_JSON_RESPONSE_ERROR(NEU_ERR_IS_BUSY, { - neu_http_response(aio, NEU_ERR_IS_BUSY, result_error); - }); + ret = neu_plugin_op(plugin, header, &cmd); + if (ret != 0) { + NEU_JSON_RESPONSE_ERROR(NEU_ERR_IS_BUSY, { + neu_http_response(aio, NEU_ERR_IS_BUSY, result_error); + }); + } } }) } diff --git a/plugins/restful/datatag_handle.c b/plugins/restful/datatag_handle.c index ccbb74193..61a4419a5 100644 --- a/plugins/restful/datatag_handle.c +++ b/plugins/restful/datatag_handle.c @@ -36,11 +36,10 @@ void handle_add_tags(nng_aio *aio) NEU_PROCESS_HTTP_REQUEST_VALIDATE_JWT( aio, neu_json_add_tags_req_t, neu_json_decode_add_tags_req, { - if (strlen(req->group) >= NEU_GROUP_NAME_LEN) { - NEU_JSON_RESPONSE_ERROR(NEU_ERR_GROUP_NAME_TOO_LONG, { - neu_http_response(aio, NEU_ERR_GROUP_NAME_TOO_LONG, - result_error); - }); + if (strlen(req->node) >= NEU_NODE_NAME_LEN) { + CHECK_NODE_NAME_LENGTH_ERR; + } else if (strlen(req->group) >= NEU_GROUP_NAME_LEN) { + CHECK_GROUP_NAME_LENGTH_ERR; } else { int ret = 0; neu_reqresp_head_t header = { 0 }; @@ -110,6 +109,11 @@ void handle_add_gtags(nng_aio *aio) header.ctx = aio; header.type = NEU_REQ_ADD_GTAG; + if (strlen(req->node) >= NEU_NODE_NAME_LEN) { + err_type = NEU_ERR_NODE_NAME_TOO_LONG; + goto error; + } + for (int i = 0; i < req->n_group; i++) { if (strlen(req->groups[i].group) >= NEU_GROUP_NAME_LEN) { err_type = NEU_ERR_GROUP_NAME_TOO_LONG; @@ -123,8 +127,8 @@ void handle_add_gtags(nng_aio *aio) strcpy(cmd.driver, req->node); cmd.n_group = req->n_group; + cmd.groups = calloc(req->n_group, sizeof(neu_gdatatag_t)); - cmd.groups = calloc(req->n_group, sizeof(neu_gdatatag_t)); for (int i = 0; i < req->n_group; i++) { strcpy(cmd.groups[i].group, req->groups[i].group); cmd.groups[i].n_tag = req->groups[i].n_tag; @@ -166,11 +170,9 @@ void handle_add_gtags(nng_aio *aio) }); } goto success; - error: NEU_JSON_RESPONSE_ERROR( err_type, { neu_http_response(aio, err_type, result_error); }); - success:; }) } @@ -179,9 +181,8 @@ void handle_add_gtags_resp(nng_aio *aio, neu_resp_add_tag_t *resp) { neu_json_add_gtag_res_t res = { 0 }; char * result = NULL; - - res.error = resp->error; - res.index = resp->index; + res.error = resp->error; + res.index = resp->index; neu_json_encode_by_fn(&res, neu_json_encode_au_gtags_resp, &result); NEU_JSON_RESPONSE_ERROR(resp->error, @@ -198,13 +199,33 @@ void handle_del_tags(nng_aio *aio) int ret = 0; neu_reqresp_head_t header = { 0 }; neu_req_del_tag_t cmd = { 0 }; + int err_type; header.ctx = aio; header.type = NEU_REQ_DEL_TAG; + + if (strlen(req->node) >= NEU_NODE_NAME_LEN) { + err_type = NEU_ERR_NODE_NAME_TOO_LONG; + goto error; + } + + if (strlen(req->group) >= NEU_GROUP_NAME_LEN) { + err_type = NEU_ERR_GROUP_NAME_TOO_LONG; + goto error; + } + strcpy(cmd.driver, req->node); strcpy(cmd.group, req->group); cmd.n_tag = req->n_tags; - cmd.tags = calloc(req->n_tags, sizeof(char *)); + + for (int i = 0; i < req->n_tags; i++) { + if (strlen(req->tags[i]) >= NEU_TAG_ADDRESS_LEN) { + err_type = NEU_ERR_TAG_NAME_TOO_LONG; + goto error; + } + } + + cmd.tags = calloc(req->n_tags, sizeof(char *)); for (int i = 0; i < req->n_tags; i++) { cmd.tags[i] = strdup(req->tags[i]); @@ -216,6 +237,12 @@ void handle_del_tags(nng_aio *aio) neu_http_response(aio, NEU_ERR_IS_BUSY, result_error); }); } + goto success; + + error: + NEU_JSON_RESPONSE_ERROR( + err_type, { neu_http_response(aio, err_type, result_error); }); + success:; }) } @@ -231,6 +258,7 @@ void handle_update_tags(nng_aio *aio) header.ctx = aio; header.type = NEU_REQ_UPDATE_TAG; + strcpy(cmd.driver, req->node); strcpy(cmd.group, req->group); cmd.n_tag = req->n_tag; @@ -345,4 +373,4 @@ void handle_get_tags_resp(nng_aio *aio, neu_resp_get_tag_t *tags) free(result); free(tags_res.tags); utarray_free(tags->tags); -} +} \ No newline at end of file diff --git a/plugins/restful/group_config_handle.c b/plugins/restful/group_config_handle.c index 59b83780f..cbf2f43ad 100644 --- a/plugins/restful/group_config_handle.c +++ b/plugins/restful/group_config_handle.c @@ -32,27 +32,21 @@ void handle_add_group_config(nng_aio *aio) { neu_plugin_t *plugin = neu_rest_get_plugin(); - NEU_PROCESS_HTTP_REQUEST_VALIDATE_JWT( aio, neu_json_add_group_config_req_t, neu_json_decode_add_group_config_req, { - if (strlen(req->group) >= NEU_GROUP_NAME_LEN) { - NEU_JSON_RESPONSE_ERROR(NEU_ERR_GROUP_NAME_TOO_LONG, { - neu_http_response(aio, NEU_ERR_GROUP_NAME_TOO_LONG, - result_error); - }); + if (strlen(req->node) >= NEU_NODE_NAME_LEN) { + CHECK_NODE_NAME_LENGTH_ERR; + } else if (strlen(req->group) >= NEU_GROUP_NAME_LEN) { + CHECK_GROUP_NAME_LENGTH_ERR; } else if (req->interval < NEU_GROUP_INTERVAL_LIMIT) { - NEU_JSON_RESPONSE_ERROR(NEU_ERR_GROUP_PARAMETER_INVALID, { - neu_http_response(aio, NEU_ERR_GROUP_PARAMETER_INVALID, - result_error); - }); + CHECK_GROUP_INTERVAL_ERR; } else { int ret = 0; neu_reqresp_head_t header = { 0 }; neu_req_add_group_t cmd = { 0 }; - - header.ctx = aio; - header.type = NEU_REQ_ADD_GROUP; + header.ctx = aio; + header.type = NEU_REQ_ADD_GROUP; strcpy(cmd.driver, req->node); strcpy(cmd.group, req->group); cmd.interval = req->interval; @@ -135,23 +129,28 @@ void handle_update_group(nng_aio *aio) void handle_del_group_config(nng_aio *aio) { neu_plugin_t *plugin = neu_rest_get_plugin(); - NEU_PROCESS_HTTP_REQUEST_VALIDATE_JWT( aio, neu_json_del_group_config_req_t, neu_json_decode_del_group_config_req, { - int ret = 0; - neu_reqresp_head_t header = { 0 }; - neu_req_del_group_t cmd = { 0 }; + if (strlen(req->node) >= NEU_NODE_NAME_LEN) { + CHECK_NODE_NAME_LENGTH_ERR; + } else if (strlen(req->group) >= NEU_GROUP_NAME_LEN) { + CHECK_GROUP_NAME_LENGTH_ERR; + } else { + int ret = 0; + neu_reqresp_head_t header = { 0 }; + neu_req_del_group_t cmd = { 0 }; - header.ctx = aio; - header.type = NEU_REQ_DEL_GROUP; - strcpy(cmd.driver, req->node); - strcpy(cmd.group, req->group); - ret = neu_plugin_op(plugin, header, &cmd); - if (ret != 0) { - NEU_JSON_RESPONSE_ERROR(NEU_ERR_IS_BUSY, { - neu_http_response(aio, NEU_ERR_IS_BUSY, result_error); - }); + header.ctx = aio; + header.type = NEU_REQ_DEL_GROUP; + strcpy(cmd.driver, req->node); + strcpy(cmd.group, req->group); + ret = neu_plugin_op(plugin, header, &cmd); + if (ret != 0) { + NEU_JSON_RESPONSE_ERROR(NEU_ERR_IS_BUSY, { + neu_http_response(aio, NEU_ERR_IS_BUSY, result_error); + }); + } } }) } @@ -250,6 +249,18 @@ static inline int send_subscribe(nng_aio *aio, neu_reqresp_type_e type, .type = type, }; + if (strlen(req->app) >= NEU_NODE_NAME_LEN) { + return NEU_ERR_NODE_NAME_TOO_LONG; + } + + if (strlen(req->driver) >= NEU_NODE_NAME_LEN) { + return NEU_ERR_NODE_NAME_TOO_LONG; + } + + if (strlen(req->group) >= NEU_GROUP_NAME_LEN) { + return NEU_ERR_GROUP_NAME_TOO_LONG; + } + neu_req_subscribe_t cmd = { 0 }; strcpy(cmd.app, req->app); strcpy(cmd.driver, req->driver); @@ -291,14 +302,30 @@ void handle_grp_update_subscribe(nng_aio *aio) void handle_grp_unsubscribe(nng_aio *aio) { neu_plugin_t *plugin = neu_rest_get_plugin(); - NEU_PROCESS_HTTP_REQUEST_VALIDATE_JWT( aio, neu_json_unsubscribe_req_t, neu_json_decode_unsubscribe_req, { int ret = 0; neu_req_unsubscribe_t cmd = { 0 }; neu_reqresp_head_t header = { 0 }; - header.ctx = aio; - header.type = NEU_REQ_UNSUBSCRIBE_GROUP; + int err_type; + header.ctx = aio; + header.type = NEU_REQ_UNSUBSCRIBE_GROUP; + + if (strlen(req->app) >= NEU_NODE_NAME_LEN) { + err_type = NEU_ERR_NODE_NAME_TOO_LONG; + goto error; + } + + if (strlen(req->driver) >= NEU_NODE_NAME_LEN) { + err_type = NEU_ERR_NODE_NAME_TOO_LONG; + goto error; + } + + if (strlen(req->group) >= NEU_GROUP_NAME_LEN) { + err_type = NEU_ERR_GROUP_NAME_TOO_LONG; + goto error; + } + strcpy(cmd.app, req->app); strcpy(cmd.driver, req->driver); strcpy(cmd.group, req->group); @@ -308,6 +335,12 @@ void handle_grp_unsubscribe(nng_aio *aio) neu_http_response(aio, NEU_ERR_IS_BUSY, result_error); }); } + goto success; + + error: + NEU_JSON_RESPONSE_ERROR( + err_type, { neu_http_response(aio, err_type, result_error); }); + success:; }) } @@ -321,6 +354,20 @@ static inline int send_subscribe_groups(nng_aio * aio, .type = NEU_REQ_SUBSCRIBE_GROUPS, }; + if (strlen(req->app) >= NEU_NODE_NAME_LEN) { + return NEU_ERR_NODE_NAME_TOO_LONG; + } + + for (int i = 0; i < req->n_group; i++) { + if (strlen(req->groups[i].driver) >= NEU_NODE_NAME_LEN) { + return NEU_ERR_NODE_NAME_TOO_LONG; + } + + if (strlen(req->groups[i].group) >= NEU_GROUP_NAME_LEN) { + return NEU_ERR_GROUP_NAME_TOO_LONG; + } + } + neu_req_subscribe_groups_t cmd = { .app = req->app, .n_group = req->n_group, diff --git a/plugins/restful/log_handle.c b/plugins/restful/log_handle.c index 81b3ca66f..105bad8bd 100644 --- a/plugins/restful/log_handle.c +++ b/plugins/restful/log_handle.c @@ -49,46 +49,52 @@ void handle_log_level(nng_aio *aio) NEU_PROCESS_HTTP_REQUEST_VALIDATE_JWT( aio, neu_json_update_log_level_req_t, neu_json_decode_update_log_level_req, { - neu_reqresp_head_t header = { 0 }; - neu_req_update_log_level_t cmd = { 0 }; - int log_level = -1; - - if (0 == strcmp(req->log_level, NEU_LOG_LEVEL_DEBUG)) { - log_level = ZLOG_LEVEL_DEBUG; - } else if (0 == strcmp(req->log_level, NEU_LOG_LEVEL_INFO)) { - log_level = ZLOG_LEVEL_INFO; - } else if (0 == strcmp(req->log_level, NEU_LOG_LEVEL_NOTICE)) { - log_level = ZLOG_LEVEL_NOTICE; - } else if (0 == strcmp(req->log_level, NEU_LOG_LEVEL_WARN)) { - log_level = ZLOG_LEVEL_WARN; - } else if (0 == strcmp(req->log_level, NEU_LOG_LEVEL_ERROR)) { - log_level = ZLOG_LEVEL_ERROR; - } else if (0 == strcmp(req->log_level, NEU_LOG_LEVEL_FATAL)) { - log_level = ZLOG_LEVEL_FATAL; + if (req->node_name != NULL && + strlen(req->node_name) > NEU_NODE_NAME_LEN) { + CHECK_NODE_NAME_LENGTH_ERR; } else { - nlog_error("Failed to modify log_level of the node, " - "node_name:%s, log_level: %s", - req->node_name, req->log_level); - } + neu_reqresp_head_t header = { 0 }; + neu_req_update_log_level_t cmd = { 0 }; + int log_level = -1; - if (log_level == -1) { - NEU_JSON_RESPONSE_ERROR(NEU_ERR_PARAM_IS_WRONG, { - neu_http_response(aio, error_code.error, result_error); - }); - } else { - header.ctx = aio; - header.type = NEU_REQ_UPDATE_LOG_LEVEL; - cmd.core = req->core; - cmd.log_level = log_level; - if (req->node_name != NULL) { - strcpy(cmd.node, req->node_name); + if (0 == strcmp(req->log_level, NEU_LOG_LEVEL_DEBUG)) { + log_level = ZLOG_LEVEL_DEBUG; + } else if (0 == strcmp(req->log_level, NEU_LOG_LEVEL_INFO)) { + log_level = ZLOG_LEVEL_INFO; + } else if (0 == strcmp(req->log_level, NEU_LOG_LEVEL_NOTICE)) { + log_level = ZLOG_LEVEL_NOTICE; + } else if (0 == strcmp(req->log_level, NEU_LOG_LEVEL_WARN)) { + log_level = ZLOG_LEVEL_WARN; + } else if (0 == strcmp(req->log_level, NEU_LOG_LEVEL_ERROR)) { + log_level = ZLOG_LEVEL_ERROR; + } else if (0 == strcmp(req->log_level, NEU_LOG_LEVEL_FATAL)) { + log_level = ZLOG_LEVEL_FATAL; + } else { + nlog_error("Failed to modify log_level of the node, " + "node_name:%s, log_level: %s", + req->node_name, req->log_level); } - int ret = neu_plugin_op(plugin, header, &cmd); - if (ret != 0) { - NEU_JSON_RESPONSE_ERROR(NEU_ERR_IS_BUSY, { - neu_http_response(aio, NEU_ERR_IS_BUSY, result_error); + if (log_level == -1) { + NEU_JSON_RESPONSE_ERROR(NEU_ERR_PARAM_IS_WRONG, { + neu_http_response(aio, error_code.error, result_error); }); + } else { + header.ctx = aio; + header.type = NEU_REQ_UPDATE_LOG_LEVEL; + cmd.core = req->core; + cmd.log_level = log_level; + if (req->node_name != NULL) { + strcpy(cmd.node, req->node_name); + } + + int ret = neu_plugin_op(plugin, header, &cmd); + if (ret != 0) { + NEU_JSON_RESPONSE_ERROR(NEU_ERR_IS_BUSY, { + neu_http_response(aio, NEU_ERR_IS_BUSY, + result_error); + }); + } } } }) diff --git a/plugins/restful/rw_handle.c b/plugins/restful/rw_handle.c index 0c743ae78..73b219dab 100644 --- a/plugins/restful/rw_handle.c +++ b/plugins/restful/rw_handle.c @@ -37,10 +37,25 @@ void handle_read(nng_aio *aio) int ret = 0; neu_reqresp_head_t header = { 0 }; neu_req_read_group_t cmd = { 0 }; - + int err_type; header.ctx = aio; header.type = NEU_REQ_READ_GROUP; + if (strlen(req->node) >= NEU_NODE_NAME_LEN) { + err_type = NEU_ERR_NODE_NAME_TOO_LONG; + goto error; + } + + if (strlen(req->node) >= NEU_GROUP_NAME_LEN) { + err_type = NEU_ERR_GROUP_NAME_TOO_LONG; + goto error; + } + + if (strlen(req->node) >= NEU_TAG_NAME_LEN) { + err_type = NEU_ERR_TAG_NAME_TOO_LONG; + goto error; + } + cmd.driver = req->node; cmd.group = req->group; cmd.name = req->name; @@ -55,6 +70,13 @@ void handle_read(nng_aio *aio) neu_http_response(aio, NEU_ERR_IS_BUSY, result_error); }); } + goto success; + + error: + NEU_JSON_RESPONSE_ERROR( + err_type, { neu_http_response(aio, err_type, result_error); }); + + success:; }) } @@ -66,6 +88,7 @@ void handle_write(nng_aio *aio) aio, neu_json_write_req_t, neu_json_decode_write_req, { neu_reqresp_head_t header = { 0 }; neu_req_write_tag_t cmd = { 0 }; + int err_type; if (req->t == NEU_JSON_STR && strlen(req->value.val_str) >= NEU_VALUE_SIZE) { @@ -92,6 +115,21 @@ void handle_write(nng_aio *aio) header.ctx = aio; header.type = NEU_REQ_WRITE_TAG; + if (strlen(req->node) >= NEU_NODE_NAME_LEN) { + err_type = NEU_ERR_NODE_NAME_TOO_LONG; + goto error; + } + + if (strlen(req->node) >= NEU_GROUP_NAME_LEN) { + err_type = NEU_ERR_GROUP_NAME_TOO_LONG; + goto error; + } + + if (strlen(req->node) >= NEU_TAG_NAME_LEN) { + err_type = NEU_ERR_TAG_NAME_TOO_LONG; + goto error; + } + cmd.driver = req->node; cmd.group = req->group; cmd.tag = req->tag; @@ -134,6 +172,13 @@ void handle_write(nng_aio *aio) neu_http_response(aio, NEU_ERR_IS_BUSY, result_error); }); } + goto success; + + error: + NEU_JSON_RESPONSE_ERROR( + err_type, { neu_http_response(aio, err_type, result_error); }); + + success:; }) } @@ -145,6 +190,7 @@ void handle_write_tags(nng_aio *aio) aio, neu_json_write_tags_req_t, neu_json_decode_write_tags_req, { neu_reqresp_head_t header = { 0 }; neu_req_write_tags_t cmd = { 0 }; + int err_type; for (int i = 0; i < req->n_tag; i++) { if (req->tags[i].t == NEU_JSON_STR) { @@ -164,6 +210,16 @@ void handle_write_tags(nng_aio *aio) header.ctx = aio; header.type = NEU_REQ_WRITE_TAGS; + if (strlen(req->node) >= NEU_NODE_NAME_LEN) { + err_type = NEU_ERR_NODE_NAME_TOO_LONG; + goto error; + } + + if (strlen(req->node) >= NEU_GROUP_NAME_LEN) { + err_type = NEU_ERR_GROUP_NAME_TOO_LONG; + goto error; + } + cmd.driver = req->node; cmd.group = req->group; cmd.n_tag = req->n_tag; @@ -213,6 +269,13 @@ void handle_write_tags(nng_aio *aio) neu_http_response(aio, NEU_ERR_IS_BUSY, result_error); }); } + goto success; + + error: + NEU_JSON_RESPONSE_ERROR( + err_type, { neu_http_response(aio, err_type, result_error); }); + + success:; }) } diff --git a/src/adapter/adapter.c b/src/adapter/adapter.c index 18489ae44..fcf806166 100644 --- a/src/adapter/adapter.c +++ b/src/adapter/adapter.c @@ -956,6 +956,9 @@ static int adapter_loop(enum neu_event_io_type type, int fd, void *usr_data) if (0 == ret) { adapter_storage_del_tag(cmd->driver, cmd->group, cmd->tags[i]); + } else { + error.error = ret; + break; } } } else { @@ -1073,15 +1076,24 @@ static int adapter_loop(enum neu_event_io_type type, int fd, void *usr_data) neu_resp_update_tag_t resp = { 0 }; if (adapter->module->type == NEU_NA_TYPE_DRIVER) { + for (int i = 0; i < cmd->n_tag; i++) { - int ret = neu_adapter_driver_update_tag( + int ret = neu_adapter_driver_validate_tag( (neu_adapter_driver_t *) adapter, cmd->group, &cmd->tags[i]); if (ret == 0) { - adapter_storage_update_tag(cmd->driver, cmd->group, - &cmd->tags[i]); - - resp.index += 1; + ret = neu_adapter_driver_update_tag( + (neu_adapter_driver_t *) adapter, cmd->group, + &cmd->tags[i]); + if (ret == 0) { + adapter_storage_update_tag(cmd->driver, cmd->group, + &cmd->tags[i]); + + resp.index += 1; + } else { + resp.error = ret; + break; + } } else { resp.error = ret; break; diff --git a/tests/ut/async_queue_test.cc b/tests/ut/async_queue_test.cc index 843ec8d4c..246971ade 100644 --- a/tests/ut/async_queue_test.cc +++ b/tests/ut/async_queue_test.cc @@ -1,5 +1,4 @@ #include -#include extern "C" { #include "utils/async_queue.h"