diff --git a/src/client/inc/tsclient.h b/src/client/inc/tsclient.h index c03e3be4ed..24e0f48ec9 100644 --- a/src/client/inc/tsclient.h +++ b/src/client/inc/tsclient.h @@ -285,8 +285,8 @@ typedef struct STscObj { void * pTimer; char user[TSDB_USER_LEN]; char pass[TSDB_KEY_LEN]; - char acctId[TSDB_DB_NAME_LEN]; - char db[TSDB_TABLE_ID_LEN]; + char acctId[TSDB_ACCT_LEN]; + char db[TSDB_DB_NAME_LEN]; char sversion[TSDB_VERSION_LEN]; char writeAuth : 1; char superAuth : 1; diff --git a/src/client/src/tscLocal.c b/src/client/src/tscLocal.c index 91e51f16ed..52240941f2 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, @@ -286,7 +286,7 @@ static void tscProcessCurrentUser(SSqlObj *pSql) { } static void tscProcessCurrentDB(SSqlObj *pSql) { - char db[TSDB_DB_NAME_LEN + 1] = {0}; + char db[TSDB_DB_NAME_LEN] = {0}; extractDBName(pSql->pTscObj->db, db); SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(&pSql->cmd, 0); diff --git a/src/client/src/tscParseInsert.c b/src/client/src/tscParseInsert.c index cbc83c6e75..c4749e4611 100644 --- a/src/client/src/tscParseInsert.c +++ b/src/client/src/tscParseInsert.c @@ -795,7 +795,7 @@ static int32_t tscCheckIfCreateTable(char **sqlstr, SSqlObj *pSql) { STableMetaInfo *pSTableMeterMetaInfo = tscGetMetaInfo(pQueryInfo, STABLE_INDEX); tscSetTableFullName(pSTableMeterMetaInfo, &sToken, pSql); - strncpy(pTag->name, pSTableMeterMetaInfo->name, TSDB_TABLE_ID_LEN); + tstrncpy(pTag->name, pSTableMeterMetaInfo->name, sizeof(pTag->name)); code = tscGetTableMeta(pSql, pSTableMeterMetaInfo); if (code != TSDB_CODE_SUCCESS) { return code; @@ -989,7 +989,7 @@ static int32_t tscCheckIfCreateTable(char **sqlstr, SSqlObj *pSql) { int validateTableName(char *tblName, int len) { char buf[TSDB_TABLE_ID_LEN] = {0}; - strncpy(buf, tblName, len); + tstrncpy(buf, tblName, sizeof(buf)); SSQLToken token = {.n = len, .type = TK_ID, .z = buf}; tSQLGetToken(buf, &token.type); @@ -1512,7 +1512,7 @@ void tscProcessMultiVnodesInsertFromFile(SSqlObj *pSql) { } pCmd->count = 1; - strncpy(path, pDataBlock->filename, PATH_MAX); + tstrncpy(path, pDataBlock->filename, sizeof(path)); FILE *fp = fopen(path, "r"); if (fp == NULL) { @@ -1520,7 +1520,7 @@ void tscProcessMultiVnodesInsertFromFile(SSqlObj *pSql) { continue; } - strncpy(pTableMetaInfo->name, pDataBlock->tableId, TSDB_TABLE_ID_LEN); + tstrncpy(pTableMetaInfo->name, pDataBlock->tableId, sizeof(pTableMetaInfo->name)); memset(pDataBlock->pData, 0, pDataBlock->nAllocSize); int32_t ret = tscGetTableMeta(pSql, pTableMetaInfo); diff --git a/src/client/src/tscProfile.c b/src/client/src/tscProfile.c index 79d00bf5dc..55243c4382 100644 --- a/src/client/src/tscProfile.c +++ b/src/client/src/tscProfile.c @@ -239,8 +239,7 @@ int tscBuildQueryStreamDesc(void *pMsg, STscObj *pObj) { continue; } - strncpy(pQdesc->sql, pSql->sqlstr, TSDB_SHOW_SQL_LEN - 1); - pQdesc->sql[TSDB_SHOW_SQL_LEN - 1] = 0; + tstrncpy(pQdesc->sql, pSql->sqlstr, sizeof(pQdesc->sql)); pQdesc->stime = htobe64(pSql->stime); pQdesc->queryId = htonl(pSql->queryId); pQdesc->useconds = htobe64(pSql->res.useconds); @@ -256,8 +255,7 @@ int tscBuildQueryStreamDesc(void *pMsg, STscObj *pObj) { SSqlStream *pStream = pObj->streamList; while (pStream) { - strncpy(pSdesc->sql, pStream->pSql->sqlstr, TSDB_SHOW_SQL_LEN - 1); - pSdesc->sql[TSDB_SHOW_SQL_LEN - 1] = 0; + tstrncpy(pSdesc->sql, pStream->pSql->sqlstr, sizeof(pSdesc->sql)); pSdesc->streamId = htonl(pStream->streamId); pSdesc->num = htobe64(pStream->num); diff --git a/src/client/src/tscSQLParser.c b/src/client/src/tscSQLParser.c index f29c886cba..ab2d7d7830 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 (pToken->n >= TSDB_TABLE_NAME_LEN) { return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg2); } @@ -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; @@ -1041,7 +1042,7 @@ int32_t setObjFullName(char* fullName, const char* account, SSQLToken* pDB, SSQL /* db name is not specified, the tableName dose not include db name */ if (pDB != NULL) { - if (pDB->n > TSDB_DB_NAME_LEN) { + if (pDB->n >= TSDB_DB_NAME_LEN) { return TSDB_CODE_TSC_INVALID_SQL; } @@ -1060,7 +1061,7 @@ int32_t setObjFullName(char* fullName, const char* account, SSQLToken* pDB, SSQL } } else { // pDB == NULL, the db prefix name is specified in tableName /* the length limitation includes tablename + dbname + sep */ - if (tableName->n > TSDB_TABLE_NAME_LEN + TSDB_DB_NAME_LEN + tListLen(TS_PATH_DELIMITER)) { + if (tableName->n > (TSDB_TABLE_NAME_LEN - 1) + (TSDB_DB_NAME_LEN - 1) + sizeof(TS_PATH_DELIMITER)) { return TSDB_CODE_TSC_INVALID_SQL; } } @@ -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)); @@ -1308,7 +1309,7 @@ static void addProjectQueryCol(SQueryInfo* pQueryInfo, int32_t startPos, SColumn SSchema* pSchema = tscGetTableColumnSchema(pTableMeta, pIndex->columnIndex); char* colName = (pItem->aliasName == NULL) ? pSchema->name : pItem->aliasName; - strncpy(pExpr->aliasName, colName, tListLen(pExpr->aliasName)); + tstrncpy(pExpr->aliasName, colName, sizeof(pExpr->aliasName)); SColumnList ids = {0}; ids.num = 1; @@ -1358,7 +1359,7 @@ static int32_t doAddProjectionExprAndResultFields(SQueryInfo* pQueryInfo, SColum for (int32_t j = 0; j < numOfTotalColumns; ++j) { SSqlExpr* pExpr = doAddProjectCol(pQueryInfo, startPos + j, j, pIndex->tableIndex); - strncpy(pExpr->aliasName, pSchema[j].name, tListLen(pExpr->aliasName)); + tstrncpy(pExpr->aliasName, pSchema[j].name, sizeof(pExpr->aliasName)); pIndex->columnIndex = j; SColumnList ids = {0}; @@ -1401,7 +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 + VARSTR_HEADER_SIZE}; + SSchema colSchema = {.type = TSDB_DATA_TYPE_BINARY, .bytes = (TSDB_TABLE_NAME_LEN - 1) + VARSTR_HEADER_SIZE}; strcpy(colSchema.name, TSQL_TBNAME_L); tscAddSpecialColumnForSelect(pQueryInfo, startPos, TSDB_FUNC_TAGPRJ, &index, &colSchema, true); @@ -1448,11 +1449,11 @@ 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); - strncpy(pExpr->aliasName, columnName, tListLen(pExpr->aliasName)); + tstrncpy(pExpr->aliasName, columnName, sizeof(pExpr->aliasName)); // for all queries, the timestamp column needs to be loaded SColumnIndex index = {.tableIndex = pColIndex->tableIndex, .columnIndex = PRIMARYKEY_TIMESTAMP_COL_INDEX}; @@ -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) { @@ -2202,7 +2203,7 @@ int32_t setShowInfo(SSqlObj* pSql, struct SSqlInfo* pInfo) { if (pDbPrefixToken->type != 0) { assert(pDbPrefixToken->n >= 0); - if (pDbPrefixToken->n > TSDB_DB_NAME_LEN) { // db name is too long + if (pDbPrefixToken->n >= TSDB_DB_NAME_LEN) { // db name is too long return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg3); } @@ -2229,7 +2230,7 @@ int32_t setShowInfo(SSqlObj* pSql, struct SSqlInfo* pInfo) { return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg6); } - if (pCmd->payloadLen > TSDB_TABLE_NAME_LEN) { + if (pCmd->payloadLen >= TSDB_TABLE_NAME_LEN) { return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg2); } } @@ -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); } @@ -5232,7 +5233,7 @@ 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 + VARSTR_HEADER_SIZE; // todo extract method + bytes = (TSDB_TABLE_NAME_LEN - 1) + VARSTR_HEADER_SIZE; // todo extract method name = TSQL_TBNAME_L; } else { if (TSDB_COL_IS_TAG(pColIndex->flag)) { @@ -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 59415f842e..88417addf6 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/tscServer.c b/src/client/src/tscServer.c index f96b979105..8815e463ad 100644 --- a/src/client/src/tscServer.c +++ b/src/client/src/tscServer.c @@ -866,7 +866,7 @@ int32_t tscBuildCreateDbMsg(SSqlObj *pSql, SSqlInfo *pInfo) { assert(pCmd->numOfClause == 1); STableMetaInfo *pTableMetaInfo = tscGetTableMetaInfoFromCmd(pCmd, pCmd->clauseIndex, 0); - strncpy(pCreateDbMsg->db, pTableMetaInfo->name, tListLen(pCreateDbMsg->db)); + tstrncpy(pCreateDbMsg->db, pTableMetaInfo->name, sizeof(pCreateDbMsg->db)); return TSDB_CODE_SUCCESS; } @@ -1430,9 +1430,9 @@ int tscBuildConnectMsg(SSqlObj *pSql, SSqlInfo *pInfo) { char *db; // ugly code to move the space db = strstr(pObj->db, TS_PATH_DELIMITER); db = (db == NULL) ? pObj->db : db + 1; - strcpy(pConnect->db, db); - strcpy(pConnect->clientVersion, version); - strcpy(pConnect->msgVersion, ""); + tstrncpy(pConnect->db, db, sizeof(pConnect->db)); + tstrncpy(pConnect->clientVersion, version, sizeof(pConnect->clientVersion)); + tstrncpy(pConnect->msgVersion, "", sizeof(pConnect->msgVersion)); return TSDB_CODE_SUCCESS; } @@ -2153,8 +2153,8 @@ int tscProcessConnectRsp(SSqlObj *pSql) { strcpy(pObj->acctId, pConnect->acctId); // copy acctId from response int32_t len = sprintf(temp, "%s%s%s", pObj->acctId, TS_PATH_DELIMITER, pObj->db); - assert(len <= tListLen(pObj->db)); - strncpy(pObj->db, temp, tListLen(pObj->db)); + assert(len <= sizeof(pObj->db)); + tstrncpy(pObj->db, temp, sizeof(pObj->db)); if (pConnect->ipList.numOfIps > 0) tscSetMgmtIpList(&pConnect->ipList); @@ -2338,7 +2338,7 @@ static int32_t getTableMetaFromMgmt(SSqlObj *pSql, STableMetaInfo *pTableMetaInf STableMetaInfo *pNewMeterMetaInfo = tscAddEmptyMetaInfo(pNewQueryInfo); assert(pNew->cmd.numOfClause == 1 && pNewQueryInfo->numOfTables == 1); - strncpy(pNewMeterMetaInfo->name, pTableMetaInfo->name, tListLen(pNewMeterMetaInfo->name)); + tstrncpy(pNewMeterMetaInfo->name, pTableMetaInfo->name, sizeof(pNewMeterMetaInfo->name)); memcpy(pNew->cmd.payload, pSql->cmd.payload, pSql->cmd.payloadLen); // tag information if table does not exists. pNew->cmd.payloadLen = pSql->cmd.payloadLen; tscTrace("%p new pSqlObj:%p to get tableMeta, auto create:%d", pSql, pNew, pNew->cmd.autoCreated); diff --git a/src/client/src/tscSql.c b/src/client/src/tscSql.c index aab931cf93..5f2a8598db 100644 --- a/src/client/src/tscSql.c +++ b/src/client/src/tscSql.c @@ -86,21 +86,21 @@ SSqlObj *taosConnectImpl(const char *ip, const char *user, const char *pass, con pObj->signature = pObj; - strncpy(pObj->user, user, TSDB_USER_LEN); + tstrncpy(pObj->user, user, sizeof(pObj->user)); taosEncryptPass((uint8_t *)pass, strlen(pass), pObj->pass); if (db) { int32_t len = strlen(db); /* db name is too long */ - if (len > TSDB_DB_NAME_LEN) { + if (len >= TSDB_DB_NAME_LEN) { terrno = TSDB_CODE_TSC_INVALID_DB_LENGTH; rpcClose(pDnodeConn); free(pObj); return NULL; } - char tmp[TSDB_DB_NAME_LEN + 1] = {0}; - strcpy(tmp, db); + char tmp[TSDB_DB_NAME_LEN] = {0}; + tstrncpy(tmp, db, sizeof(tmp)); strdequote(tmp); strtolower(pObj->db, tmp); diff --git a/src/client/src/tscSubquery.c b/src/client/src/tscSubquery.c index 84daf63b42..ddc41e52e5 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/tscSystem.c b/src/client/src/tscSystem.c index fd7d31aa9f..66fbf9fc97 100644 --- a/src/client/src/tscSystem.c +++ b/src/client/src/tscSystem.c @@ -180,7 +180,7 @@ static int taos_options_imp(TSDB_OPTION option, const char *pStr) { assert(cfg != NULL); if (cfg->cfgStatus <= TAOS_CFG_CSTATUS_OPTION) { - strncpy(configDir, pStr, TSDB_FILENAME_LEN); + tstrncpy(configDir, pStr, TSDB_FILENAME_LEN); cfg->cfgStatus = TAOS_CFG_CSTATUS_OPTION; tscPrint("set config file directory:%s", pStr); } else { @@ -234,7 +234,7 @@ static int taos_options_imp(TSDB_OPTION option, const char *pStr) { tscPrint("failed to set locale:%s, current locale:%s", pStr, tsLocale); } - strncpy(tsLocale, locale, tListLen(tsLocale)); + tstrncpy(tsLocale, locale, sizeof(tsLocale)); char *charset = strrchr(tsLocale, sep); if (charset != NULL) { @@ -249,7 +249,7 @@ static int taos_options_imp(TSDB_OPTION option, const char *pStr) { tscPrint("charset changed from %s to %s", tsCharset, charset); } - strncpy(tsCharset, charset, tListLen(tsCharset)); + tstrncpy(tsCharset, charset, sizeof(tsCharset)); cfg->cfgStatus = TAOS_CFG_CSTATUS_OPTION; } else { @@ -286,7 +286,7 @@ static int taos_options_imp(TSDB_OPTION option, const char *pStr) { tscPrint("charset changed from %s to %s", tsCharset, pStr); } - strncpy(tsCharset, pStr, tListLen(tsCharset)); + tstrncpy(tsCharset, pStr, sizeof(tsCharset)); cfg->cfgStatus = TAOS_CFG_CSTATUS_OPTION; } else { tscPrint("charset:%s not valid", pStr); @@ -324,7 +324,7 @@ static int taos_options_imp(TSDB_OPTION option, const char *pStr) { // return -1; // } - strncpy(tsSocketType, pStr, tListLen(tsSocketType)); + tstrncpy(tsSocketType, pStr, sizeof(tsSocketType)); cfg->cfgStatus = TAOS_CFG_CSTATUS_OPTION; tscPrint("socket type is set:%s", tsSocketType); } diff --git a/src/client/src/tscUtil.c b/src/client/src/tscUtil.c index caf424080d..a810e5d4be 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/common/src/tglobal.c b/src/common/src/tglobal.c index e010f7fe56..12754b164b 100644 --- a/src/common/src/tglobal.c +++ b/src/common/src/tglobal.c @@ -708,7 +708,7 @@ static void doInitGlobalConfig() { cfg.cfgType = TSDB_CFG_CTYPE_B_CONFIG | TSDB_CFG_CTYPE_B_CLIENT; cfg.minValue = 0; cfg.maxValue = 0; - cfg.ptrLength = TSDB_DB_NAME_LEN; + cfg.ptrLength = TSDB_DB_NAME_LEN - 1; cfg.unitType = TAOS_CFG_UTYPE_NONE; taosInitConfigOption(cfg); @@ -923,7 +923,7 @@ static void doInitGlobalConfig() { cfg.cfgType = TSDB_CFG_CTYPE_B_CONFIG | TSDB_CFG_CTYPE_B_SHOW; cfg.minValue = 0; cfg.maxValue = 0; - cfg.ptrLength = TSDB_DB_NAME_LEN; + cfg.ptrLength = TSDB_DB_NAME_LEN - 1; cfg.unitType = TAOS_CFG_UTYPE_NONE; taosInitConfigOption(cfg); diff --git a/src/common/src/tname.c b/src/common/src/tname.c index 3566f26abd..d2f5dfc4f7 100644 --- a/src/common/src/tname.c +++ b/src/common/src/tname.c @@ -28,8 +28,7 @@ void extractTableName(const char* tableId, char* name) { size_t s1 = strcspn(tableId, &TS_PATH_DELIMITER[0]); size_t s2 = strcspn(&tableId[s1 + 1], &TS_PATH_DELIMITER[0]); - strncpy(name, &tableId[s1 + s2 + 2], TSDB_TABLE_NAME_LEN); - name[TSDB_TABLE_NAME_LEN] = 0; + tstrncpy(name, &tableId[s1 + s2 + 2], TSDB_TABLE_NAME_LEN); } char* extractDBName(const char* tableId, char* name) { diff --git a/src/connector/python/linux/python3/taos/cinterface.py b/src/connector/python/linux/python3/taos/cinterface.py index c3c8a4603a..568e73e355 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 947cbe6759..bf6942be8b 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 c75fc70d75..1e0ddf0f9f 100644 --- a/src/inc/taosdef.h +++ b/src/inc/taosdef.h @@ -189,7 +189,7 @@ void tsDataSwap(void *pLeft, void *pRight, int32_t type, int32_t size); #define TSDB_USERID_LEN 9 #define TS_PATH_DELIMITER_LEN 1 -#define TSDB_METER_ID_LEN_MARGIN 10 +#define TSDB_METER_ID_LEN_MARGIN 8 #define TSDB_TABLE_ID_LEN (TSDB_DB_NAME_LEN+TSDB_TABLE_NAME_LEN+2*TS_PATH_DELIMITER_LEN+TSDB_USERID_LEN+TSDB_METER_ID_LEN_MARGIN) //TSDB_DB_NAME_LEN+TSDB_TABLE_NAME_LEN+2*strlen(TS_PATH_DELIMITER)+strlen(USERID) #define TSDB_UNI_LEN 24 #define TSDB_USER_LEN TSDB_UNI_LEN @@ -200,9 +200,9 @@ 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 192 -#define TSDB_DB_NAME_LEN 32 -#define TSDB_COL_NAME_LEN 64 +#define TSDB_TABLE_NAME_LEN 193 +#define TSDB_DB_NAME_LEN 33 +#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 88c6f9cf26..dca8287394 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; @@ -250,7 +250,7 @@ typedef struct { typedef struct { char tableId[TSDB_TABLE_ID_LEN + 1]; - char db[TSDB_DB_NAME_LEN + 1]; + char db[TSDB_DB_NAME_LEN]; int8_t igExists; int8_t getMeta; int16_t numOfTags; @@ -268,7 +268,7 @@ typedef struct { typedef struct { char tableId[TSDB_TABLE_ID_LEN + 1]; - char db[TSDB_DB_NAME_LEN + 1]; + char db[TSDB_DB_NAME_LEN]; int16_t type; /* operation type */ int16_t numOfCols; /* number of schema */ int32_t tagValLen; @@ -502,7 +502,7 @@ typedef struct { typedef struct { char acct[TSDB_USER_LEN + 1]; - char db[TSDB_DB_NAME_LEN + 1]; + char db[TSDB_DB_NAME_LEN]; int32_t cacheBlockSize; //MB int32_t totalBlocks; int32_t maxTables; @@ -606,7 +606,7 @@ typedef struct { } SMDVnodeDesc; typedef struct { - char db[TSDB_DB_NAME_LEN + 1]; + char db[TSDB_DB_NAME_LEN]; SMDVnodeCfg cfg; SMDVnodeDesc nodes[TSDB_MAX_REPLICA]; } SMDCreateVnodeMsg; @@ -671,7 +671,7 @@ typedef struct { */ typedef struct { int8_t type; - char db[TSDB_DB_NAME_LEN + 1]; + char db[TSDB_DB_NAME_LEN]; uint16_t payloadLen; char payload[]; } SCMShowMsg; diff --git a/src/kit/taosdump/taosdump.c b/src/kit/taosdump/taosdump.c index d96f80ed20..417de629aa 100644 --- a/src/kit/taosdump/taosdump.c +++ b/src/kit/taosdump/taosdump.c @@ -88,21 +88,21 @@ 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; extern char version[]; typedef struct { - char name[TSDB_DB_NAME_LEN + 1]; + char name[TSDB_DB_NAME_LEN]; int32_t replica; int32_t days; int32_t keep; diff --git a/src/mnode/inc/mnodeDef.h b/src/mnode/inc/mnodeDef.h index cf9058b9cf..35ae9aad33 100644 --- a/src/mnode/inc/mnodeDef.h +++ b/src/mnode/inc/mnodeDef.h @@ -115,7 +115,7 @@ typedef struct { typedef struct SVgObj { uint32_t vgId; - char dbName[TSDB_DB_NAME_LEN + 1]; + char dbName[TSDB_DB_NAME_LEN]; int64_t createdTime; SVnodeGid vnodeGid[TSDB_MAX_REPLICA]; int32_t numOfVnodes; @@ -154,7 +154,7 @@ typedef struct { } SDbCfg; typedef struct SDbObj { - char name[TSDB_DB_NAME_LEN + 1]; + char name[TSDB_DB_NAME_LEN]; char acct[TSDB_USER_LEN + 1]; int64_t createdTime; int32_t cfgVersion; @@ -219,7 +219,7 @@ typedef struct SAcctObj { typedef struct { int8_t type; int32_t index; - char db[TSDB_DB_NAME_LEN + 1]; + char db[TSDB_DB_NAME_LEN]; void * pIter; int16_t numOfColumns; int32_t rowSize; diff --git a/src/mnode/src/mnodeDb.c b/src/mnode/src/mnodeDb.c index 90569b4a95..c80dc3036f 100644 --- a/src/mnode/src/mnodeDb.c +++ b/src/mnode/src/mnodeDb.c @@ -328,8 +328,8 @@ static int32_t mnodeCreateDb(SAcctObj *pAcct, SCMCreateDbMsg *pCreate) { if (code != 0) return code; pDb = calloc(1, sizeof(SDbObj)); - strncpy(pDb->name, pCreate->db, TSDB_DB_NAME_LEN); - strncpy(pDb->acct, pAcct->user, TSDB_USER_LEN); + tstrncpy(pDb->name, pCreate->db, sizeof(pDb->name)); + tstrncpy(pDb->acct, pAcct->user, sizeof(pDb->acct)); pDb->createdTime = taosGetTimestampMs(); pDb->cfg = (SDbCfg) { .cacheBlockSize = pCreate->cacheBlockSize, @@ -373,7 +373,7 @@ static int32_t mnodeCreateDb(SAcctObj *pAcct, SCMCreateDbMsg *pCreate) { } bool mnodeCheckIsMonitorDB(char *db, char *monitordb) { - char dbName[TSDB_DB_NAME_LEN + 1] = {0}; + char dbName[TSDB_DB_NAME_LEN] = {0}; extractDBName(db, dbName); size_t len = strlen(dbName); @@ -453,7 +453,7 @@ static int32_t mnodeGetDbMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pConn SUserObj *pUser = mnodeGetUserFromConn(pConn); if (pUser == NULL) return 0; - pShow->bytes[cols] = TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE; + pShow->bytes[cols] = (TSDB_DB_NAME_LEN - 1) + VARSTR_HEADER_SIZE; pSchema[cols].type = TSDB_DATA_TYPE_BINARY; strcpy(pSchema[cols].name, "name"); pSchema[cols].bytes = htons(pShow->bytes[cols]); @@ -610,7 +610,7 @@ static int32_t mnodeRetrieveDbs(SShowObj *pShow, char *data, int32_t rows, void pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows; char* name = mnodeGetDbStr(pDb->name); - STR_WITH_MAXSIZE_TO_VARSTR(pWrite, name, TSDB_DB_NAME_LEN); + STR_WITH_MAXSIZE_TO_VARSTR(pWrite, name, TSDB_DB_NAME_LEN - 1); cols++; pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows; diff --git a/src/mnode/src/mnodeSdb.c b/src/mnode/src/mnodeSdb.c index b9033cdf3c..8d74827841 100644 --- a/src/mnode/src/mnodeSdb.c +++ b/src/mnode/src/mnodeSdb.c @@ -43,7 +43,7 @@ typedef enum { } ESdbStatus; typedef struct _SSdbTable { - char tableName[TSDB_DB_NAME_LEN + 1]; + char tableName[TSDB_DB_NAME_LEN]; ESdbTable tableId; ESdbKey keyType; int32_t hashSessions; diff --git a/src/mnode/src/mnodeTable.c b/src/mnode/src/mnodeTable.c index 119ba1627d..cde94c39b5 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, @@ -1078,7 +1078,7 @@ static int32_t mnodeGetShowSuperTableMeta(STableMetaMsg *pMeta, SShowObj *pShow, int32_t cols = 0; SSchema *pSchema = pMeta->schema; - pShow->bytes[cols] = TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE; + pShow->bytes[cols] = (TSDB_TABLE_NAME_LEN - 1) + VARSTR_HEADER_SIZE; pSchema[cols].type = TSDB_DATA_TYPE_BINARY; strcpy(pSchema[cols].name, "name"); pSchema[cols].bytes = htons(pShow->bytes[cols]); @@ -1138,7 +1138,7 @@ int32_t mnodeRetrieveShowSuperTables(SShowObj *pShow, char *data, int32_t rows, prefixLen = strlen(prefix); SPatternCompareInfo info = PATTERN_COMPARE_INFO_INITIALIZER; - char stableName[TSDB_TABLE_NAME_LEN + 1] = {0}; + char stableName[TSDB_TABLE_NAME_LEN] = {0}; while (numOfRows < rows) { pShow->pIter = mnodeGetNextSuperTable(pShow->pIter, &pTable); @@ -1151,7 +1151,7 @@ int32_t mnodeRetrieveShowSuperTables(SShowObj *pShow, char *data, int32_t rows, memset(stableName, 0, tListLen(stableName)); mnodeExtractTableName(pTable->info.tableId, stableName); - if (pShow->payloadLen > 0 && patternMatch(pShow->payload, stableName, TSDB_TABLE_NAME_LEN, &info) != TSDB_PATTERN_MATCH) { + if (pShow->payloadLen > 0 && patternMatch(pShow->payload, stableName, sizeof(stableName) - 1, &info) != TSDB_PATTERN_MATCH) { mnodeDecTableRef(pTable); continue; } @@ -1160,7 +1160,7 @@ int32_t mnodeRetrieveShowSuperTables(SShowObj *pShow, char *data, int32_t rows, pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows; - int16_t len = strnlen(stableName, TSDB_DB_NAME_LEN); + int16_t len = strnlen(stableName, TSDB_DB_NAME_LEN - 1); *(int16_t*) pWrite = len; pWrite += sizeof(int16_t); // todo refactor @@ -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); @@ -2079,7 +2079,7 @@ static int32_t mnodeGetShowTableMeta(STableMetaMsg *pMeta, SShowObj *pShow, void int32_t cols = 0; SSchema *pSchema = pMeta->schema; - pShow->bytes[cols] = TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE; + pShow->bytes[cols] = (TSDB_TABLE_NAME_LEN - 1) + VARSTR_HEADER_SIZE; pSchema[cols].type = TSDB_DATA_TYPE_BINARY; strcpy(pSchema[cols].name, "table_name"); pSchema[cols].bytes = htons(pShow->bytes[cols]); @@ -2097,7 +2097,7 @@ static int32_t mnodeGetShowTableMeta(STableMetaMsg *pMeta, SShowObj *pShow, void pSchema[cols].bytes = htons(pShow->bytes[cols]); cols++; - pShow->bytes[cols] = TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE; + pShow->bytes[cols] = (TSDB_TABLE_NAME_LEN - 1) + VARSTR_HEADER_SIZE; pSchema[cols].type = TSDB_DATA_TYPE_BINARY; strcpy(pSchema[cols].name, "stable_name"); pSchema[cols].bytes = htons(pShow->bytes[cols]); @@ -2141,12 +2141,12 @@ static int32_t mnodeRetrieveShowTables(SShowObj *pShow, char *data, int32_t rows continue; } - char tableName[TSDB_TABLE_NAME_LEN + 1] = {0}; + char tableName[TSDB_TABLE_NAME_LEN] = {0}; // pattern compare for table name mnodeExtractTableName(pTable->info.tableId, tableName); - if (pShow->payloadLen > 0 && patternMatch(pShow->payload, tableName, TSDB_TABLE_NAME_LEN, &info) != TSDB_PATTERN_MATCH) { + if (pShow->payloadLen > 0 && patternMatch(pShow->payload, tableName, sizeof(tableName) - 1, &info) != TSDB_PATTERN_MATCH) { mnodeDecTableRef(pTable); continue; } @@ -2155,7 +2155,7 @@ static int32_t mnodeRetrieveShowTables(SShowObj *pShow, char *data, int32_t rows char *pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows; - STR_WITH_MAXSIZE_TO_VARSTR(pWrite, tableName, TSDB_TABLE_NAME_LEN); + STR_WITH_MAXSIZE_TO_VARSTR(pWrite, tableName, sizeof(tableName) - 1); cols++; pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows; @@ -2173,10 +2173,10 @@ static int32_t mnodeRetrieveShowTables(SShowObj *pShow, char *data, int32_t rows pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows; - memset(tableName, 0, tListLen(tableName)); + memset(tableName, 0, sizeof(tableName)); if (pTable->info.type == TSDB_CHILD_TABLE) { mnodeExtractTableName(pTable->superTable->info.tableId, tableName); - STR_WITH_MAXSIZE_TO_VARSTR(pWrite, tableName, TSDB_TABLE_NAME_LEN); + STR_WITH_MAXSIZE_TO_VARSTR(pWrite, tableName, sizeof(tableName) - 1); } cols++; @@ -2268,7 +2268,7 @@ static int32_t mnodeGetStreamTableMeta(STableMetaMsg *pMeta, SShowObj *pShow, vo int32_t cols = 0; SSchema *pSchema = pMeta->schema; - pShow->bytes[cols] = TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE; + pShow->bytes[cols] = (TSDB_TABLE_NAME_LEN - 1) + VARSTR_HEADER_SIZE; pSchema[cols].type = TSDB_DATA_TYPE_BINARY; strcpy(pSchema[cols].name, "table_name"); pSchema[cols].bytes = htons(pShow->bytes[cols]); @@ -2331,12 +2331,12 @@ static int32_t mnodeRetrieveStreamTables(SShowObj *pShow, char *data, int32_t ro continue; } - char tableName[TSDB_TABLE_NAME_LEN + 1] = {0}; + char tableName[TSDB_TABLE_NAME_LEN] = {0}; // pattern compare for table name mnodeExtractTableName(pTable->info.tableId, tableName); - if (pShow->payloadLen > 0 && patternMatch(pShow->payload, tableName, TSDB_TABLE_NAME_LEN, &info) != TSDB_PATTERN_MATCH) { + if (pShow->payloadLen > 0 && patternMatch(pShow->payload, tableName, sizeof(tableName) - 1, &info) != TSDB_PATTERN_MATCH) { mnodeDecTableRef(pTable); continue; } @@ -2345,7 +2345,7 @@ static int32_t mnodeRetrieveStreamTables(SShowObj *pShow, char *data, int32_t ro char *pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows; - STR_WITH_MAXSIZE_TO_VARSTR(pWrite, tableName, TSDB_TABLE_NAME_LEN); + STR_WITH_MAXSIZE_TO_VARSTR(pWrite, tableName, sizeof(tableName) - 1); cols++; pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows; diff --git a/src/os/darwin/src/darwinPlatform.c b/src/os/darwin/src/darwinPlatform.c index 05290c43f4..cae65a2927 100644 --- a/src/os/darwin/src/darwinPlatform.c +++ b/src/os/darwin/src/darwinPlatform.c @@ -189,7 +189,7 @@ void taosGetSystemLocale() { if (cfg_locale && cfg_locale->cfgStatus < TAOS_CFG_CSTATUS_DEFAULT) { char *locale = setlocale(LC_CTYPE, "chs"); if (locale != NULL) { - strncpy(tsLocale, locale, sizeof(tsLocale) / sizeof(tsLocale[0])); + tstrncpy(tsLocale, locale, sizeof(tsLocale)); cfg_locale->cfgStatus = TAOS_CFG_CSTATUS_DEFAULT; uPrint("locale not configured, set to default:%s", tsLocale); } diff --git a/src/os/linux/src/linuxSysPara.c b/src/os/linux/src/linuxSysPara.c index b270eb14cf..3ec7de3b63 100644 --- a/src/os/linux/src/linuxSysPara.c +++ b/src/os/linux/src/linuxSysPara.c @@ -229,7 +229,7 @@ static void taosGetSystemLocale() { // get and set default locale uError("can't get locale from system, set it to en_US.UTF-8"); strcpy(tsLocale, "en_US.UTF-8"); } else { - tstrncpy(tsLocale, locale, tListLen(tsLocale)); + tstrncpy(tsLocale, locale, sizeof(tsLocale)); uError("locale not configured, set to system default:%s", tsLocale); } } @@ -242,7 +242,7 @@ static void taosGetSystemLocale() { // get and set default locale str++; char *revisedCharset = taosCharsetReplace(str); - strncpy(tsCharset, revisedCharset, tListLen(tsCharset)); + tstrncpy(tsCharset, revisedCharset, sizeof(tsCharset)); free(revisedCharset); uWarn("charset not configured, set to system default:%s", tsCharset); diff --git a/src/os/windows/src/twindows.c b/src/os/windows/src/twindows.c index 76b354f784..82bcc69b11 100644 --- a/src/os/windows/src/twindows.c +++ b/src/os/windows/src/twindows.c @@ -226,7 +226,7 @@ void taosGetSystemLocale() { if (cfg_locale && cfg_locale->cfgStatus < TAOS_CFG_CSTATUS_DEFAULT) { char *locale = setlocale(LC_CTYPE, "chs"); if (locale != NULL) { - strncpy(tsLocale, locale, sizeof(tsLocale) / sizeof(tsLocale[0])); + tstrncpy(tsLocale, locale, sizeof(tsLocale)); cfg_locale->cfgStatus = TAOS_CFG_CSTATUS_DEFAULT; uPrint("locale not configured, set to default:%s", tsLocale); } diff --git a/src/plugins/http/src/httpUtil.c b/src/plugins/http/src/httpUtil.c index b91b89e21c..1fb63ea2fc 100644 --- a/src/plugins/http/src/httpUtil.c +++ b/src/plugins/http/src/httpUtil.c @@ -307,7 +307,7 @@ void httpTrimTableName(char *name) { for (int i = 0; name[i] != 0; i++) { if (name[i] == ' ' || name[i] == ':' || name[i] == '.' || name[i] == '-' || name[i] == '/' || name[i] == '\'') name[i] = '_'; - if (i == TSDB_TABLE_NAME_LEN + 1) { + if (i == TSDB_TABLE_NAME_LEN) { name[i] = 0; break; } @@ -323,7 +323,7 @@ int httpShrinkTableName(HttpContext *pContext, int pos, char *name) { len++; } - if (len < TSDB_TABLE_NAME_LEN) { + if (len < TSDB_TABLE_NAME_LEN - 1) { return pos; } diff --git a/src/plugins/http/src/tgHandle.c b/src/plugins/http/src/tgHandle.c index c6a2230bfb..945eff7611 100644 --- a/src/plugins/http/src/tgHandle.c +++ b/src/plugins/http/src/tgHandle.c @@ -209,7 +209,7 @@ void tgParseSchemaMetric(cJSON *metric) { goto ParseEnd; } int nameLen = (int)strlen(field->valuestring); - if (nameLen == 0 || nameLen > TSDB_TABLE_NAME_LEN) { + if (nameLen == 0 || nameLen >= TSDB_TABLE_NAME_LEN) { parsedOk = false; goto ParseEnd; } @@ -409,7 +409,7 @@ bool tgProcessSingleMetric(HttpContext *pContext, cJSON *metric, char *db) { httpSendErrorResp(pContext, HTTP_TG_METRIC_NAME_NULL); return false; } - if (nameLen >= TSDB_TABLE_NAME_LEN - 7) { + if (nameLen >= TSDB_TABLE_NAME_LEN - 8) { httpSendErrorResp(pContext, HTTP_TG_METRIC_NAME_LONG); return false; } @@ -498,7 +498,7 @@ bool tgProcessSingleMetric(HttpContext *pContext, cJSON *metric, char *db) { return false; } - if (strlen(host->valuestring) >= TSDB_TABLE_NAME_LEN) { + if (strlen(host->valuestring) >= TSDB_TABLE_NAME_LEN - 1) { httpSendErrorResp(pContext, HTTP_TG_TABLE_SIZE); return false; } diff --git a/src/query/src/qExecutor.c b/src/query/src/qExecutor.c index 26502a7408..92c23f6b83 100644 --- a/src/query/src/qExecutor.c +++ b/src/query/src/qExecutor.c @@ -1368,7 +1368,7 @@ 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 + VARSTR_HEADER_SIZE; + pCtx->inputBytes = (TSDB_TABLE_NAME_LEN - 1) + VARSTR_HEADER_SIZE; pCtx->inputType = TSDB_DATA_TYPE_BINARY; } else { pCtx->inputBytes = pQuery->tagColList[index].bytes; @@ -5148,8 +5148,8 @@ static int32_t createQFunctionExprFromMsg(SQueryTableMsg *pQueryMsg, SExprInfo * 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 + VARSTR_HEADER_SIZE; - } else { + bytes = (TSDB_TABLE_NAME_LEN - 1) + VARSTR_HEADER_SIZE; + } else{ int32_t j = getColumnIndexInSource(pQueryMsg, &pExprs[i].base, pTagCols); assert(j < pQueryMsg->numOfCols || j < pQueryMsg->numOfTags || j == TSDB_TBNAME_COLUMN_INDEX); @@ -6038,7 +6038,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 + VARSTR_HEADER_SIZE); + char* dst = pQuery->sdata[j]->data + count * ((TSDB_TABLE_NAME_LEN - 1) + VARSTR_HEADER_SIZE); memcpy(dst, data, varDataTLen(data)); } else {// todo refactor int16_t type = pExprInfo[j].type; diff --git a/src/query/src/qast.c b/src/query/src/qast.c index 0a0fe56ebd..1db965e8a3 100644 --- a/src/query/src/qast.c +++ b/src/query/src/qast.c @@ -138,7 +138,7 @@ static tExprNode *tExprNodeCreate(SSchema *pSchema, int32_t numOfCols, SSQLToken memcpy(pNode->pSchema, &pSchema[i], sizeof(SSchema)); } else { pNode->pSchema->type = TSDB_DATA_TYPE_BINARY; - pNode->pSchema->bytes = TSDB_TABLE_NAME_LEN; + pNode->pSchema->bytes = TSDB_TABLE_NAME_LEN - 1; strcpy(pNode->pSchema->name, TSQL_TBNAME_L); pNode->pSchema->colId = -1; } @@ -1127,7 +1127,7 @@ tExprNode* exprTreeFromTableName(const char* tbnameCond) { left->pSchema = pSchema; pSchema->type = TSDB_DATA_TYPE_BINARY; - pSchema->bytes = TSDB_TABLE_NAME_LEN; + pSchema->bytes = TSDB_TABLE_NAME_LEN - 1; strcpy(pSchema->name, TSQL_TBNAME_L); pSchema->colId = -1; diff --git a/src/query/src/qtsbuf.c b/src/query/src/qtsbuf.c index 555ccb7318..869299f309 100644 --- a/src/query/src/qtsbuf.c +++ b/src/query/src/qtsbuf.c @@ -51,7 +51,7 @@ STSBuf* tsBufCreateFromFile(const char* path, bool autoDelete) { return NULL; } - strncpy(pTSBuf->path, path, PATH_MAX); + tstrncpy(pTSBuf->path, path, sizeof(pTSBuf->path)); pTSBuf->f = fopen(pTSBuf->path, "r+"); if (pTSBuf->f == NULL) { diff --git a/src/rpc/src/rpcMain.c b/src/rpc/src/rpcMain.c index b0a5a0bfc0..6044061127 100644 --- a/src/rpc/src/rpcMain.c +++ b/src/rpc/src/rpcMain.c @@ -458,7 +458,7 @@ int rpcGetConnInfo(void *thandle, SRpcConnInfo *pInfo) { pInfo->clientPort = pConn->peerPort; // pInfo->serverIp = pConn->destIp; - strncpy(pInfo->user, pConn->user, sizeof(pInfo->user)); + tstrncpy(pInfo->user, pConn->user, sizeof(pInfo->user)); return 0; } diff --git a/src/tsdb/src/tsdbMeta.c b/src/tsdb/src/tsdbMeta.c index 40e5667893..8eb259d38c 100644 --- a/src/tsdb/src/tsdbMeta.c +++ b/src/tsdb/src/tsdbMeta.c @@ -334,7 +334,7 @@ static STable *tsdbNewTable(STableCfg *pCfg, bool isSuper) { pTable->schema[0] = tdDupSchema(pCfg->schema); pTable->tagSchema = tdDupSchema(pCfg->tagSchema); - tsize = strnlen(pCfg->sname, TSDB_TABLE_NAME_LEN); + tsize = strnlen(pCfg->sname, TSDB_TABLE_NAME_LEN - 1); pTable->name = calloc(1, tsize + VARSTR_HEADER_SIZE + 1); if (pTable->name == NULL) { terrno = TSDB_CODE_TDB_OUT_OF_MEMORY; @@ -355,7 +355,7 @@ static STable *tsdbNewTable(STableCfg *pCfg, bool isSuper) { pTable->tableId.tid = pCfg->tableId.tid; pTable->lastKey = TSKEY_INITIAL_VAL; - tsize = strnlen(pCfg->name, TSDB_TABLE_NAME_LEN); + tsize = strnlen(pCfg->name, TSDB_TABLE_NAME_LEN - 1); pTable->name = calloc(1, tsize + VARSTR_HEADER_SIZE + 1); if (pTable->name == NULL) { terrno = TSDB_CODE_TDB_OUT_OF_MEMORY; diff --git a/src/tsdb/src/tsdbRead.c b/src/tsdb/src/tsdbRead.c index 86ce44ad7b..d3a909a162 100644 --- a/src/tsdb/src/tsdbRead.c +++ b/src/tsdb/src/tsdbRead.c @@ -2011,7 +2011,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 + VARSTR_HEADER_SIZE; + bytes = (TSDB_TABLE_NAME_LEN - 1) + VARSTR_HEADER_SIZE; } else { STColumn* pCol = schemaColAt(pTableGroupSupp->pTagSchema, colIndex); bytes = pCol->bytes; diff --git a/src/util/src/tsched.c b/src/util/src/tsched.c index 898ab70876..6fc767fd3b 100644 --- a/src/util/src/tsched.c +++ b/src/util/src/tsched.c @@ -15,6 +15,7 @@ #include "os.h" #include "taosdef.h" +#include "tutil.h" #include "tulog.h" #include "tsched.h" #include "ttimer.h" @@ -62,8 +63,7 @@ void *taosInitScheduler(int queueSize, int numOfThreads, const char *label) { } pSched->queueSize = queueSize; - strncpy(pSched->label, label, sizeof(pSched->label)); // fix buffer overflow - pSched->label[sizeof(pSched->label)-1] = '\0'; + tstrncpy(pSched->label, label, sizeof(pSched->label)); // fix buffer overflow pSched->fullSlot = 0; pSched->emptySlot = 0; diff --git a/src/util/src/ttimer.c b/src/util/src/ttimer.c index 68db574d82..cdb2c6c0be 100644 --- a/src/util/src/ttimer.c +++ b/src/util/src/ttimer.c @@ -552,8 +552,7 @@ void* taosTmrInit(int maxNumOfTmrs, int resolution, int longest, const char* lab return NULL; } - strncpy(ctrl->label, label, sizeof(ctrl->label)); - ctrl->label[sizeof(ctrl->label) - 1] = 0; + tstrncpy(ctrl->label, label, sizeof(ctrl->label)); tmrTrace("%s timer controller is initialized, number of timer controllers: %d.", label, numOfTmrCtrl); return ctrl; } diff --git a/src/util/src/tutil.c b/src/util/src/tutil.c index c524a61591..9209117415 100644 --- a/src/util/src/tutil.c +++ b/src/util/src/tutil.c @@ -582,13 +582,13 @@ bool taosGetVersionNumber(char *versionStr, int *versionNubmer) { } int taosCheckVersion(char *input_client_version, char *input_server_version, int comparedSegments) { - char client_version[64] = {0}; - char server_version[64] = {0}; + char client_version[TSDB_VERSION_LEN] = {0}; + char server_version[TSDB_VERSION_LEN] = {0}; int clientVersionNumber[4] = {0}; int serverVersionNumber[4] = {0}; - strcpy(client_version, input_client_version); - strcpy(server_version, input_server_version); + tstrncpy(client_version, input_client_version, sizeof(client_version)); + tstrncpy(server_version, input_server_version, sizeof(server_version)); if (!taosGetVersionNumber(client_version, clientVersionNumber)) { uError("invalid client version:%s", client_version); diff --git a/src/vnode/inc/vnodeInt.h b/src/vnode/inc/vnodeInt.h index 7c95e81cf5..ab74e329e6 100644 --- a/src/vnode/inc/vnodeInt.h +++ b/src/vnode/inc/vnodeInt.h @@ -51,7 +51,7 @@ typedef struct { SSyncCfg syncCfg; SWalCfg walCfg; char *rootDir; - char db[TSDB_DB_NAME_LEN + 1]; + char db[TSDB_DB_NAME_LEN]; } SVnodeObj; int vnodeWriteToQueue(void *param, void *pHead, int type); diff --git a/tests/pytest/table/boundary.py b/tests/pytest/table/boundary.py index bb5e187f0d..d9f0490ed4 100644 --- a/tests/pytest/table/boundary.py +++ b/tests/pytest/table/boundary.py @@ -96,14 +96,14 @@ class TDTestCase: tdLog.notice("table name max length is %d" % maxTableNameLen) # create a super table with name exceed max length - sname = self.generateString(maxTableNameLen + 1) + sname = self.generateString(maxTableNameLen) tdLog.info("create a super table with length %d" % len(sname)) tdSql.error( "create table %s (ts timestamp, value int) tags(id int)" % sname) # create a super table with name of max length - sname = self.generateString(maxTableNameLen) + sname = self.generateString(maxTableNameLen - 1) tdLog.info("create a super table with length %d" % len(sname)) tdSql.execute( "create table %s (ts timestamp, value int) tags(id int)" % @@ -113,12 +113,12 @@ class TDTestCase: tdSql.checkRows(1) # create a child table with name exceed max length - name = self.generateString(maxTableNameLen + 1) + name = self.generateString(maxTableNameLen) tdLog.info("create a child table with length %d" % len(name)) tdSql.error("create table %s using %s tags(0)" % (name, sname)) # create a child table with name of max length - name = self.generateString(maxTableNameLen) + name = self.generateString(maxTableNameLen - 1) tdLog.info("create a child table with length %d" % len(name)) tdSql.execute("create table %s using %s tags(0)" % (name, sname)) tdSql.query('show tables') diff --git a/tests/pytest/table/tablename-boundary.py b/tests/pytest/table/tablename-boundary.py index ce843c3fe7..6ce986c51e 100644 --- a/tests/pytest/table/tablename-boundary.py +++ b/tests/pytest/table/tablename-boundary.py @@ -18,9 +18,7 @@ class TDTestCase: tdSql.prepare() getTableNameLen = "grep -w '#define TSDB_TABLE_NAME_LEN' ../../src/inc/taosdef.h|awk '{print $3}'" - tableNameMaxLen = int( - subprocess.check_output( - getTableNameLen, shell=True)) + tableNameMaxLen = int( subprocess.check_output(getTableNameLen, shell=True)) - 1 tdLog.info("table name max length is %d" % tableNameMaxLen) chars = string.ascii_uppercase + string.ascii_lowercase tb_name = ''.join(random.choices(chars, k=tableNameMaxLen)) diff --git a/tests/tsim/src/simParse.c b/tests/tsim/src/simParse.c index fddda679c6..9fea1f115b 100644 --- a/tests/tsim/src/simParse.c +++ b/tests/tsim/src/simParse.c @@ -165,7 +165,7 @@ SScript *simBuildScriptObj(char *fileName) { script->type = SIM_SCRIPT_TYPE_MAIN; script->numOfLines = numOfLines; - strncpy(script->fileName, fileName, MAX_FILE_NAME_LEN); + tstrncpy(script->fileName, fileName, sizeof(script->fileName)); script->optionBuffer = malloc(optionOffset); memcpy(script->optionBuffer, optionBuffer, optionOffset);