Merge pull request #27948 from taosdata/enh/TD-32144-3.0

enh: check memory allocation
This commit is contained in:
Hongze Cheng 2024-09-20 09:04:34 +08:00 committed by GitHub
commit 80152c1e48
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 37 additions and 12 deletions

View File

@ -49,7 +49,7 @@ int32_t toUInteger(const char *z, int32_t n, int32_t base, uint64_t *value);
*/ */
int32_t toIntegerPure(const char *z, int32_t n, int32_t base, int64_t *value); int32_t toIntegerPure(const char *z, int32_t n, int32_t base, int64_t *value);
void taosVariantCreateFromBinary(SVariant *pVar, const char *pz, size_t len, uint32_t type); int32_t taosVariantCreateFromBinary(SVariant *pVar, const char *pz, size_t len, uint32_t type);
void taosVariantDestroy(SVariant *pV); void taosVariantDestroy(SVariant *pV);

View File

@ -143,7 +143,7 @@ bool tNameIsValid(const SName* name) {
SName* tNameDup(const SName* name) { SName* tNameDup(const SName* name) {
SName* p = taosMemoryMalloc(sizeof(SName)); SName* p = taosMemoryMalloc(sizeof(SName));
memcpy(p, name, sizeof(SName)); if (p) TAOS_MEMCPY(p, name, sizeof(SName));
return p; return p;
} }

View File

@ -368,7 +368,7 @@ int32_t toUInteger(const char *z, int32_t n, int32_t base, uint64_t *value) {
* @param len * @param len
* @param type * @param type
*/ */
void taosVariantCreateFromBinary(SVariant *pVar, const char *pz, size_t len, uint32_t type) { int32_t taosVariantCreateFromBinary(SVariant *pVar, const char *pz, size_t len, uint32_t type) {
switch (type) { switch (type) {
case TSDB_DATA_TYPE_BOOL: case TSDB_DATA_TYPE_BOOL:
case TSDB_DATA_TYPE_TINYINT: { case TSDB_DATA_TYPE_TINYINT: {
@ -426,6 +426,7 @@ void taosVariantCreateFromBinary(SVariant *pVar, const char *pz, size_t len, uin
size_t lenInwchar = len / TSDB_NCHAR_SIZE; size_t lenInwchar = len / TSDB_NCHAR_SIZE;
pVar->ucs4 = taosMemoryCalloc(1, (lenInwchar + 1) * TSDB_NCHAR_SIZE); pVar->ucs4 = taosMemoryCalloc(1, (lenInwchar + 1) * TSDB_NCHAR_SIZE);
if(!pVar->ucs4) return terrno;
(void)memcpy(pVar->ucs4, pz, lenInwchar * TSDB_NCHAR_SIZE); (void)memcpy(pVar->ucs4, pz, lenInwchar * TSDB_NCHAR_SIZE);
pVar->nLen = (int32_t)len; pVar->nLen = (int32_t)len;
@ -446,6 +447,7 @@ void taosVariantCreateFromBinary(SVariant *pVar, const char *pz, size_t len, uin
} }
pVar->nType = type; pVar->nType = type;
return 0;
} }
void taosVariantDestroy(SVariant *pVar) { void taosVariantDestroy(SVariant *pVar) {

View File

@ -846,6 +846,7 @@ static int32_t mndRetrieveConns(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBl
if (pShow->pIter == NULL) { if (pShow->pIter == NULL) {
SProfileMgmt *pMgmt = &pMnode->profileMgmt; SProfileMgmt *pMgmt = &pMnode->profileMgmt;
pShow->pIter = taosCacheCreateIter(pMgmt->connCache); pShow->pIter = taosCacheCreateIter(pMgmt->connCache);
if (!pShow->pIter) return terrno;
} }
while (numOfRows < rows) { while (numOfRows < rows) {
@ -1005,6 +1006,7 @@ static int32_t mndRetrieveQueries(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *p
if (pShow->pIter == NULL) { if (pShow->pIter == NULL) {
SProfileMgmt *pMgmt = &pMnode->profileMgmt; SProfileMgmt *pMgmt = &pMnode->profileMgmt;
pShow->pIter = taosCacheCreateIter(pMgmt->connCache); pShow->pIter = taosCacheCreateIter(pMgmt->connCache);
if (!pShow->pIter) return terrno;
} }
// means fetched some data last time for this conn // means fetched some data last time for this conn
@ -1042,6 +1044,7 @@ static int32_t mndRetrieveApps(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlo
if (pShow->pIter == NULL) { if (pShow->pIter == NULL) {
SProfileMgmt *pMgmt = &pMnode->profileMgmt; SProfileMgmt *pMgmt = &pMnode->profileMgmt;
pShow->pIter = taosCacheCreateIter(pMgmt->appCache); pShow->pIter = taosCacheCreateIter(pMgmt->appCache);
if (!pShow->pIter) return terrno;
} }
while (numOfRows < rows) { while (numOfRows < rows) {

View File

@ -3902,8 +3902,7 @@ int32_t doAddIntoResult(SqlFunctionCtx* pCtx, void* pData, int32_t rowIndex, SSD
int32_t code = TSDB_CODE_SUCCESS; int32_t code = TSDB_CODE_SUCCESS;
SVariant val = {0}; SVariant val = {0};
// TODO(smj) : this func need err code TAOS_CHECK_RETURN(taosVariantCreateFromBinary(&val, pData, tDataTypes[type].bytes, type));
taosVariantCreateFromBinary(&val, pData, tDataTypes[type].bytes, type);
STopBotResItem* pItems = pRes->pItems; STopBotResItem* pItems = pRes->pItems;

View File

@ -380,6 +380,14 @@ SCacheObj *taosCacheInit(int32_t keyType, int64_t refreshTimeInMs, bool extendLi
return NULL; return NULL;
} }
pCacheObj->name = taosStrdup(cacheName);
if (pCacheObj->name == NULL) {
taosMemoryFreeClear(pCacheObj->pEntryList);
taosMemoryFree(pCacheObj);
uError("failed to allocate memory, reason:%s", terrstr());
return NULL;
}
// set free cache node callback function // set free cache node callback function
pCacheObj->hashFp = taosGetDefaultHashFunction(keyType); pCacheObj->hashFp = taosGetDefaultHashFunction(keyType);
pCacheObj->freeFp = fn; pCacheObj->freeFp = fn;
@ -389,13 +397,13 @@ SCacheObj *taosCacheInit(int32_t keyType, int64_t refreshTimeInMs, bool extendLi
if (__trashcan_lock_init(pCacheObj) != 0) { if (__trashcan_lock_init(pCacheObj) != 0) {
taosMemoryFreeClear(pCacheObj->pEntryList); taosMemoryFreeClear(pCacheObj->pEntryList);
taosMemoryFreeClear(pCacheObj->name);
taosMemoryFree(pCacheObj); taosMemoryFree(pCacheObj);
uError("failed to init lock, reason:%s", strerror(errno)); uError("failed to init lock, reason:%s", strerror(errno));
return NULL; return NULL;
} }
pCacheObj->name = taosStrdup(cacheName);
(void)doRegisterCacheObj(pCacheObj); (void)doRegisterCacheObj(pCacheObj);
return pCacheObj; return pCacheObj;
} }
@ -746,8 +754,13 @@ void taosAddToTrashcan(SCacheObj *pCacheObj, SCacheNode *pNode) {
return; return;
} }
__trashcan_wr_lock(pCacheObj);
STrashElem *pElem = taosMemoryCalloc(1, sizeof(STrashElem)); STrashElem *pElem = taosMemoryCalloc(1, sizeof(STrashElem));
if (!pElem) {
uError("cache:%s key:%p, %p move to trashcan failed since %s, numOfElem in trashcan:%d", pCacheObj->name,
pNode->key, pNode->data, terrstr(), pCacheObj->numOfElemsInTrash);
return;
}
__trashcan_wr_lock(pCacheObj);
pElem->pData = pNode; pElem->pData = pNode;
pElem->prev = NULL; pElem->prev = NULL;
pElem->next = NULL; pElem->next = NULL;
@ -928,9 +941,11 @@ size_t taosCacheGetNumOfObj(const SCacheObj *pCacheObj) { return pCacheObj->numO
SCacheIter *taosCacheCreateIter(const SCacheObj *pCacheObj) { SCacheIter *taosCacheCreateIter(const SCacheObj *pCacheObj) {
SCacheIter *pIter = taosMemoryCalloc(1, sizeof(SCacheIter)); SCacheIter *pIter = taosMemoryCalloc(1, sizeof(SCacheIter));
if (pIter) {
pIter->pCacheObj = (SCacheObj *)pCacheObj; pIter->pCacheObj = (SCacheObj *)pCacheObj;
pIter->entryIndex = -1; pIter->entryIndex = -1;
pIter->index = -1; pIter->index = -1;
}
return pIter; return pIter;
} }

View File

@ -427,6 +427,11 @@ int32_t cfgCheckRangeForDynUpdate(SConfig *pCfg, const char *name, const char *p
case CFG_DTYPE_STRING: { case CFG_DTYPE_STRING: {
if (strcasecmp(name, "slowLogScope") == 0) { if (strcasecmp(name, "slowLogScope") == 0) {
char *tmp = taosStrdup(pVal); char *tmp = taosStrdup(pVal);
if (!tmp) {
cfgUnLock(pCfg);
uError("failed to config:%s since %s", name, terrstr());
TAOS_RETURN(terrno);
}
int32_t scope = 0; int32_t scope = 0;
int32_t code = taosSetSlowLogScope(tmp, &scope); int32_t code = taosSetSlowLogScope(tmp, &scope);
if (TSDB_CODE_SUCCESS != code) { if (TSDB_CODE_SUCCESS != code) {

View File

@ -652,6 +652,7 @@ void taosPrintLongString(const char *flags, int32_t level, int32_t dflag, const
if (!(dflag & DEBUG_FILE) && !(dflag & DEBUG_SCREEN)) return; if (!(dflag & DEBUG_FILE) && !(dflag & DEBUG_SCREEN)) return;
char *buffer = taosMemoryMalloc(LOG_MAX_LINE_DUMP_BUFFER_SIZE); char *buffer = taosMemoryMalloc(LOG_MAX_LINE_DUMP_BUFFER_SIZE);
if (!buffer) return;
int32_t len = taosBuildLogHead(buffer, flags); int32_t len = taosBuildLogHead(buffer, flags);
va_list argpointer; va_list argpointer;