From 5495be2784547be3d9cde09177186199087c7977 Mon Sep 17 00:00:00 2001 From: wangmm0220 Date: Tue, 19 Oct 2021 13:21:47 +0800 Subject: [PATCH] TD-6129 add taos json key hash free function --- src/client/src/tscUtil.c | 8 +++++++- src/util/inc/tutil.h | 1 + src/util/src/tutil.c | 11 +++++++++++ tests/pytest/stable/json_tag.py | 9 +++++++++ 4 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/client/src/tscUtil.c b/src/client/src/tscUtil.c index efe52ac8dc..f963a1e6a2 100644 --- a/src/client/src/tscUtil.c +++ b/src/client/src/tscUtil.c @@ -5285,6 +5285,7 @@ end: } int parseJsontoTagData(char* json, SKVRowBuilder* kvRowBuilder, char* errMsg, int16_t startColId){ + // set json NULL data uint8_t nullTypeVal[CHAR_BYTES + VARSTR_HEADER_SIZE + CHAR_BYTES] = {0}; uint8_t jsonNULL = TSDB_DATA_JSON_NULL; int jsonIndex = startColId + 1; @@ -5302,6 +5303,7 @@ int parseJsontoTagData(char* json, SKVRowBuilder* kvRowBuilder, char* errMsg, in *(uint8_t*)(varDataVal(nullTypeVal + CHAR_BYTES)) = jsonNotNull; tdAddColToKVRow(kvRowBuilder, jsonIndex++, TSDB_DATA_TYPE_NCHAR, &nullTypeVal, true); // add json type + // set json real data cJSON *root = cJSON_Parse(json); if (root == NULL){ tscError("json parse error"); @@ -5329,7 +5331,11 @@ int parseJsontoTagData(char* json, SKVRowBuilder* kvRowBuilder, char* errMsg, in if (item->type == cJSON_NULL){ continue; } - + if(!isValidateTag(jsonKey)){ + tscError("json key not validate"); + retCode = tscSQLSyntaxErrMsg(errMsg, "json key not validate", NULL); + goto end; + } if(strlen(jsonKey) > TSDB_MAX_JSON_KEY_LEN){ tscError("json key too long error"); retCode = tscSQLSyntaxErrMsg(errMsg, "json key too long, more than 256", NULL); diff --git a/src/util/inc/tutil.h b/src/util/inc/tutil.h index 43382a4856..3eb675f569 100644 --- a/src/util/inc/tutil.h +++ b/src/util/inc/tutil.h @@ -46,6 +46,7 @@ int taosCheckVersion(char *input_client_version, char *input_server_version, in char * taosIpStr(uint32_t ipInt); uint32_t ip2uint(const char *const ip_addr); void jsonKeyMd5(void *pMsg, int msgLen, void *pKey); +bool isValidateTag(char *input); static FORCE_INLINE void taosEncryptPass(uint8_t *inBuf, size_t inLen, char *target) { MD5_CTX context; diff --git a/src/util/src/tutil.c b/src/util/src/tutil.c index 2def2be8a4..441ee74e16 100644 --- a/src/util/src/tutil.c +++ b/src/util/src/tutil.c @@ -466,6 +466,17 @@ void jsonKeyMd5(void *pMsg, int msgLen, void *pKey) { memcpy(pKey, context.digest, sizeof(context.digest)); } +bool isValidateTag(char *input) { + if (!input) return false; + int len = strlen(input); + if (len == 0) return false; + if (input[0] != '_' || isalpha(input[0]) == 0) return false; + for (int i = 1; i < len; ++i) { + if (input[0] != '_' || isalnum(input[0]) == 0) return false; + } + return true; +} + FORCE_INLINE float taos_align_get_float(const char* pBuf) { #if __STDC_VERSION__ >= 201112L static_assert(sizeof(float) == sizeof(uint32_t), "sizeof(float) must equal to sizeof(uint32_t)"); diff --git a/tests/pytest/stable/json_tag.py b/tests/pytest/stable/json_tag.py index 7a469647d0..27be0b7eac 100644 --- a/tests/pytest/stable/json_tag.py +++ b/tests/pytest/stable/json_tag.py @@ -33,6 +33,9 @@ class TDTestCase: tdSql.execute("create table if not exists db_json_tag_test.jsons1(ts timestamp, dataInt int, dataBool bool, dataStr nchar(50)) tags(jtag json)") tdSql.execute("CREATE TABLE if not exists db_json_tag_test.jsons1_1 using db_json_tag_test.jsons1 tags('{\"loc\":\"fff\",\"id\":5}')") tdSql.error("CREATE TABLE if not exists db_json_tag_test.jsons1_3 using db_json_tag_test.jsons1 tags(3333)") + tdSql.error("CREATE TABLE if not exists db_json_tag_test.jsons1_3 using db_json_tag_test.jsons1 tags('{\"1loc\":\"fff\",\";id\":5}')") + tdSql.error("CREATE TABLE if not exists db_json_tag_test.jsons1_3 using db_json_tag_test.jsons1 tags('{\"。loc\":\"fff\",\"fsd\":5}')") + tdSql.error("CREATE TABLE if not exists db_json_tag_test.jsons1_3 using db_json_tag_test.jsons1 tags('{\"试试\":\"fff\",\";id\":5}')") tdSql.execute("insert into db_json_tag_test.jsons1_2 using db_json_tag_test.jsons1 tags('{\"num\":5,\"location\":\"beijing\"}') values (now, 2, true, 'json2')") tdSql.error("insert into db_json_tag_test.jsons1_4 using db_json_tag_test.jsons1 tags(3)") tdSql.execute("insert into db_json_tag_test.jsons1_1 values(now, 1, false, 'json1')") @@ -228,6 +231,12 @@ class TDTestCase: tdSql.query("select distinct jtag from db_json_tag_test.jsons1") tdSql.checkRows(6) + tdSql.query("select distinct jtag->'location' from db_json_tag_test.jsons1") + tdSql.checkRows(3) + + # test chinese + tdSql.execute("CREATE TABLE if not exists db_json_tag_test.jsons1_11 using db_json_tag_test.jsons1 tags('{\"k1\":\"中国\",\"k2\":\"是是是\"}')") + def stop(self): tdSql.close() tdLog.success("%s successfully executed" % __file__) -- GitLab