From fa5d5ed776ecdb971cfb9c588a8adc8b622f1cf3 Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Wed, 10 Jun 2020 23:22:59 +0800 Subject: [PATCH] [td-582] fix crash. --- src/client/src/tscFunctionImpl.c | 1 - src/client/src/tscSQLParser.c | 23 ++++++++++------------- src/client/src/tscSchemaUtil.c | 8 -------- src/common/inc/tname.h | 3 +++ src/common/src/tname.c | 13 +++++++++++++ src/inc/taosdef.h | 2 +- src/mnode/src/mnodeTable.c | 20 ++++++++++++-------- src/query/src/qExecutor.c | 21 +++++++++++++-------- src/tsdb/src/tsdbRead.c | 2 +- 9 files changed, 53 insertions(+), 40 deletions(-) diff --git a/src/client/src/tscFunctionImpl.c b/src/client/src/tscFunctionImpl.c index b05e82b39a..a0deaa519a 100644 --- a/src/client/src/tscFunctionImpl.c +++ b/src/client/src/tscFunctionImpl.c @@ -151,7 +151,6 @@ typedef struct SRateInfo { double sum; // for sum/avg } SRateInfo; - int32_t getResultDataInfo(int32_t dataType, int32_t dataBytes, int32_t functionId, int32_t param, int16_t *type, int16_t *bytes, int32_t *interBytes, int16_t extLength, bool isSuperTable) { if (!isValidDataType(dataType, dataBytes)) { diff --git a/src/client/src/tscSQLParser.c b/src/client/src/tscSQLParser.c index 372e81d737..47f679a922 100644 --- a/src/client/src/tscSQLParser.c +++ b/src/client/src/tscSQLParser.c @@ -348,7 +348,7 @@ int32_t tscToSQLCmd(SSqlObj* pSql, struct SSqlInfo* pInfo) { return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg1); } - if (pToken->n >= TSDB_TABLE_NAME_LEN) { + if (tscValidateTableNameLength(pToken->n)) { return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg2); } @@ -1056,7 +1056,7 @@ int32_t setObjFullName(char* fullName, const char* account, SSQLToken* pDB, SSQL totalLen += 1; /* here we only check the table name length limitation */ - if (tableName->n > TSDB_TABLE_NAME_LEN) { + if (!tscValidateTableNameLength(tableName->n)) { return TSDB_CODE_TSC_INVALID_SQL; } } else { // pDB == NULL, the db prefix name is specified in tableName @@ -1402,9 +1402,7 @@ int32_t addProjectionExprAndResultField(SQueryInfo* pQueryInfo, tSQLExprItem* pI } if (index.columnIndex == TSDB_TBNAME_COLUMN_INDEX) { - SSchema colSchema = {.type = TSDB_DATA_TYPE_BINARY, .bytes = (TSDB_TABLE_NAME_LEN - 1) + VARSTR_HEADER_SIZE}; - strcpy(colSchema.name, TSQL_TBNAME_L); - + SSchema colSchema = tGetTableNameColumnSchema(); tscAddSpecialColumnForSelect(pQueryInfo, startPos, TSDB_FUNC_TAGPRJ, &index, &colSchema, true); } else { STableMetaInfo* pTableMetaInfo = tscGetMetaInfo(pQueryInfo, index.tableIndex); @@ -1914,9 +1912,7 @@ int32_t addExprAndResultField(SQueryInfo* pQueryInfo, int32_t colIndex, tSQLExpr SSchema s = {0}; if (index.columnIndex == TSDB_TBNAME_COLUMN_INDEX) { - s.bytes = TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE; - s.type = TSDB_DATA_TYPE_BINARY; - s.colId = TSDB_TBNAME_COLUMN_INDEX; + s = tGetTableNameColumnSchema(); } else { s = pTagSchema[index.columnIndex]; } @@ -2230,7 +2226,7 @@ int32_t setShowInfo(SSqlObj* pSql, struct SSqlInfo* pInfo) { return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg6); } - if (pCmd->payloadLen >= TSDB_TABLE_NAME_LEN) { + if (!tscValidateTableNameLength(pCmd->payloadLen)) { return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg2); } } @@ -2861,7 +2857,7 @@ static int32_t tablenameListToString(tSQLExpr* pExpr, SStringBuilder* sb) { taosStringBuilderAppendString(sb, TBNAME_LIST_SEP); } - if (pSub->val.nLen <= 0 || pSub->val.nLen > TSDB_TABLE_NAME_LEN) { + if (pSub->val.nLen <= 0 || !tscValidateTableNameLength(pSub->val.nLen)) { return TSDB_CODE_TSC_INVALID_SQL; } } @@ -5232,9 +5228,10 @@ static int32_t doAddGroupbyColumnsOnDemand(SQueryInfo* pQueryInfo) { int16_t colIndex = pColIndex->colIndex; if (colIndex == TSDB_TBNAME_COLUMN_INDEX) { - type = TSDB_DATA_TYPE_BINARY; - bytes = (TSDB_TABLE_NAME_LEN - 1) + VARSTR_HEADER_SIZE; // todo extract method - name = TSQL_TBNAME_L; + SSchema s = tGetTableNameColumnSchema(); + type = s.type; + bytes = s.bytes; + name = s.name; } else { if (TSDB_COL_IS_TAG(pColIndex->flag)) { SSchema* tagSchema = tscGetTableTagSchema(pTableMetaInfo->pTableMeta); diff --git a/src/client/src/tscSchemaUtil.c b/src/client/src/tscSchemaUtil.c index 88417addf6..439aa7c1de 100644 --- a/src/client/src/tscSchemaUtil.c +++ b/src/client/src/tscSchemaUtil.c @@ -50,14 +50,6 @@ int32_t tscGetNumOfColumns(const STableMeta* pTableMeta) { SSchema *tscGetTableSchema(const STableMeta *pTableMeta) { assert(pTableMeta != NULL); - -// if (pTableMeta->tableType == TSDB_CHILD_TABLE) { -// STableMeta* pSTableMeta = pTableMeta->pSTable; -// assert (pSTableMeta != NULL); -// -// return pSTableMeta->schema; -// } - return (SSchema*) pTableMeta->schema; } diff --git a/src/common/inc/tname.h b/src/common/inc/tname.h index 810d7f492c..d2008c9ff8 100644 --- a/src/common/inc/tname.h +++ b/src/common/inc/tname.h @@ -23,5 +23,8 @@ void extractTableName(const char *tableId, char *name); char* extractDBName(const char *tableId, char *name); +SSchema tGetTableNameColumnSchema(); + +bool tscValidateTableNameLength(size_t len); #endif // TDENGINE_NAME_H diff --git a/src/common/src/tname.c b/src/common/src/tname.c index d2f5dfc4f7..2514ed26e5 100644 --- a/src/common/src/tname.c +++ b/src/common/src/tname.c @@ -37,3 +37,16 @@ char* extractDBName(const char* tableId, char* name) { return strncpy(name, &tableId[offset1 + 1], len); } + +SSchema tGetTableNameColumnSchema() { + SSchema s = {0}; + s.bytes = TSDB_TABLE_NAME_LEN - 1 + VARSTR_HEADER_SIZE; + s.type = TSDB_DATA_TYPE_BINARY; + s.colId = TSDB_TBNAME_COLUMN_INDEX; + strncpy(s.name, TSQL_TBNAME_L, TSDB_COL_NAME_LEN); + return s; +} + +bool tscValidateTableNameLength(size_t len) { + return len < TSDB_TABLE_NAME_LEN; +} \ No newline at end of file diff --git a/src/inc/taosdef.h b/src/inc/taosdef.h index 251e3a9e40..be9c0b5ef6 100644 --- a/src/inc/taosdef.h +++ b/src/inc/taosdef.h @@ -200,7 +200,7 @@ void tsDataSwap(void *pLeft, void *pRight, int32_t type, int32_t size); #define TSDB_MIN_COLUMNS 2 //PRIMARY COLUMN(timestamp) + other columns #define TSDB_NODE_NAME_LEN 64 -#define TSDB_TABLE_NAME_LEN 193 +#define TSDB_TABLE_NAME_LEN 193 // it is a null-terminated string #define TSDB_DB_NAME_LEN 33 #define TSDB_COL_NAME_LEN 65 #define TSDB_MAX_SAVED_SQL_LEN TSDB_MAX_COLUMNS * 64 diff --git a/src/mnode/src/mnodeTable.c b/src/mnode/src/mnodeTable.c index a2ddd9893f..adb8e261d4 100644 --- a/src/mnode/src/mnodeTable.c +++ b/src/mnode/src/mnodeTable.c @@ -1078,8 +1078,9 @@ static int32_t mnodeGetShowSuperTableMeta(STableMetaMsg *pMeta, SShowObj *pShow, int32_t cols = 0; SSchema *pSchema = pMeta->schema; - pShow->bytes[cols] = (TSDB_TABLE_NAME_LEN - 1) + VARSTR_HEADER_SIZE; - pSchema[cols].type = TSDB_DATA_TYPE_BINARY; + SSchema tbnameSchema = tGetTableNameColumnSchema(); + pShow->bytes[cols] = tbnameSchema.bytes; + pSchema[cols].type = tbnameSchema.type; strcpy(pSchema[cols].name, "name"); pSchema[cols].bytes = htons(pShow->bytes[cols]); cols++; @@ -2079,8 +2080,9 @@ static int32_t mnodeGetShowTableMeta(STableMetaMsg *pMeta, SShowObj *pShow, void int32_t cols = 0; SSchema *pSchema = pMeta->schema; - pShow->bytes[cols] = (TSDB_TABLE_NAME_LEN - 1) + VARSTR_HEADER_SIZE; - pSchema[cols].type = TSDB_DATA_TYPE_BINARY; + SSchema s = tGetTableNameColumnSchema(); + pShow->bytes[cols] = s.bytes; + pSchema[cols].type = s.type; strcpy(pSchema[cols].name, "table_name"); pSchema[cols].bytes = htons(pShow->bytes[cols]); cols++; @@ -2097,8 +2099,9 @@ static int32_t mnodeGetShowTableMeta(STableMetaMsg *pMeta, SShowObj *pShow, void pSchema[cols].bytes = htons(pShow->bytes[cols]); cols++; - pShow->bytes[cols] = (TSDB_TABLE_NAME_LEN - 1) + VARSTR_HEADER_SIZE; - pSchema[cols].type = TSDB_DATA_TYPE_BINARY; + SSchema tbCol = tGetTableNameColumnSchema(); + pShow->bytes[cols] = tbCol.bytes; + pSchema[cols].type = tbCol.type; strcpy(pSchema[cols].name, "stable_name"); pSchema[cols].bytes = htons(pShow->bytes[cols]); cols++; @@ -2268,8 +2271,9 @@ static int32_t mnodeGetStreamTableMeta(STableMetaMsg *pMeta, SShowObj *pShow, vo int32_t cols = 0; SSchema *pSchema = pMeta->schema; - pShow->bytes[cols] = (TSDB_TABLE_NAME_LEN - 1) + VARSTR_HEADER_SIZE; - pSchema[cols].type = TSDB_DATA_TYPE_BINARY; + SSchema tbnameColSchema = tGetTableNameColumnSchema(); + pShow->bytes[cols] = tbnameColSchema.bytes; + pSchema[cols].type = tbnameColSchema.type; strcpy(pSchema[cols].name, "table_name"); pSchema[cols].bytes = htons(pShow->bytes[cols]); cols++; diff --git a/src/query/src/qExecutor.c b/src/query/src/qExecutor.c index cdb05d2288..ebc06a7875 100644 --- a/src/query/src/qExecutor.c +++ b/src/query/src/qExecutor.c @@ -1368,8 +1368,10 @@ static int32_t setupQueryRuntimeEnv(SQueryRuntimeEnv *pRuntimeEnv, int16_t order int32_t index = pSqlFuncMsg->colInfo.colIndex; if (TSDB_COL_IS_TAG(pIndex->flag)) { if (pIndex->colId == TSDB_TBNAME_COLUMN_INDEX) { // todo refactor - pCtx->inputBytes = (TSDB_TABLE_NAME_LEN - 1) + VARSTR_HEADER_SIZE; - pCtx->inputType = TSDB_DATA_TYPE_BINARY; + SSchema s = tGetTableNameColumnSchema(); + + pCtx->inputBytes = s.bytes; + pCtx->inputType = s.type; } else { pCtx->inputBytes = pQuery->tagColList[index].bytes; pCtx->inputType = pQuery->tagColList[index].type; @@ -5143,8 +5145,9 @@ static int32_t createQFunctionExprFromMsg(SQueryTableMsg *pQueryMsg, SExprInfo * type = TSDB_DATA_TYPE_DOUBLE; bytes = tDataTypeDesc[type].nSize; } else if (pExprs[i].base.colInfo.colId == TSDB_TBNAME_COLUMN_INDEX && pExprs[i].base.functionId == TSDB_FUNC_TAGPRJ) { // parse the normal column - type = TSDB_DATA_TYPE_BINARY; - bytes = (TSDB_TABLE_NAME_LEN - 1) + VARSTR_HEADER_SIZE; + SSchema s = tGetTableNameColumnSchema(); + type = s.type; + bytes = s.bytes; } else{ int32_t j = getColumnIndexInSource(pQueryMsg, &pExprs[i].base, pTagCols); assert(j < pQueryMsg->numOfCols || j < pQueryMsg->numOfTags || j == TSDB_TBNAME_COLUMN_INDEX); @@ -5154,10 +5157,11 @@ static int32_t createQFunctionExprFromMsg(SQueryTableMsg *pQueryMsg, SExprInfo * type = pCol->type; bytes = pCol->bytes; } else { - type = TSDB_DATA_TYPE_BINARY; - bytes = TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE; - } + SSchema s = tGetTableNameColumnSchema(); + type = s.type; + bytes = s.bytes; + } } int32_t param = pExprs[i].base.arg[0].argValue.i64; @@ -6025,6 +6029,7 @@ static void buildTagQueryResult(SQInfo* pQInfo) { qTrace("QInfo:%p create count(tbname) query, res:%d rows:1", pQInfo, count); } else { // return only the tags|table name etc. count = 0; + SSchema tbnameSchema = tGetTableNameColumnSchema(); while(pQInfo->tableIndex < num && count < pQuery->rec.capacity) { int32_t i = pQInfo->tableIndex++; @@ -6034,7 +6039,7 @@ static void buildTagQueryResult(SQInfo* pQInfo) { for(int32_t j = 0; j < pQuery->numOfOutput; ++j) { if (pExprInfo[j].base.colInfo.colId == TSDB_TBNAME_COLUMN_INDEX) { char* data = tsdbGetTableName(pQInfo->tsdb, &item->id); - char* dst = pQuery->sdata[j]->data + count * ((TSDB_TABLE_NAME_LEN - 1) + VARSTR_HEADER_SIZE); + char* dst = pQuery->sdata[j]->data + count * tbnameSchema.bytes; memcpy(dst, data, varDataTLen(data)); } else {// todo refactor int16_t type = pExprInfo[j].type; diff --git a/src/tsdb/src/tsdbRead.c b/src/tsdb/src/tsdbRead.c index d3bc45e2e4..e74ec3b06a 100644 --- a/src/tsdb/src/tsdbRead.c +++ b/src/tsdb/src/tsdbRead.c @@ -1942,7 +1942,7 @@ int32_t tableGroupComparFn(const void *p1, const void *p2, const void *param) { f1 = (char*) pTable1->name; f2 = (char*) pTable2->name; type = TSDB_DATA_TYPE_BINARY; - bytes = (TSDB_TABLE_NAME_LEN - 1) + VARSTR_HEADER_SIZE; + bytes = tGetTableNameColumnSchema().bytes; } else { STColumn* pCol = schemaColAt(pTableGroupSupp->pTagSchema, colIndex); bytes = pCol->bytes; -- GitLab