diff --git a/src/client/src/tscLocal.c b/src/client/src/tscLocal.c index 295b6912756cb10da25417da6f0ea86c04097dec..52240941f2f80be75afc0832e97acf8d421e5743 100644 --- a/src/client/src/tscLocal.c +++ b/src/client/src/tscLocal.c @@ -132,7 +132,7 @@ static int32_t tscSetValueToResObj(SSqlObj *pSql, int32_t rowLen) { for (int32_t i = 0; i < numOfRows; ++i) { TAOS_FIELD *pField = tscFieldInfoGetField(&pQueryInfo->fieldsInfo, 0); char* dst = pRes->data + tscFieldInfoGetOffset(pQueryInfo, 0) * totalNumOfRows + pField->bytes * i; - STR_WITH_MAXSIZE_TO_VARSTR(dst, pSchema[i].name, TSDB_COL_NAME_LEN); + STR_WITH_MAXSIZE_TO_VARSTR(dst, pSchema[i].name, TSDB_COL_NAME_LEN - 1); char *type = tDataTypeDesc[pSchema[i].type].aName; @@ -171,7 +171,7 @@ static int32_t tscSetValueToResObj(SSqlObj *pSql, int32_t rowLen) { // field name TAOS_FIELD *pField = tscFieldInfoGetField(&pQueryInfo->fieldsInfo, 0); char* output = pRes->data + tscFieldInfoGetOffset(pQueryInfo, 0) * totalNumOfRows + pField->bytes * i; - STR_WITH_MAXSIZE_TO_VARSTR(output, pSchema[i].name, TSDB_COL_NAME_LEN); + STR_WITH_MAXSIZE_TO_VARSTR(output, pSchema[i].name, TSDB_COL_NAME_LEN - 1); // type name pField = tscFieldInfoGetField(&pQueryInfo->fieldsInfo, 1); @@ -211,18 +211,18 @@ static int32_t tscBuildTableSchemaResultFields(SSqlObj *pSql, int32_t numOfCols, SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(&pSql->cmd, 0); pQueryInfo->order.order = TSDB_ORDER_ASC; - TAOS_FIELD f = {.type = TSDB_DATA_TYPE_BINARY, .bytes = TSDB_COL_NAME_LEN + VARSTR_HEADER_SIZE}; - strncpy(f.name, "Field", TSDB_COL_NAME_LEN); + TAOS_FIELD f = {.type = TSDB_DATA_TYPE_BINARY, .bytes = (TSDB_COL_NAME_LEN - 1) + VARSTR_HEADER_SIZE}; + tstrncpy(f.name, "Field", sizeof(f.name)); SFieldSupInfo* pInfo = tscFieldInfoAppend(&pQueryInfo->fieldsInfo, &f); pInfo->pSqlExpr = tscSqlExprAppend(pQueryInfo, TSDB_FUNC_TS_DUMMY, &index, TSDB_DATA_TYPE_BINARY, - TSDB_COL_NAME_LEN + VARSTR_HEADER_SIZE, TSDB_COL_NAME_LEN, false); + (TSDB_COL_NAME_LEN - 1) + VARSTR_HEADER_SIZE, (TSDB_COL_NAME_LEN - 1), false); - rowLen += (TSDB_COL_NAME_LEN + VARSTR_HEADER_SIZE); + rowLen += ((TSDB_COL_NAME_LEN - 1) + VARSTR_HEADER_SIZE); f.bytes = typeColLength; f.type = TSDB_DATA_TYPE_BINARY; - strncpy(f.name, "Type", TSDB_COL_NAME_LEN); + tstrncpy(f.name, "Type", sizeof(f.name)); pInfo = tscFieldInfoAppend(&pQueryInfo->fieldsInfo, &f); pInfo->pSqlExpr = tscSqlExprAppend(pQueryInfo, TSDB_FUNC_TS_DUMMY, &index, TSDB_DATA_TYPE_BINARY, typeColLength, @@ -232,7 +232,7 @@ static int32_t tscBuildTableSchemaResultFields(SSqlObj *pSql, int32_t numOfCols, f.bytes = sizeof(int32_t); f.type = TSDB_DATA_TYPE_INT; - strncpy(f.name, "Length", TSDB_COL_NAME_LEN); + tstrncpy(f.name, "Length", sizeof(f.name)); pInfo = tscFieldInfoAppend(&pQueryInfo->fieldsInfo, &f); pInfo->pSqlExpr = tscSqlExprAppend(pQueryInfo, TSDB_FUNC_TS_DUMMY, &index, TSDB_DATA_TYPE_INT, sizeof(int32_t), @@ -242,7 +242,7 @@ static int32_t tscBuildTableSchemaResultFields(SSqlObj *pSql, int32_t numOfCols, f.bytes = noteColLength; f.type = TSDB_DATA_TYPE_BINARY; - strncpy(f.name, "Note", TSDB_COL_NAME_LEN); + tstrncpy(f.name, "Note", sizeof(f.name)); pInfo = tscFieldInfoAppend(&pQueryInfo->fieldsInfo, &f); pInfo->pSqlExpr = tscSqlExprAppend(pQueryInfo, TSDB_FUNC_TS_DUMMY, &index, TSDB_DATA_TYPE_BINARY, noteColLength, diff --git a/src/client/src/tscSQLParser.c b/src/client/src/tscSQLParser.c index ef22333836e574b66d78a4e11defddc449503782..5167add6cbcbb0c512297542a54f45536e7a532b 100644 --- a/src/client/src/tscSQLParser.c +++ b/src/client/src/tscSQLParser.c @@ -933,7 +933,7 @@ bool validateOneTags(SSqlCmd* pCmd, TAOS_FIELD* pTagField) { SSchema* pSchema = tscGetTableSchema(pTableMeta); for (int32_t i = 0; i < numOfTags + numOfCols; ++i) { - if (strncasecmp(pTagField->name, pSchema[i].name, TSDB_COL_NAME_LEN) == 0) { + if (strncasecmp(pTagField->name, pSchema[i].name, sizeof(pTagField->name) - 1) == 0) { invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg2); return false; } @@ -993,7 +993,7 @@ bool validateOneColumn(SSqlCmd* pCmd, TAOS_FIELD* pColField) { // field name must be unique for (int32_t i = 0; i < numOfTags + numOfCols; ++i) { - if (strncasecmp(pColField->name, pSchema[i].name, TSDB_COL_NAME_LEN) == 0) { + if (strncasecmp(pColField->name, pSchema[i].name, sizeof(pColField->name) - 1) == 0) { invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg2); return false; } @@ -1005,7 +1005,8 @@ bool validateOneColumn(SSqlCmd* pCmd, TAOS_FIELD* pColField) { /* is contained in pFieldList or not */ static bool has(tFieldList* pFieldList, int32_t startIdx, const char* name) { for (int32_t j = startIdx; j < pFieldList->nField; ++j) { - if (strncasecmp(name, pFieldList->p[j].name, TSDB_COL_NAME_LEN) == 0) return true; + TAOS_FIELD* field = pFieldList->p + j; + if (strncasecmp(name, field->name, sizeof(field->name) - 1) == 0) return true; } return false; @@ -1166,7 +1167,7 @@ int32_t parseSelectClause(SSqlCmd* pCmd, int32_t clauseIndex, tSQLExprList* pSel /* todo alias name should use the original sql string */ char* name = (pItem->aliasName != NULL)? pItem->aliasName:arithmeticExprStr; - strncpy(pExpr->aliasName, name, TSDB_COL_NAME_LEN); + tstrncpy(pExpr->aliasName, name, sizeof(pExpr->aliasName)); tExprNode* pNode = NULL; SArray* colList = taosArrayInit(10, sizeof(SColIndex)); @@ -1448,7 +1449,7 @@ static int32_t setExprInfoForFunctions(SQueryInfo* pQueryInfo, SSchema* pSchema, if (aliasName != NULL) { strcpy(columnName, aliasName); } else { - getRevisedName(columnName, functionID, TSDB_COL_NAME_LEN, pSchema[pColIndex->columnIndex].name); + getRevisedName(columnName, functionID, sizeof(columnName) - 1, pSchema[pColIndex->columnIndex].name); } SSqlExpr* pExpr = tscSqlExprAppend(pQueryInfo, functionID, pColIndex, type, bytes, bytes, false); @@ -1536,7 +1537,7 @@ int32_t addExprAndResultField(SQueryInfo* pQueryInfo, int32_t colIndex, tSQLExpr } memset(pExpr->aliasName, 0, tListLen(pExpr->aliasName)); - getColumnName(pItem, pExpr->aliasName, TSDB_COL_NAME_LEN); + getColumnName(pItem, pExpr->aliasName, sizeof(pExpr->aliasName) - 1); SColumnList ids = getColumnList(1, index.tableIndex, index.columnIndex); if (finalResult) { @@ -1651,7 +1652,7 @@ int32_t addExprAndResultField(SQueryInfo* pQueryInfo, int32_t colIndex, tSQLExpr ids.ids[0] = index; memset(pExpr->aliasName, 0, tListLen(pExpr->aliasName)); - getColumnName(pItem, pExpr->aliasName, TSDB_COL_NAME_LEN); + getColumnName(pItem, pExpr->aliasName, sizeof(pExpr->aliasName) - 1); if (finalResult) { int32_t numOfOutput = tscNumOfFields(pQueryInfo); @@ -1851,7 +1852,7 @@ int32_t addExprAndResultField(SQueryInfo* pQueryInfo, int32_t colIndex, tSQLExpr } memset(pExpr->aliasName, 0, tListLen(pExpr->aliasName)); - getColumnName(pItem, pExpr->aliasName, TSDB_COL_NAME_LEN); + getColumnName(pItem, pExpr->aliasName, sizeof(pExpr->aliasName) - 1); SColumnList ids = getColumnList(1, 0, index.columnIndex); if (finalResult) { @@ -3997,7 +3998,7 @@ int32_t tsRewriteFieldNameIfNecessary(SQueryInfo* pQueryInfo) { for (int32_t i = 0; i < pQueryInfo->fieldsInfo.numOfOutput; ++i) { char* fieldName = tscFieldInfoGetField(&pQueryInfo->fieldsInfo, i)->name; - for (int32_t j = 0; j < TSDB_COL_NAME_LEN && fieldName[j] != 0; ++j) { + for (int32_t j = 0; j < (TSDB_COL_NAME_LEN - 1) && fieldName[j] != 0; ++j) { for (int32_t k = 0; k < tListLen(rep); ++k) { if (fieldName[j] == rep[k]) { fieldName[j] = '_'; @@ -4013,7 +4014,7 @@ int32_t tsRewriteFieldNameIfNecessary(SQueryInfo* pQueryInfo) { for (int32_t i = 0; i < pQueryInfo->fieldsInfo.numOfOutput; ++i) { char* fieldName = tscFieldInfoGetField(&pQueryInfo->fieldsInfo, i)->name; for (int32_t j = i + 1; j < pQueryInfo->fieldsInfo.numOfOutput; ++j) { - if (strncasecmp(fieldName, tscFieldInfoGetField(&pQueryInfo->fieldsInfo, j)->name, TSDB_COL_NAME_LEN) == 0) { + if (strncasecmp(fieldName, tscFieldInfoGetField(&pQueryInfo->fieldsInfo, j)->name, (TSDB_COL_NAME_LEN - 1)) == 0) { const char* msg = "duplicated column name in new table"; return invalidSqlErrMsg(pQueryInfo->msg, msg); } @@ -4368,7 +4369,7 @@ int32_t setAlterTableInfo(SSqlObj* pSql, struct SSqlInfo* pInfo) { } tVariantListItem* pItem = &pAlterSQL->varList->a[0]; - if (pItem->pVar.nLen > TSDB_COL_NAME_LEN) { + if (pItem->pVar.nLen >= TSDB_COL_NAME_LEN) { return invalidSqlErrMsg(pQueryInfo->msg, msg9); } @@ -4420,7 +4421,7 @@ int32_t setAlterTableInfo(SSqlObj* pSql, struct SSqlInfo* pInfo) { return TSDB_CODE_TSC_INVALID_SQL; } - char name[TSDB_COL_NAME_LEN + 1] = {0}; + char name[TSDB_COL_NAME_LEN] = {0}; strncpy(name, pVarList->a[0].pVar.pz, pVarList->a[0].pVar.nLen); TAOS_FIELD f = tscCreateField(TSDB_DATA_TYPE_INT, name, tDataTypeDesc[TSDB_DATA_TYPE_INT].nSize); tscFieldInfoAppend(&pQueryInfo->fieldsInfo, &f); @@ -4522,8 +4523,8 @@ int32_t setAlterTableInfo(SSqlObj* pSql, struct SSqlInfo* pInfo) { return invalidSqlErrMsg(pQueryInfo->msg, msg18); } - char name1[TSDB_COL_NAME_LEN + 1] = {0}; - strncpy(name1, pItem->pVar.pz, pItem->pVar.nLen); + char name1[TSDB_COL_NAME_LEN] = {0}; + tstrncpy(name1, pItem->pVar.pz, sizeof(name1)); TAOS_FIELD f = tscCreateField(TSDB_DATA_TYPE_INT, name1, tDataTypeDesc[TSDB_DATA_TYPE_INT].nSize); tscFieldInfoAppend(&pQueryInfo->fieldsInfo, &f); } @@ -5254,8 +5255,8 @@ static int32_t doAddGroupbyColumnsOnDemand(SQueryInfo* pQueryInfo) { SColumnIndex index = {.tableIndex = pQueryInfo->groupbyExpr.tableIndex, .columnIndex = colIndex}; SSqlExpr* pExpr = tscSqlExprAppend(pQueryInfo, TSDB_FUNC_TAG, &index, type, bytes, bytes, true); - memset(pExpr->aliasName, 0, tListLen(pExpr->aliasName)); - strncpy(pExpr->aliasName, name, TSDB_COL_NAME_LEN); + memset(pExpr->aliasName, 0, sizeof(pExpr->aliasName)); + tstrncpy(pExpr->aliasName, name, sizeof(pExpr->aliasName)); pExpr->colInfo.flag = TSDB_COL_TAG; @@ -6019,7 +6020,7 @@ int32_t exprTreeFromSqlExpr(tExprNode **pExpr, const tSQLExpr* pSqlExpr, SArray* if (pCols != NULL) { // record the involved columns SColIndex colIndex = {0}; - strncpy(colIndex.name, pSchema->name, TSDB_COL_NAME_LEN); + tstrncpy(colIndex.name, pSchema->name, sizeof(colIndex.name)); colIndex.colId = pSchema->colId; colIndex.colIndex = index.columnIndex; diff --git a/src/client/src/tscSchemaUtil.c b/src/client/src/tscSchemaUtil.c index 59415f842e8763373aa3d089130eca1d320f80c5..88417addf635fd3cf0824cf6774112c1bfd5a05f 100644 --- a/src/client/src/tscSchemaUtil.c +++ b/src/client/src/tscSchemaUtil.c @@ -115,7 +115,7 @@ bool isValidSchema(struct SSchema* pSchema, int32_t numOfCols) { // 3. valid column names for (int32_t j = i + 1; j < numOfCols; ++j) { - if (strncasecmp(pSchema[i].name, pSchema[j].name, TSDB_COL_NAME_LEN) == 0) { + if (strncasecmp(pSchema[i].name, pSchema[j].name, sizeof(pSchema[i].name) - 1) == 0) { return false; } } diff --git a/src/client/src/tscSubquery.c b/src/client/src/tscSubquery.c index 84daf63b42cca54dae579783ef6cc2747c69da7c..ddc41e52e52705d2838bef337996f50708bc23c0 100644 --- a/src/client/src/tscSubquery.c +++ b/src/client/src/tscSubquery.c @@ -1921,7 +1921,7 @@ static char *getArithemicInputSrc(void *param, const char *name, int32_t colId) for (int32_t i = 0; i < pSupport->numOfCols; ++i) { pExpr = taosArrayGetP(pSupport->exprList, i); - if (strncmp(name, pExpr->aliasName, TSDB_COL_NAME_LEN) == 0) { + if (strncmp(name, pExpr->aliasName, sizeof(pExpr->aliasName) - 1) == 0) { index = i; break; } diff --git a/src/client/src/tscUtil.c b/src/client/src/tscUtil.c index caf424080de9726497e06704191cbe542fcdfc23..a810e5d4be748ef07a7de52fc6b2b018e13eb317 100644 --- a/src/client/src/tscUtil.c +++ b/src/client/src/tscUtil.c @@ -801,7 +801,7 @@ int tscAllocPayload(SSqlCmd* pCmd, int size) { TAOS_FIELD tscCreateField(int8_t type, const char* name, int16_t bytes) { TAOS_FIELD f = { .type = type, .bytes = bytes, }; - strncpy(f.name, name, TSDB_COL_NAME_LEN); + tstrncpy(f.name, name, sizeof(f.name)); return f; } @@ -966,12 +966,12 @@ static SSqlExpr* doBuildSqlExpr(SQueryInfo* pQueryInfo, int16_t functionId, SCol if (isTagCol) { SSchema* pSchema = tscGetTableTagSchema(pTableMetaInfo->pTableMeta); pExpr->colInfo.colId = pSchema[pColIndex->columnIndex].colId; - strncpy(pExpr->colInfo.name, pSchema[pColIndex->columnIndex].name, TSDB_COL_NAME_LEN); + tstrncpy(pExpr->colInfo.name, pSchema[pColIndex->columnIndex].name, sizeof(pExpr->colInfo.name)); } else if (pTableMetaInfo->pTableMeta != NULL) { // in handling select database/version/server_status(), the pTableMeta is NULL SSchema* pSchema = tscGetTableColumnSchema(pTableMetaInfo->pTableMeta, pColIndex->columnIndex); pExpr->colInfo.colId = pSchema->colId; - strncpy(pExpr->colInfo.name, pSchema->name, TSDB_COL_NAME_LEN); + tstrncpy(pExpr->colInfo.name, pSchema->name, sizeof(pExpr->colInfo.name)); } } diff --git a/src/connector/python/linux/python3/taos/cinterface.py b/src/connector/python/linux/python3/taos/cinterface.py index c3c8a4603a3b7dcacea7fa3561cdd455d2e953fa..568e73e355cd774bdf4ffa2e563ae761c25e49ae 100644 --- a/src/connector/python/linux/python3/taos/cinterface.py +++ b/src/connector/python/linux/python3/taos/cinterface.py @@ -130,9 +130,9 @@ _CONVERT_FUNC = { # Corresponding TAOS_FIELD structure in C class TaosField(ctypes.Structure): - _fields_ = [('name', ctypes.c_char * 64), - ('bytes', ctypes.c_short), - ('type', ctypes.c_char)] + _fields_ = [('name', ctypes.c_char * 65), + ('type', ctypes.c_char), + ('bytes', ctypes.c_short)] # C interface class class CTaosInterface(object): diff --git a/src/inc/taos.h b/src/inc/taos.h index 947cbe6759a6dfe580f7972d3f0b8c7eebeef5ff..bf6942be8b7ddff9ffff4af222fb5e938e63d096 100644 --- a/src/inc/taos.h +++ b/src/inc/taos.h @@ -53,9 +53,9 @@ typedef enum { } TSDB_OPTION; typedef struct taosField { - char name[64]; - short bytes; + char name[65]; uint8_t type; + short bytes; } TAOS_FIELD; #ifdef _TD_GO_DLL_ diff --git a/src/inc/taosdef.h b/src/inc/taosdef.h index b0e01e087ee0cc110f6fa0ddce231e0d21f94928..1e0ddf0f9f23a10f5e5ad573d78488b2892f896f 100644 --- a/src/inc/taosdef.h +++ b/src/inc/taosdef.h @@ -202,7 +202,7 @@ void tsDataSwap(void *pLeft, void *pRight, int32_t type, int32_t size); #define TSDB_NODE_NAME_LEN 64 #define TSDB_TABLE_NAME_LEN 193 #define TSDB_DB_NAME_LEN 33 -#define TSDB_COL_NAME_LEN 64 +#define TSDB_COL_NAME_LEN 65 #define TSDB_MAX_SAVED_SQL_LEN TSDB_MAX_COLUMNS * 64 #define TSDB_MAX_SQL_LEN TSDB_PAYLOAD_SIZE #define TSDB_MAX_SQL_SHOW_LEN 256 diff --git a/src/inc/taosmsg.h b/src/inc/taosmsg.h index c85bb229b9d37c617e51cb702882b6bd5995d951..dca8287394f3a6e401c0527daa238875fb88f693 100644 --- a/src/inc/taosmsg.h +++ b/src/inc/taosmsg.h @@ -224,7 +224,7 @@ typedef struct { typedef struct SSchema { uint8_t type; - char name[TSDB_COL_NAME_LEN + 1]; + char name[TSDB_COL_NAME_LEN]; int16_t colId; int16_t bytes; } SSchema; diff --git a/src/kit/taosdump/taosdump.c b/src/kit/taosdump/taosdump.c index c588dce20d6dde26abd022d06580cdbd8a323367..417de629aa5d1724d18fc3fb393d4b0b300a9b57 100644 --- a/src/kit/taosdump/taosdump.c +++ b/src/kit/taosdump/taosdump.c @@ -88,14 +88,14 @@ enum _describe_table_index { }; typedef struct { - char field[TSDB_COL_NAME_LEN + 1]; + char field[TSDB_COL_NAME_LEN]; char type[16]; int length; char note[128]; } SColDes; typedef struct { - char name[TSDB_COL_NAME_LEN + 1]; + char name[TSDB_COL_NAME_LEN]; SColDes cols[]; } STableDef; diff --git a/src/mnode/src/mnodeTable.c b/src/mnode/src/mnodeTable.c index ac826c9781d0cf24823c29e119e331d7f4d03ba7..cde94c39b5d230eaa69cb84a57504d9e1e5022ce 100644 --- a/src/mnode/src/mnodeTable.c +++ b/src/mnode/src/mnodeTable.c @@ -951,7 +951,7 @@ static int32_t mnodeModifySuperTableTagName(SSuperTableObj *pStable, char *oldTa // update SSchema *schema = (SSchema *) (pStable->schema + pStable->numOfColumns + col); - strncpy(schema->name, newTagName, TSDB_COL_NAME_LEN); + tstrncpy(schema->name, newTagName, sizeof(schema->name)); SSdbOper oper = { .type = SDB_OPER_GLOBAL, @@ -1228,7 +1228,7 @@ static int32_t mnodeSetSchemaFromSuperTable(SSchema *pSchema, SSuperTableObj *pT assert(numOfCols <= TSDB_MAX_COLUMNS); for (int32_t i = 0; i < numOfCols; ++i) { - strncpy(pSchema->name, pTable->schema[i].name, TSDB_COL_NAME_LEN); + tstrncpy(pSchema->name, pTable->schema[i].name, sizeof(pSchema->name)); pSchema->type = pTable->schema[i].type; pSchema->bytes = htons(pTable->schema[i].bytes); pSchema->colId = htons(pTable->schema[i].colId);