From 3d7fbb58cd0eb09a7121637ec6522d0d1e7851f5 Mon Sep 17 00:00:00 2001 From: fengzero Date: Mon, 28 Sep 2026 03:22:15 +0000 Subject: [PATCH] check import ede --- plugins/restful/ede_handle.c | 23 +++++++++++++++++++++-- plugins/restful/global_config_handle.c | 1 + src/utils/ede.c | 6 ++++++ tests/ut/CMakeLists.txt | 1 + tests/ut/ede_test.cc | 17 +++++++++++++++++ tests/ut/wrong.ede | 5 +++++ 6 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 tests/ut/wrong.ede diff --git a/plugins/restful/ede_handle.c b/plugins/restful/ede_handle.c index 36e6cd592..f9775e208 100644 --- a/plugins/restful/ede_handle.c +++ b/plugins/restful/ede_handle.c @@ -1,3 +1,5 @@ +#include + #include #include @@ -11,6 +13,20 @@ #include "json/neu_json_error.h" #include "json/neu_json_fn.h" +static void ede_cmd_uninit(neu_req_add_gtag_t *cmd) +{ + if (cmd == NULL || cmd->groups == NULL) { + return; + } + + for (int i = 0; i < cmd->n_group; ++i) { + neu_ede_tags_uninit(cmd->groups[i].tags, cmd->groups[i].n_tag); + } + free(cmd->groups); + cmd->groups = NULL; + cmd->n_group = 0; +} + void handle_ede(nng_aio *aio) { neu_plugin_t *plugin = neu_rest_get_plugin(); @@ -31,13 +47,16 @@ void handle_ede(nng_aio *aio) NEU_JSON_RESPONSE_ERROR(NEU_ERR_INVALID_CID, { neu_http_response(aio, NEU_ERR_INVALID_CID, result_error); }); + goto success; } - int ret = neu_plugin_op(plugin, header, &cmd); - if (ret != 0) { + if (neu_plugin_op(plugin, header, &cmd) != 0) { + ede_cmd_uninit(&cmd); NEU_JSON_RESPONSE_ERROR(NEU_ERR_IS_BUSY, { neu_http_response(aio, NEU_ERR_IS_BUSY, result_error); }); } + + success:; }) } \ No newline at end of file diff --git a/plugins/restful/global_config_handle.c b/plugins/restful/global_config_handle.c index 152b7ea88..4ff798b58 100644 --- a/plugins/restful/global_config_handle.c +++ b/plugins/restful/global_config_handle.c @@ -1148,6 +1148,7 @@ static int get_tags_resp(context_t *ctx, neu_resp_get_tag_t *tags) tags_res.tags[index].precision = tag->precision; tags_res.tags[index].decimal = tag->decimal; tags_res.tags[index].bias = tag->bias; + tags_res.tags[index].unit = tag->unit; tags_res.tags[index].t = NEU_JSON_UNDEFINE; } diff --git a/src/utils/ede.c b/src/utils/ede.c index 54e3a36c6..4f044104a 100644 --- a/src/utils/ede.c +++ b/src/utils/ede.c @@ -332,6 +332,12 @@ int neu_ede_parse_file(const char *file_path, neu_ede_result_t *result) } fclose(fp); + + if (!object_header_seen || result->count == 0) { + neu_ede_result_uninit(result); + return -1; + } + return 0; } diff --git a/tests/ut/CMakeLists.txt b/tests/ut/CMakeLists.txt index 1f1afa6c1..5d5a0b8fc 100644 --- a/tests/ut/CMakeLists.txt +++ b/tests/ut/CMakeLists.txt @@ -94,6 +94,7 @@ target_include_directories(mqtt_schema_test PRIVATE target_link_libraries(mqtt_schema_test neuron-base gtest_main gtest) file(COPY ${CMAKE_SOURCE_DIR}/tests/ut/EDE_test.csv DESTINATION ${UT_DIRECTORY}/config) +file(COPY ${CMAKE_SOURCE_DIR}/tests/ut/wrong.ede DESTINATION ${UT_DIRECTORY}/config) add_executable(ede_test ede_test.cc ${CMAKE_SOURCE_DIR}/src/utils/ede.c) target_include_directories(ede_test PRIVATE ${CMAKE_SOURCE_DIR}/src diff --git a/tests/ut/ede_test.cc b/tests/ut/ede_test.cc index 2c2323287..ea7f90967 100644 --- a/tests/ut/ede_test.cc +++ b/tests/ut/ede_test.cc @@ -15,6 +15,7 @@ zlog_category_t *neuron = NULL; static constexpr size_t kExpectedEdeTagCount = 1499; #define EDE_TEST_FILE_PATH "./config/EDE_test.csv" +#define WRONG_EDE_FILE_PATH "./config/wrong.ede" static std::vector split_semicolon(const std::string &line) { @@ -150,6 +151,22 @@ TEST(EdeTest, ParseEdeFileToAddressAndType) neu_ede_result_uninit(&result); } +TEST(EdeTest, RejectTruncatedEdeFile) +{ + neu_ede_result_t result = { 0 }; + neu_datatag_t * tags = NULL; + size_t count = 0; + + EXPECT_NE(neu_ede_parse_file(WRONG_EDE_FILE_PATH, &result), 0); + EXPECT_EQ(result.entries, nullptr); + EXPECT_EQ(result.count, 0); + + EXPECT_NE(neu_ede_parse_file_to_tags(WRONG_EDE_FILE_PATH, &tags, &count), + 0); + EXPECT_EQ(tags, nullptr); + EXPECT_EQ(count, 0); +} + TEST(EdeTest, FormatAddress) { char address[128] = { 0 }; diff --git a/tests/ut/wrong.ede b/tests/ut/wrong.ede new file mode 100644 index 000000000..36eb4e632 --- /dev/null +++ b/tests/ut/wrong.ede @@ -0,0 +1,5 @@ +#Engineering-Data-Exchange;;;;;;;;;;;;;;;;;;; +#PROJECT_NAME;NeuronEX BACnet/IP EDE Simulator;;;;;;;;;;;;;;;;;;; +#VERSION_OF_REFERENCEFILE;1.0;;;;;;;;;;;;;;;;;;; +#TIMESTAMP_OF_LAST_CHANGE;25.09.2026;;;;;;;;;;;;;;;;;;; +#AUTHOR_OF_LAST_ \ No newline at end of file