diff --git a/src/client/src/tscSystem.c b/src/client/src/tscSystem.c index 19f275aa90fc348ae631499ca6b7c8c4091a75e7..63741e08f47316536743f8a8229c8a7cb6de1af0 100644 --- a/src/client/src/tscSystem.c +++ b/src/client/src/tscSystem.c @@ -440,30 +440,30 @@ int taos_options(TSDB_OPTION option, const void *arg, ...) { #include "cJSON.h" static setConfRet taos_set_config_imp(const char *config){ - setConfRet ret; + setConfRet ret = {SET_CONF_RET_SUCC, {0}}; static bool setConfFlag = false; if (setConfFlag) { - ret.retCode = -5; + ret.retCode = SET_CONF_RET_ERR_ONLY_ONCE; strcpy(ret.retMsg, "configuration can only set once"); return ret; } taosInitGlobalCfg(); cJSON *root = cJSON_Parse(config); if (root == NULL){ - ret.retCode = -4; + ret.retCode = SET_CONF_RET_ERR_JSON_PARSE; strcpy(ret.retMsg, "parse json error"); return ret; } int size = cJSON_GetArraySize(root); if(!cJSON_IsObject(root) || size == 0) { - ret.retCode = -3; + ret.retCode = SET_CONF_RET_ERR_JSON_INVALID; strcpy(ret.retMsg, "json content is invalid, must be not empty object"); return ret; } if(size >= 1000) { - ret.retCode = -6; + ret.retCode = SET_CONF_RET_ERR_TOO_LONG; strcpy(ret.retMsg, "json object size is too long"); return ret; } @@ -471,21 +471,25 @@ static setConfRet taos_set_config_imp(const char *config){ for(int i = 0; i < size; i++){ cJSON *item = cJSON_GetArrayItem(root, i); if(!item) { - ret.retCode = -2; + ret.retCode = SET_CONF_RET_ERR_INNER; strcpy(ret.retMsg, "inner error"); return ret; } if(!taosReadConfigOption(item->string, item->valuestring, NULL, NULL, TAOS_CFG_CSTATUS_OPTION, TSDB_CFG_CTYPE_B_CLIENT)){ - ret.retCode = -1; + ret.retCode = SET_CONF_RET_ERR_PART; if (strlen(ret.retMsg) == 0){ - snprintf(ret.retMsg, 1024, "part error|%s", item->string); + snprintf(ret.retMsg, RET_MSG_LENGTH - 1, "part error|%s", item->string); }else{ - char tmp[1000] = {0}; - strncpy(tmp, ret.retMsg, 1000); - snprintf(ret.retMsg, 1024, "%s|%s", tmp, item->string); + int leftSize = RET_MSG_LENGTH - 1 - strlen(ret.retMsg); + leftSize = leftSize >= 0 ? leftSize : 0; + strncat(ret.retMsg, "|", leftSize); + leftSize = RET_MSG_LENGTH - 1 - strlen(ret.retMsg); + leftSize = leftSize >= 0 ? leftSize : 0; + strncat(ret.retMsg, item->string, leftSize); } } } + cJSON_Delete(root); setConfFlag = true; return ret; } diff --git a/src/client/tests/setConfigTest.cpp b/src/client/tests/setConfigTest.cpp index 1b3f74c1898d50b1912b05d35a94283ffdb1bafa..fb016715f6ad2f5311aa2d81b608c2043f86c4f0 100644 --- a/src/client/tests/setConfigTest.cpp +++ b/src/client/tests/setConfigTest.cpp @@ -62,3 +62,10 @@ TEST(testCase, set_config_test5) { ASSERT_EQ(ret.retCode, -3); printf("msg:%d->%s", ret.retCode, ret.retMsg); } + +TEST(testCase, set_config_test6) { + const char *config = "{\"numOfCoitThreadsnumOfCoitThreadsnumOfCoitThreadsnumOfCoitThreadsnumOfCoitThreadsnumOfCoitThreadsnumOfCoitThreadsnumOfCoitThreadsnumOfCoitThreadsnumOfCoitThreadsnumOfCoitThreadsnumOfCoitThreadsnumOfCoitThreadsnumOfCoitThreadsnumOfCoitThreadsnumOfCoitThreadsnumOfCoitThreadsnumOfCoitThreadsnumOfCoitThreadsnumOfCoitThreadsnumOfCoitThreadsnumOfCoitThreadsnumOfCoitThreadsnumOfCoitThreadsnumOfCoitThreadsnumOfCoitThreadsnumOfCoitThreadsnumOfCoitThreadsnumOfCoitThreadsnumOfCoitThreadsnumOfCoitThreadsnumOfCoitThreadsnumOfCoitThreadsnumOfCoitThreadsnumOfCoitThreadsnumOfCoitThreadsnumOfCoitThreadsnumOfCoitThreadsnumOfCoitThreadsnumOfCoitThreadsnumOfCoitThreadsnumOfCoitThreadsnumOfCoitThreadsnumOfCoitThreadsnumOfCoitThreadsnumOfCoitThreadsnumOfCoitThreadsnumOfCoitThreadsnumOfCoitThreadsnumOfCoitThreadsnumOfCoitThreadsnumOfCoitThreadsnumOfCoitThreadsnumOfCoitThreadsnumOfCoitThreadsnumOfCoitThreadsnumOfCoitThreadsnumOfCoitThreadsnumOfCoitThreadsnumOfCoitThreadsnumOfCoitThreadsnumOfCoitThreadsnumOfCoitThreadsnumOfCoitT3333dd\":\"10\", \"esdfa\":\"10\"}"; + setConfRet ret = taos_set_config(config); + ASSERT_EQ(ret.retCode, -1); + printf("msg:%d->%s", ret.retCode, ret.retMsg); +} diff --git a/src/inc/taos.h b/src/inc/taos.h index d77fa8f5fcb4c957c274d076a1d6f2ef5631c85a..662c66c6ec741b2e4c26977d7655985f1f5f8de2 100644 --- a/src/inc/taos.h +++ b/src/inc/taos.h @@ -62,9 +62,20 @@ typedef struct taosField { int16_t bytes; } TAOS_FIELD; +typedef enum { + SET_CONF_RET_SUCC = 0, + SET_CONF_RET_ERR_PART = -1, + SET_CONF_RET_ERR_INNER = -2, + SET_CONF_RET_ERR_JSON_INVALID = -3, + SET_CONF_RET_ERR_JSON_PARSE = -4, + SET_CONF_RET_ERR_ONLY_ONCE = -5, + SET_CONF_RET_ERR_TOO_LONG = -6 +} SET_CONF_RET_CODE; + +#define RET_MSG_LENGTH 1024 typedef struct setConfRet { - char retMsg[1024]; - int8_t retCode; + SET_CONF_RET_CODE retCode; + char retMsg[RET_MSG_LENGTH]; } setConfRet; #ifdef _TD_GO_DLL_