From 1b53ba2df42e999324a074cd4bcba18078aca990 Mon Sep 17 00:00:00 2001 From: kailixu Date: Wed, 18 Sep 2024 23:17:08 +0800 Subject: [PATCH 1/2] enh: check memory allocation --- include/common/tvariant.h | 2 +- source/common/src/tname.c | 2 +- source/common/src/tvariant.c | 4 +++- source/dnode/mnode/impl/src/mndProfile.c | 3 +++ source/libs/function/src/builtinsimpl.c | 3 +-- source/util/src/tcache.c | 28 +++++++++++++++++++----- source/util/src/tconfig.c | 7 +++++- source/util/src/tlog.c | 3 ++- 8 files changed, 39 insertions(+), 13 deletions(-) diff --git a/include/common/tvariant.h b/include/common/tvariant.h index 9a0585dada..0d84b2414e 100644 --- a/include/common/tvariant.h +++ b/include/common/tvariant.h @@ -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); -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); diff --git a/source/common/src/tname.c b/source/common/src/tname.c index b725514d4f..a9dda87591 100644 --- a/source/common/src/tname.c +++ b/source/common/src/tname.c @@ -143,7 +143,7 @@ bool tNameIsValid(const SName* name) { SName* tNameDup(const SName* name) { SName* p = taosMemoryMalloc(sizeof(SName)); - memcpy(p, name, sizeof(SName)); + if (p) TAOS_MEMCPY(p, name, sizeof(SName)); return p; } diff --git a/source/common/src/tvariant.c b/source/common/src/tvariant.c index 3cb8d5f41e..60cf7963a8 100644 --- a/source/common/src/tvariant.c +++ b/source/common/src/tvariant.c @@ -368,7 +368,7 @@ int32_t toUInteger(const char *z, int32_t n, int32_t base, uint64_t *value) { * @param len * @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) { case TSDB_DATA_TYPE_BOOL: 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; pVar->ucs4 = taosMemoryCalloc(1, (lenInwchar + 1) * TSDB_NCHAR_SIZE); + if(!pVar->ucs4) return terrno; (void)memcpy(pVar->ucs4, pz, lenInwchar * TSDB_NCHAR_SIZE); pVar->nLen = (int32_t)len; @@ -446,6 +447,7 @@ void taosVariantCreateFromBinary(SVariant *pVar, const char *pz, size_t len, uin } pVar->nType = type; + return 0; } void taosVariantDestroy(SVariant *pVar) { diff --git a/source/dnode/mnode/impl/src/mndProfile.c b/source/dnode/mnode/impl/src/mndProfile.c index 5d91b0b0d8..645a187abb 100644 --- a/source/dnode/mnode/impl/src/mndProfile.c +++ b/source/dnode/mnode/impl/src/mndProfile.c @@ -846,6 +846,7 @@ static int32_t mndRetrieveConns(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBl if (pShow->pIter == NULL) { SProfileMgmt *pMgmt = &pMnode->profileMgmt; pShow->pIter = taosCacheCreateIter(pMgmt->connCache); + if (!pShow->pIter) return terrno; } while (numOfRows < rows) { @@ -1005,6 +1006,7 @@ static int32_t mndRetrieveQueries(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *p if (pShow->pIter == NULL) { SProfileMgmt *pMgmt = &pMnode->profileMgmt; pShow->pIter = taosCacheCreateIter(pMgmt->connCache); + if (!pShow->pIter) return terrno; } // 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) { SProfileMgmt *pMgmt = &pMnode->profileMgmt; pShow->pIter = taosCacheCreateIter(pMgmt->appCache); + if (!pShow->pIter) return terrno; } while (numOfRows < rows) { diff --git a/source/libs/function/src/builtinsimpl.c b/source/libs/function/src/builtinsimpl.c index 5fc257db5b..1cb7e64d8d 100644 --- a/source/libs/function/src/builtinsimpl.c +++ b/source/libs/function/src/builtinsimpl.c @@ -3902,8 +3902,7 @@ int32_t doAddIntoResult(SqlFunctionCtx* pCtx, void* pData, int32_t rowIndex, SSD int32_t code = TSDB_CODE_SUCCESS; SVariant val = {0}; - // TODO(smj) : this func need err code - taosVariantCreateFromBinary(&val, pData, tDataTypes[type].bytes, type); + TAOS_CHECK_RETURN(taosVariantCreateFromBinary(&val, pData, tDataTypes[type].bytes, type)); STopBotResItem* pItems = pRes->pItems; diff --git a/source/util/src/tcache.c b/source/util/src/tcache.c index 3b5c46c2b3..40d282efb9 100644 --- a/source/util/src/tcache.c +++ b/source/util/src/tcache.c @@ -366,7 +366,7 @@ SCacheObj *taosCacheInit(int32_t keyType, int64_t refreshTimeInMs, bool extendLi SCacheObj *pCacheObj = (SCacheObj *)taosMemoryCalloc(1, sizeof(SCacheObj)); if (pCacheObj == NULL) { - uError("failed to allocate memory, reason:%s", strerror(errno)); + uError("failed to allocate memory, reason:%s", terrstr()); terrno = TSDB_CODE_OUT_OF_MEMORY; return NULL; } @@ -380,6 +380,14 @@ SCacheObj *taosCacheInit(int32_t keyType, int64_t refreshTimeInMs, bool extendLi 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 pCacheObj->hashFp = taosGetDefaultHashFunction(keyType); pCacheObj->freeFp = fn; @@ -389,13 +397,14 @@ SCacheObj *taosCacheInit(int32_t keyType, int64_t refreshTimeInMs, bool extendLi if (__trashcan_lock_init(pCacheObj) != 0) { taosMemoryFreeClear(pCacheObj->pEntryList); + taosMemoryFreeClear(pCacheObj->name); taosMemoryFree(pCacheObj); uError("failed to init lock, reason:%s", strerror(errno)); return NULL; } - pCacheObj->name = taosStrdup(cacheName); + (void)doRegisterCacheObj(pCacheObj); return pCacheObj; } @@ -746,8 +755,13 @@ void taosAddToTrashcan(SCacheObj *pCacheObj, SCacheNode *pNode) { return; } - __trashcan_wr_lock(pCacheObj); 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->prev = NULL; pElem->next = NULL; @@ -928,9 +942,11 @@ size_t taosCacheGetNumOfObj(const SCacheObj *pCacheObj) { return pCacheObj->numO SCacheIter *taosCacheCreateIter(const SCacheObj *pCacheObj) { SCacheIter *pIter = taosMemoryCalloc(1, sizeof(SCacheIter)); - pIter->pCacheObj = (SCacheObj *)pCacheObj; - pIter->entryIndex = -1; - pIter->index = -1; + if (pIter) { + pIter->pCacheObj = (SCacheObj *)pCacheObj; + pIter->entryIndex = -1; + pIter->index = -1; + } return pIter; } diff --git a/source/util/src/tconfig.c b/source/util/src/tconfig.c index 0840fd571c..6868e6d0eb 100644 --- a/source/util/src/tconfig.c +++ b/source/util/src/tconfig.c @@ -426,7 +426,12 @@ int32_t cfgCheckRangeForDynUpdate(SConfig *pCfg, const char *name, const char *p switch (pItem->dtype) { case CFG_DTYPE_STRING: { 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 code = taosSetSlowLogScope(tmp, &scope); if (TSDB_CODE_SUCCESS != code) { diff --git a/source/util/src/tlog.c b/source/util/src/tlog.c index 7d905e843d..aea6647e4c 100644 --- a/source/util/src/tlog.c +++ b/source/util/src/tlog.c @@ -651,7 +651,8 @@ void taosPrintLongString(const char *flags, int32_t level, int32_t dflag, const if (!osLogSpaceSufficient()) 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); va_list argpointer; From 1add9ded8d2b0c579a8e32329ea4554cdce97e69 Mon Sep 17 00:00:00 2001 From: kailixu Date: Wed, 18 Sep 2024 23:19:00 +0800 Subject: [PATCH 2/2] enh: check memory allocation --- source/util/src/tcache.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/source/util/src/tcache.c b/source/util/src/tcache.c index 40d282efb9..81e50f6a6b 100644 --- a/source/util/src/tcache.c +++ b/source/util/src/tcache.c @@ -366,7 +366,7 @@ SCacheObj *taosCacheInit(int32_t keyType, int64_t refreshTimeInMs, bool extendLi SCacheObj *pCacheObj = (SCacheObj *)taosMemoryCalloc(1, sizeof(SCacheObj)); if (pCacheObj == NULL) { - uError("failed to allocate memory, reason:%s", terrstr()); + uError("failed to allocate memory, reason:%s", strerror(errno)); terrno = TSDB_CODE_OUT_OF_MEMORY; return NULL; } @@ -404,7 +404,6 @@ SCacheObj *taosCacheInit(int32_t keyType, int64_t refreshTimeInMs, bool extendLi return NULL; } - (void)doRegisterCacheObj(pCacheObj); return pCacheObj; }