diff --git a/include/neuron/errcodes.h b/include/neuron/errcodes.h index 30f37cfe..6d83e2bf 100644 --- a/include/neuron/errcodes.h +++ b/include/neuron/errcodes.h @@ -136,7 +136,7 @@ typedef enum { NEU_ERR_MQTT_SUSPENDED = 4015, NEU_ERR_MQTT_PUBLISH_OVER_LENGTH = 4016, - NEU_ERR_FILE_TOO_LONG = 4100, + NEU_ERR_STRING_TOO_LONG = 4100, NEU_ERR_FILE_OPEN_FAILURE = 4101, NEU_ERR_FILE_READ_FAILURE = 4102, NEU_ERR_FILE_WRITE_FAILURE = 4103, diff --git a/plugins/file/file.json b/plugins/file/file.json index 32e49a12..821fa8da 100644 --- a/plugins/file/file.json +++ b/plugins/file/file.json @@ -15,7 +15,8 @@ "type": "int", "default": 512, "valid": { - "min": 1 + "min": 1, + "max": 512 } } } \ No newline at end of file diff --git a/plugins/file/file_req.c b/plugins/file/file_req.c index b3a4ed65..925d95b5 100644 --- a/plugins/file/file_req.c +++ b/plugins/file/file_req.c @@ -62,7 +62,7 @@ int file_group_timer(neu_plugin_t *plugin, neu_plugin_group_t *group) } if ((f = fopen(tag->address, "rb+")) == NULL) { - nlog_warn("open file:%s", tag->address); + nlog_warn("fail open to read and write file: %s", tag->address); dvalue.type = NEU_TYPE_ERROR; dvalue.value.i32 = NEU_ERR_FILE_OPEN_FAILURE; goto dvalue_result; @@ -71,8 +71,11 @@ int file_group_timer(neu_plugin_t *plugin, neu_plugin_group_t *group) fseek(f, 0, SEEK_END); int size = ftell(f); if (size >= length) { + nlog_warn( + "the file length exceeds the set value, file: %s, length: %d", + tag->address, size); dvalue.type = NEU_TYPE_ERROR; - dvalue.value.i32 = NEU_ERR_FILE_TOO_LONG; + dvalue.value.i32 = NEU_ERR_STRING_TOO_LONG; fclose(f); goto dvalue_result; @@ -85,6 +88,14 @@ int file_group_timer(neu_plugin_t *plugin, neu_plugin_group_t *group) dvalue.type = NEU_TYPE_ERROR; dvalue.value.i32 = NEU_ERR_FILE_READ_FAILURE; + fclose(f); + goto dvalue_result; + } else if (ret >= NEU_VALUE_SIZE) { + nlog_warn("the file length exceeds the maximum, file: %s, ret: %d", + tag->address, ret); + dvalue.type = NEU_TYPE_ERROR; + dvalue.value.i32 = NEU_ERR_STRING_TOO_LONG; + fclose(f); goto dvalue_result; } @@ -121,19 +132,32 @@ static void plugin_group_free(neu_plugin_group_t *pgp) int file_write(neu_plugin_t *plugin, void *req, neu_datatag_t *tag, neu_value_u value) { - FILE *fp = fopen(tag->address, "w"); - int ret = 0; + FILE *fp = NULL; + int ret = 0; + int length = strlen(value.str); + + if (length >= NEU_VALUE_SIZE) { + nlog_warn("write file exceeds the maximum value, file: %s, length: %d", + tag->address, length); + ret = NEU_ERR_STRING_TOO_LONG; - if (0 == fwrite(value.str, sizeof(char), strlen(value.str), fp)) { - nlog_warn("write file failed:%s", tag->address); - ret = NEU_ERR_FILE_WRITE_FAILURE; goto dvalue_result; } + fp = fopen(tag->address, "w"); + + if (0 == fwrite(value.str, sizeof(char), strlen(value.str), fp)) { + nlog_warn("write file failed: %s", tag->address); + ret = NEU_ERR_FILE_WRITE_FAILURE; + + fclose(fp); + goto dvalue_result; + } + + fclose(fp); neu_datatag_string_ltoh(value.str, strlen(value.str)); dvalue_result: - fclose(fp); plugin->common.adapter_callbacks->driver.write_response( plugin->common.adapter, req, ret); diff --git a/plugins/restful/rw_handle.c b/plugins/restful/rw_handle.c index b16fc06a..8c5b0a8d 100644 --- a/plugins/restful/rw_handle.c +++ b/plugins/restful/rw_handle.c @@ -58,7 +58,6 @@ void handle_write(nng_aio *aio) NEU_PROCESS_HTTP_REQUEST_VALIDATE_JWT( aio, neu_json_write_req_t, neu_json_decode_write_req, { - int ret = 0; neu_reqresp_head_t header = { 0 }; neu_req_write_tag_t cmd = { 0 }; @@ -75,8 +74,13 @@ void handle_write(nng_aio *aio) cmd.value.value.u64 = req->value.val_int; break; case NEU_JSON_STR: - cmd.value.type = NEU_TYPE_STRING; - strcpy(cmd.value.value.str, req->value.val_str); + if (strlen(req->value.val_str) >= NEU_VALUE_SIZE) { + cmd.value.type = NEU_TYPE_ERROR; + cmd.value.value.i32 = NEU_ERR_STRING_TOO_LONG; + } else { + cmd.value.type = NEU_TYPE_STRING; + strcpy(cmd.value.value.str, req->value.val_str); + } break; case NEU_JSON_DOUBLE: cmd.value.type = NEU_TYPE_DOUBLE; @@ -91,11 +95,18 @@ void handle_write(nng_aio *aio) break; } - 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 (cmd.value.type == NEU_TYPE_ERROR) { + NEU_JSON_RESPONSE_ERROR(NEU_ERR_STRING_TOO_LONG, { + neu_http_response(aio, NEU_ERR_STRING_TOO_LONG, + result_error); }); + } else { + 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/src/parser/neu_json_tag.c b/src/parser/neu_json_tag.c index be981e84..a3f9beab 100644 --- a/src/parser/neu_json_tag.c +++ b/src/parser/neu_json_tag.c @@ -200,7 +200,7 @@ int neu_json_tag_check_type(neu_json_tag_t *tag) case NEU_TYPE_BYTES: case NEU_TYPE_STRING: return NEU_JSON_STR == tag->t && - strlen(tag->value.val_str) < NEU_VALUE_SIZE; + strlen(tag->value.val_str) <= NEU_VALUE_SIZE; } } else { // non-static tag should have no initial value diff --git a/src/utils/http.c b/src/utils/http.c index d8e3ea4b..816409fa 100644 --- a/src/utils/http.c +++ b/src/utils/http.c @@ -295,6 +295,8 @@ int neu_http_response(nng_aio *aio, neu_err_code_e code, char *content) case NEU_ERR_MQTT_CONNECT_FAILURE: case NEU_ERR_MQTT_SUBSCRIBE_FAILURE: case NEU_ERR_MQTT_PUBLISH_FAILURE: + case NEU_ERR_STRING_TOO_LONG: + case NEU_ERR_FILE_WRITE_FAILURE: status = NNG_HTTP_STATUS_OK; break; case NEU_ERR_EINTERNAL: