From 9cf658c396180599ec052ae958e8e304165e6f85 Mon Sep 17 00:00:00 2001 From: 54liuyao <54liuyao@163.com> Date: Thu, 24 Mar 2022 22:13:19 -0900 Subject: [PATCH] [TD-13918]: Strings are compared without comparing lengths --- src/client/inc/tscUtil.h | 2 +- src/client/src/tscParseInsert.c | 2 +- src/client/src/tscSQLParser.c | 4 ++-- src/client/src/tscUtil.c | 4 ++-- tests/develop-test/0-others/json_tag.py | 20 ++++++++++++++++++++ 5 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/client/inc/tscUtil.h b/src/client/inc/tscUtil.h index e380dc7776..f7d3aa630f 100644 --- a/src/client/inc/tscUtil.h +++ b/src/client/inc/tscUtil.h @@ -400,7 +400,7 @@ void tscRemoveCachedTableMeta(STableMetaInfo* pTableMetaInfo, uint64_t id); char* cloneCurrentDBName(SSqlObj* pSql); -int parseJsontoTagData(char* json, SKVRowBuilder* kvRowBuilder, char* errMsg, int16_t startColId); +int parseJsontoTagData(char* json, uint32_t jsonLength, SKVRowBuilder* kvRowBuilder, char* errMsg, int16_t startColId); int8_t jsonType2DbType(double data, int jsonType); void getJsonKey(SStrToken *t0); diff --git a/src/client/src/tscParseInsert.c b/src/client/src/tscParseInsert.c index c5719dde27..af8c683721 100644 --- a/src/client/src/tscParseInsert.c +++ b/src/client/src/tscParseInsert.c @@ -1068,7 +1068,7 @@ static int32_t tscCheckIfCreateTable(char **sqlstr, SSqlObj *pSql, char** boundC tfree(tmp); return tscSQLSyntaxErrMsg(pInsertParam->msg, "json tag too long", NULL); } - code = parseJsontoTagData(sToken.z, &kvRowBuilder, pInsertParam->msg, pTagSchema[spd.boundedColumns[0]].colId); + code = parseJsontoTagData(sToken.z, sToken.n, &kvRowBuilder, pInsertParam->msg, pTagSchema[spd.boundedColumns[0]].colId); if (code != TSDB_CODE_SUCCESS) { tdDestroyKVRowBuilder(&kvRowBuilder); tscDestroyBoundColumnInfo(&spd); diff --git a/src/client/src/tscSQLParser.c b/src/client/src/tscSQLParser.c index 2ab7e93c38..6cb419335f 100644 --- a/src/client/src/tscSQLParser.c +++ b/src/client/src/tscSQLParser.c @@ -7364,7 +7364,7 @@ int32_t setAlterTableInfo(SSqlObj* pSql, struct SSqlInfo* pInfo) { int8_t tagVal = TSDB_DATA_JSON_PLACEHOLDER; tdAddColToKVRow(&kvRowBuilder, pTagsSchema->colId, pTagsSchema->type, &tagVal, false); - code = parseJsontoTagData(pItem->pVar.pz, &kvRowBuilder, pMsg, pTagsSchema->colId); + code = parseJsontoTagData(pItem->pVar.pz, pItem->pVar.nLen, &kvRowBuilder, pMsg, pTagsSchema->colId); if (code != TSDB_CODE_SUCCESS) { tdDestroyKVRowBuilder(&kvRowBuilder); return code; @@ -9170,7 +9170,7 @@ int32_t doCheckForCreateFromStable(SSqlObj* pSql, SSqlInfo* pInfo) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg3); } - ret = parseJsontoTagData(pItem->pVar.pz, &kvRowBuilder, tscGetErrorMsgPayload(pCmd), pTagSchema[0].colId); + ret = parseJsontoTagData(pItem->pVar.pz, pItem->pVar.nLen, &kvRowBuilder, tscGetErrorMsgPayload(pCmd), pTagSchema[0].colId); if (ret != TSDB_CODE_SUCCESS) { tdDestroyKVRowBuilder(&kvRowBuilder); return ret; diff --git a/src/client/src/tscUtil.c b/src/client/src/tscUtil.c index ee676cfa02..dff3f32128 100644 --- a/src/client/src/tscUtil.c +++ b/src/client/src/tscUtil.c @@ -5393,7 +5393,7 @@ char* cloneCurrentDBName(SSqlObj* pSql) { return p; } -int parseJsontoTagData(char* json, SKVRowBuilder* kvRowBuilder, char* errMsg, int16_t startColId){ +int parseJsontoTagData(char* json, uint32_t jsonLength, SKVRowBuilder* kvRowBuilder, char* errMsg, int16_t startColId){ // set json NULL data uint8_t nullTypeVal[CHAR_BYTES + VARSTR_HEADER_SIZE + INT_BYTES] = {0}; uint32_t jsonNULL = TSDB_DATA_JSON_NULL; @@ -5404,7 +5404,7 @@ int parseJsontoTagData(char* json, SKVRowBuilder* kvRowBuilder, char* errMsg, in varDataSetLen(nullTypeVal + CHAR_BYTES, INT_BYTES); *(uint32_t*)(varDataVal(nullTypeKey)) = jsonNULL; tdAddColToKVRow(kvRowBuilder, jsonIndex++, TSDB_DATA_TYPE_NCHAR, nullTypeKey, false); // add json null type - if (!json || strtrim(json) == 0 || strncasecmp(json, "null", 4) == 0){ + if (!json || strtrim(json) == 0 || (jsonLength == strlen("null") && strncasecmp(json, "null", 4) == 0)){ *(uint32_t*)(varDataVal(nullTypeVal + CHAR_BYTES)) = jsonNULL; tdAddColToKVRow(kvRowBuilder, jsonIndex++, TSDB_DATA_TYPE_NCHAR, nullTypeVal, true); // add json null value return TSDB_CODE_SUCCESS; diff --git a/tests/develop-test/0-others/json_tag.py b/tests/develop-test/0-others/json_tag.py index d3891a796a..c78e48b6d1 100644 --- a/tests/develop-test/0-others/json_tag.py +++ b/tests/develop-test/0-others/json_tag.py @@ -530,6 +530,26 @@ class TDTestCase: tdSql.checkData(5, 2, 4096) tdSql.query("describe jsons1_1") tdSql.checkData(5, 2, 4096) + + #test TD-13918 + tdSql.execute("drop table if exists jsons_13918_1") + tdSql.execute("drop table if exists jsons_13918_2") + tdSql.execute("drop table if exists jsons_13918_3") + tdSql.execute("drop table if exists jsons_13918_4") + tdSql.execute("drop table if exists jsons_stb") + tdSql.execute("create table jsons_stb (ts timestamp, dataInt int) tags (jtag json)") + tdSql.error("create table jsons_13918_1 using jsons_stb tags ('nullx')") + tdSql.error("create table jsons_13918_2 using jsons_stb tags (nullx)") + tdSql.error("insert into jsons_13918_3 using jsons_stb tags('NULLx') values(1591061628001, 11)") + tdSql.error("insert into jsons_13918_4 using jsons_stb tags(NULLx) values(1591061628002, 11)") + tdSql.execute("create table jsons_13918_1 using jsons_stb tags ('null')") + tdSql.execute("create table jsons_13918_2 using jsons_stb tags (null)") + tdSql.execute("insert into jsons_13918_1 values(1591061628003, 11)") + tdSql.execute("insert into jsons_13918_2 values(1591061628004, 11)") + tdSql.execute("insert into jsons_13918_3 using jsons_stb tags('NULL') values(1591061628005, 11)") + tdSql.execute("insert into jsons_13918_4 using jsons_stb tags(\"NULL\") values(1591061628006, 11)") + tdSql.query("select * from jsons_stb") + tdSql.checkRows(4) def stop(self): tdSql.close() -- GitLab