fix: buff size

This commit is contained in:
xsren 2024-10-09 09:45:56 +08:00
parent 7f2a2cfbd0
commit e88a8317af
7 changed files with 53 additions and 47 deletions

View File

@ -29,6 +29,6 @@ int32_t qExecExplainBegin(SQueryPlan *pDag, SExplainCtx **pCtx, int64_t startTs)
int32_t qExecExplainEnd(SExplainCtx *pCtx, SRetrieveTableRsp **pRsp); int32_t qExecExplainEnd(SExplainCtx *pCtx, SRetrieveTableRsp **pRsp);
int32_t qExplainUpdateExecInfo(SExplainCtx *pCtx, SExplainRsp *pRspMsg, int32_t groupId, SRetrieveTableRsp **pRsp); int32_t qExplainUpdateExecInfo(SExplainCtx *pCtx, SExplainRsp *pRspMsg, int32_t groupId, SRetrieveTableRsp **pRsp);
void qExplainFreeCtx(SExplainCtx *pCtx); void qExplainFreeCtx(SExplainCtx *pCtx);
int32_t formatDurationOrKeep(char* buffer, int32_t timeInMinutes); int32_t formatDurationOrKeep(char* buffer, int64_t bufSize, int32_t timeInMinutes);
#endif #endif

View File

@ -336,7 +336,7 @@ char* jobTaskStatusStr(int32_t status);
SSchema createSchema(int8_t type, int32_t bytes, col_id_t colId, const char* name); SSchema createSchema(int8_t type, int32_t bytes, col_id_t colId, const char* name);
void destroyQueryExecRes(SExecResult* pRes); void destroyQueryExecRes(SExecResult* pRes);
int32_t dataConverToStr(char* str, int type, void* buf, int32_t bufSize, int32_t* len); int32_t dataConverToStr(char* str, int64_t capacity, int type, void* buf, int32_t bufSize, int32_t* len);
void parseTagDatatoJson(void* p, char** jsonStr); void parseTagDatatoJson(void* p, char** jsonStr);
int32_t cloneTableMeta(STableMeta* pSrc, STableMeta** pDst); int32_t cloneTableMeta(STableMeta* pSrc, STableMeta** pDst);
void getColumnTypeFromMeta(STableMeta* pMeta, char* pName, ETableColumnType* pType); void getColumnTypeFromMeta(STableMeta* pMeta, char* pName, ETableColumnType* pType);

View File

@ -458,17 +458,17 @@ static void buildChildElement(cJSON* json, SVCreateTbReq* pCreateReq) {
cJSON* tvalue = NULL; cJSON* tvalue = NULL;
if (IS_VAR_DATA_TYPE(pTagVal->type)) { if (IS_VAR_DATA_TYPE(pTagVal->type)) {
char* buf = NULL; char* buf = NULL;
int64_t bufSize = 0;
if (pTagVal->type == TSDB_DATA_TYPE_VARBINARY) { if (pTagVal->type == TSDB_DATA_TYPE_VARBINARY) {
buf = taosMemoryCalloc(pTagVal->nData * 2 + 2 + 3, 1); bufSize = pTagVal->nData * 2 + 2 + 3;
} else if (IS_VAR_DATA_TYPE(pTagVal->type)) {
buf = taosMemoryCalloc(pTagVal->nData + 3, 1);
} else { } else {
buf = taosMemoryCalloc(32, 1); bufSize = pTagVal->nData + 3;
} }
buf = taosMemoryCalloc(bufSize, 1);
RAW_NULL_CHECK(buf); RAW_NULL_CHECK(buf);
if (!buf) goto end; if (!buf) goto end;
if (dataConverToStr(buf, pTagVal->type, pTagVal->pData, pTagVal->nData, NULL) != TSDB_CODE_SUCCESS) { if (dataConverToStr(buf, bufSize, pTagVal->type, pTagVal->pData, pTagVal->nData, NULL) != TSDB_CODE_SUCCESS) {
taosMemoryFree(buf); taosMemoryFree(buf);
goto end; goto end;
} }
@ -738,13 +738,15 @@ static void processAlterTable(SMqMetaRsp* metaRsp, cJSON** pJson) {
goto end; goto end;
} }
} else { } else {
int64_t bufSize = 0;
if (vAlterTbReq.tagType == TSDB_DATA_TYPE_VARBINARY) { if (vAlterTbReq.tagType == TSDB_DATA_TYPE_VARBINARY) {
buf = taosMemoryCalloc(vAlterTbReq.nTagVal * 2 + 2 + 3, 1); bufSize = vAlterTbReq.nTagVal * 2 + 2 + 3;
} else { } else {
buf = taosMemoryCalloc(vAlterTbReq.nTagVal + 3, 1); bufSize = vAlterTbReq.nTagVal + 3;
} }
buf = taosMemoryCalloc(bufSize, 1);
RAW_NULL_CHECK(buf); RAW_NULL_CHECK(buf);
if (dataConverToStr(buf, vAlterTbReq.tagType, vAlterTbReq.pTagVal, vAlterTbReq.nTagVal, NULL) != if (dataConverToStr(buf, bufSize, vAlterTbReq.tagType, vAlterTbReq.pTagVal, vAlterTbReq.nTagVal, NULL) !=
TSDB_CODE_SUCCESS) { TSDB_CODE_SUCCESS) {
taosMemoryFree(buf); taosMemoryFree(buf);
goto end; goto end;

View File

@ -2366,7 +2366,7 @@ static void mndDumpDbInfoData(SMnode *pMnode, SSDataBlock *pBlock, SDbObj *pDb,
TAOS_CHECK_GOTO(colDataSetVal(pColInfo, rows, (const char *)strictVstr, false), &lino, _OVER); TAOS_CHECK_GOTO(colDataSetVal(pColInfo, rows, (const char *)strictVstr, false), &lino, _OVER);
char durationVstr[128] = {0}; char durationVstr[128] = {0};
int32_t len = formatDurationOrKeep(&durationVstr[VARSTR_HEADER_SIZE], pDb->cfg.daysPerFile); int32_t len = formatDurationOrKeep(&durationVstr[VARSTR_HEADER_SIZE], sizeof(durationVstr) - VARSTR_HEADER_SIZE, pDb->cfg.daysPerFile);
varDataSetLen(durationVstr, len); varDataSetLen(durationVstr, len);
pColInfo = taosArrayGet(pBlock->pDataBlock, cols++); pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
@ -2377,9 +2377,9 @@ static void mndDumpDbInfoData(SMnode *pMnode, SSDataBlock *pBlock, SDbObj *pDb,
char keep1Str[128] = {0}; char keep1Str[128] = {0};
char keep2Str[128] = {0}; char keep2Str[128] = {0};
int32_t lenKeep0 = formatDurationOrKeep(keep0Str, pDb->cfg.daysToKeep0); int32_t lenKeep0 = formatDurationOrKeep(keep0Str, sizeof(keep0Str), pDb->cfg.daysToKeep0);
int32_t lenKeep1 = formatDurationOrKeep(keep1Str, pDb->cfg.daysToKeep1); int32_t lenKeep1 = formatDurationOrKeep(keep1Str, sizeof(keep1Str), pDb->cfg.daysToKeep1);
int32_t lenKeep2 = formatDurationOrKeep(keep2Str, pDb->cfg.daysToKeep2); int32_t lenKeep2 = formatDurationOrKeep(keep2Str, sizeof(keep2Str), pDb->cfg.daysToKeep2);
if (pDb->cfg.daysToKeep0 > pDb->cfg.daysToKeep1 || pDb->cfg.daysToKeep0 > pDb->cfg.daysToKeep2) { if (pDb->cfg.daysToKeep0 > pDb->cfg.daysToKeep1 || pDb->cfg.daysToKeep0 > pDb->cfg.daysToKeep2) {
len = sprintf(&keepVstr[VARSTR_HEADER_SIZE], "%s,%s,%s", keep1Str, keep2Str, keep0Str); len = sprintf(&keepVstr[VARSTR_HEADER_SIZE], "%s,%s,%s", keep1Str, keep2Str, keep0Str);

View File

@ -345,20 +345,19 @@ static const char* encryptAlgorithmStr(int8_t encryptAlgorithm) {
return TSDB_CACHE_MODEL_NONE_STR; return TSDB_CACHE_MODEL_NONE_STR;
} }
int32_t formatDurationOrKeep(char* buffer, int32_t timeInMinutes) { int32_t formatDurationOrKeep(char* buffer, int64_t bufSize, int32_t timeInMinutes) {
if (buffer == NULL) { if (buffer == NULL || bufSize <= 0) {
return 0; return 0;
} }
int lMaxLen = 32;
int32_t len = 0; int32_t len = 0;
if (timeInMinutes % 1440 == 0) { if (timeInMinutes % 1440 == 0) {
int32_t days = timeInMinutes / 1440; int32_t days = timeInMinutes / 1440;
len = snprintf(buffer, lMaxLen,"%dd", days); len = snprintf(buffer, bufSize, "%dd", days);
} else if (timeInMinutes % 60 == 0) { } else if (timeInMinutes % 60 == 0) {
int32_t hours = timeInMinutes / 60; int32_t hours = timeInMinutes / 60;
len = snprintf(buffer, lMaxLen,"%dh", hours); len = snprintf(buffer, bufSize, "%dh", hours);
} else { } else {
len = snprintf(buffer, lMaxLen,"%dm", timeInMinutes); len = snprintf(buffer, bufSize, "%dm", timeInMinutes);
} }
return len; return len;
} }
@ -405,10 +404,10 @@ static int32_t setCreateDBResultIntoDataBlock(SSDataBlock* pBlock, char* dbName,
char keep1Str[128] = {0}; char keep1Str[128] = {0};
char keep2Str[128] = {0}; char keep2Str[128] = {0};
int32_t lenDuration = formatDurationOrKeep(durationStr, pCfg->daysPerFile); int32_t lenDuration = formatDurationOrKeep(durationStr, sizeof(durationStr), pCfg->daysPerFile);
int32_t lenKeep0 = formatDurationOrKeep(keep0Str, pCfg->daysToKeep0); int32_t lenKeep0 = formatDurationOrKeep(keep0Str, sizeof(keep0Str), pCfg->daysToKeep0);
int32_t lenKeep1 = formatDurationOrKeep(keep1Str, pCfg->daysToKeep1); int32_t lenKeep1 = formatDurationOrKeep(keep1Str, sizeof(keep1Str), pCfg->daysToKeep1);
int32_t lenKeep2 = formatDurationOrKeep(keep2Str, pCfg->daysToKeep2); int32_t lenKeep2 = formatDurationOrKeep(keep2Str, sizeof(keep2Str), pCfg->daysToKeep2);
if (IS_SYS_DBNAME(dbName)) { if (IS_SYS_DBNAME(dbName)) {
len += snprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_DB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE, "CREATE DATABASE `%s`", dbName); len += snprintf(buf2 + VARSTR_HEADER_SIZE, SHOW_CREATE_DB_RESULT_FIELD2_LEN - VARSTR_HEADER_SIZE, "CREATE DATABASE `%s`", dbName);
@ -542,16 +541,16 @@ void appendTagFields(char* buf, int32_t* len, STableCfg* pCfg) {
for (int32_t i = 0; i < pCfg->numOfTags; ++i) { for (int32_t i = 0; i < pCfg->numOfTags; ++i) {
SSchema* pSchema = pCfg->pSchemas + pCfg->numOfColumns + i; SSchema* pSchema = pCfg->pSchemas + pCfg->numOfColumns + i;
char type[32]; char type[32];
snprintf(type, 32, "%s", tDataTypes[pSchema->type].name); snprintf(type, sizeof(type), "%s", tDataTypes[pSchema->type].name);
if (TSDB_DATA_TYPE_VARCHAR == pSchema->type || TSDB_DATA_TYPE_VARBINARY == pSchema->type || if (TSDB_DATA_TYPE_VARCHAR == pSchema->type || TSDB_DATA_TYPE_VARBINARY == pSchema->type ||
TSDB_DATA_TYPE_GEOMETRY == pSchema->type) { TSDB_DATA_TYPE_GEOMETRY == pSchema->type) {
snprintf(type + strlen(type), 32 - strlen(type), "(%d)", (int32_t)(pSchema->bytes - VARSTR_HEADER_SIZE)); snprintf(type + strlen(type), sizeof(type) - strlen(type), "(%d)", (int32_t)(pSchema->bytes - VARSTR_HEADER_SIZE));
} else if (TSDB_DATA_TYPE_NCHAR == pSchema->type) { } else if (TSDB_DATA_TYPE_NCHAR == pSchema->type) {
snprintf(type + strlen(type), 32 - strlen(type), "(%d)", snprintf(type + strlen(type), sizeof(type) - strlen(type), "(%d)",
(int32_t)((pSchema->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE)); (int32_t)((pSchema->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE));
} }
*len += snprintf(buf + VARSTR_HEADER_SIZE + *len, 32 - (VARSTR_HEADER_SIZE + *len), "%s`%s` %s", *len += snprintf(buf + VARSTR_HEADER_SIZE + *len, sizeof(type) - (VARSTR_HEADER_SIZE + *len), "%s`%s` %s",
((i > 0) ? ", " : ""), pSchema->name, type); ((i > 0) ? ", " : ""), pSchema->name, type);
} }
} }
@ -614,11 +613,17 @@ int32_t appendTagValues(char* buf, int32_t* len, STableCfg* pCfg) {
char type = pTagVal->type; char type = pTagVal->type;
int32_t tlen = 0; int32_t tlen = 0;
int64_t leftSize = SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len);
if (leftSize <= 0) {
qError("no enough space to store tag value, leftSize:%" PRId64, leftSize);
code = TSDB_CODE_APP_ERROR;
TAOS_CHECK_ERRNO(code);
}
if (IS_VAR_DATA_TYPE(type)) { if (IS_VAR_DATA_TYPE(type)) {
code = dataConverToStr(buf + VARSTR_HEADER_SIZE + *len, type, pTagVal->pData, pTagVal->nData, &tlen); code = dataConverToStr(buf + VARSTR_HEADER_SIZE + *len, leftSize, type, pTagVal->pData, pTagVal->nData, &tlen);
TAOS_CHECK_ERRNO(code); TAOS_CHECK_ERRNO(code);
} else { } else {
code = dataConverToStr(buf + VARSTR_HEADER_SIZE + *len, type, &pTagVal->i64, tDataTypes[type].bytes, &tlen); code = dataConverToStr(buf + VARSTR_HEADER_SIZE + *len, leftSize, type, &pTagVal->i64, tDataTypes[type].bytes, &tlen);
TAOS_CHECK_ERRNO(code); TAOS_CHECK_ERRNO(code);
} }
*len += tlen; *len += tlen;

View File

@ -161,7 +161,7 @@ static int32_t udfSpawnUdfd(SUdfdData *pData) {
int len = strlen("TAOS_FQDN=") + strlen(taosFqdn) + 1; int len = strlen("TAOS_FQDN=") + strlen(taosFqdn) + 1;
taosFqdnEnvItem = taosMemoryMalloc(len); taosFqdnEnvItem = taosMemoryMalloc(len);
if (taosFqdnEnvItem != NULL) { if (taosFqdnEnvItem != NULL) {
TAOS_STRNCPY(taosFqdnEnvItem, "TAOS_FQDN=", len); tstrncpy(taosFqdnEnvItem, "TAOS_FQDN=", len);
TAOS_STRNCAT(taosFqdnEnvItem, taosFqdn, strlen(taosFqdn)); TAOS_STRNCAT(taosFqdnEnvItem, taosFqdn, strlen(taosFqdn));
fnInfo("[UDFD]Succsess to set TAOS_FQDN:%s", taosFqdn); fnInfo("[UDFD]Succsess to set TAOS_FQDN:%s", taosFqdn);
} else { } else {

View File

@ -313,42 +313,41 @@ void destroyQueryExecRes(SExecResult* pRes) {
} }
} }
// clang-format on // clang-format on
#define MAX_NUMERICAL_LENGTH (32) int32_t dataConverToStr(char* str, int64_t capacity, int type, void* buf, int32_t bufSize, int32_t* len) {
int32_t dataConverToStr(char* str, int type, void* buf, int32_t bufSize, int32_t* len) {
int32_t n = 0; int32_t n = 0;
switch (type) { switch (type) {
case TSDB_DATA_TYPE_NULL: case TSDB_DATA_TYPE_NULL:
n = snprintf(str, MAX_NUMERICAL_LENGTH, "null"); n = snprintf(str, capacity, "null");
break; break;
case TSDB_DATA_TYPE_BOOL: case TSDB_DATA_TYPE_BOOL:
n = snprintf(str, MAX_NUMERICAL_LENGTH, (*(int8_t*)buf) ? "true" : "false"); n = snprintf(str, capacity, (*(int8_t*)buf) ? "true" : "false");
break; break;
case TSDB_DATA_TYPE_TINYINT: case TSDB_DATA_TYPE_TINYINT:
n = snprintf(str, MAX_NUMERICAL_LENGTH, "%d", *(int8_t*)buf); n = snprintf(str, capacity, "%d", *(int8_t*)buf);
break; break;
case TSDB_DATA_TYPE_SMALLINT: case TSDB_DATA_TYPE_SMALLINT:
n = snprintf(str, MAX_NUMERICAL_LENGTH, "%d", *(int16_t*)buf); n = snprintf(str, capacity, "%d", *(int16_t*)buf);
break; break;
case TSDB_DATA_TYPE_INT: case TSDB_DATA_TYPE_INT:
n = snprintf(str, MAX_NUMERICAL_LENGTH, "%d", *(int32_t*)buf); n = snprintf(str, capacity, "%d", *(int32_t*)buf);
break; break;
case TSDB_DATA_TYPE_BIGINT: case TSDB_DATA_TYPE_BIGINT:
case TSDB_DATA_TYPE_TIMESTAMP: case TSDB_DATA_TYPE_TIMESTAMP:
n = snprintf(str, MAX_NUMERICAL_LENGTH, "%" PRId64, *(int64_t*)buf); n = snprintf(str, capacity, "%" PRId64, *(int64_t*)buf);
break; break;
case TSDB_DATA_TYPE_FLOAT: case TSDB_DATA_TYPE_FLOAT:
n = snprintf(str, MAX_NUMERICAL_LENGTH, "%e", GET_FLOAT_VAL(buf)); n = snprintf(str, capacity, "%e", GET_FLOAT_VAL(buf));
break; break;
case TSDB_DATA_TYPE_DOUBLE: case TSDB_DATA_TYPE_DOUBLE:
n = snprintf(str, MAX_NUMERICAL_LENGTH, "%e", GET_DOUBLE_VAL(buf)); n = snprintf(str, capacity, "%e", GET_DOUBLE_VAL(buf));
break; break;
case TSDB_DATA_TYPE_VARBINARY: { case TSDB_DATA_TYPE_VARBINARY: {
@ -395,19 +394,19 @@ int32_t dataConverToStr(char* str, int type, void* buf, int32_t bufSize, int32_t
n = length + 2; n = length + 2;
break; break;
case TSDB_DATA_TYPE_UTINYINT: case TSDB_DATA_TYPE_UTINYINT:
n = snprintf(str, MAX_NUMERICAL_LENGTH, "%d", *(uint8_t*)buf); n = snprintf(str, capacity, "%d", *(uint8_t*)buf);
break; break;
case TSDB_DATA_TYPE_USMALLINT: case TSDB_DATA_TYPE_USMALLINT:
n = snprintf(str, MAX_NUMERICAL_LENGTH, "%d", *(uint16_t*)buf); n = snprintf(str, capacity, "%d", *(uint16_t*)buf);
break; break;
case TSDB_DATA_TYPE_UINT: case TSDB_DATA_TYPE_UINT:
n = snprintf(str, MAX_NUMERICAL_LENGTH, "%u", *(uint32_t*)buf); n = snprintf(str, capacity, "%u", *(uint32_t*)buf);
break; break;
case TSDB_DATA_TYPE_UBIGINT: case TSDB_DATA_TYPE_UBIGINT:
n = snprintf(str, MAX_NUMERICAL_LENGTH, "%" PRIu64, *(uint64_t*)buf); n = snprintf(str, capacity, "%" PRIu64, *(uint64_t*)buf);
break; break;
default: default: