From 18bc5a048feafb319f605da39ef856828c18c2bd Mon Sep 17 00:00:00 2001 From: xsren <285808407@qq.com> Date: Thu, 26 Sep 2024 14:46:53 +0800 Subject: [PATCH] taosStrndup --- include/os/osDef.h | 1 - include/os/osString.h | 6 ++-- source/client/src/clientImpl.c | 5 ++- source/client/src/clientStmt.c | 4 +-- source/client/src/clientStmt2.c | 4 +-- source/dnode/mnode/impl/src/mndStreamTrans.c | 6 ++-- source/libs/function/src/builtins.c | 4 +-- source/libs/function/src/builtinsimpl.c | 8 ++--- source/libs/index/src/index.c | 5 ++- source/libs/index/src/indexComm.c | 4 +-- source/libs/nodes/src/nodesMsgFuncs.c | 4 +-- source/libs/parser/src/parAstCreater.c | 38 ++++++++++++-------- source/libs/parser/src/parInsertSql.c | 4 +-- source/libs/parser/src/parTranslater.c | 4 +-- source/libs/scalar/src/sclfunc.c | 8 ++--- source/os/src/osString.c | 16 ++++++++- tools/shell/src/shellAuto.c | 10 +++--- 17 files changed, 76 insertions(+), 55 deletions(-) diff --git a/include/os/osDef.h b/include/os/osDef.h index 439f4b5c6a..ff30265afa 100644 --- a/include/os/osDef.h +++ b/include/os/osDef.h @@ -76,7 +76,6 @@ typedef int (*__compar_fn_t)(const void *, const void *); char *strsep(char **stringp, const char *delim); char *getpass(const char *prefix); -char *strndup(const char *s, int n); // for send function in tsocket.c #define MSG_NOSIGNAL 0 diff --git a/include/os/osString.h b/include/os/osString.h index a64fb34f1e..5f211ad2ee 100644 --- a/include/os/osString.h +++ b/include/os/osString.h @@ -51,6 +51,7 @@ typedef enum { M2C = 0, C2M } ConvType; #define strtod STR_TO_LD_FUNC_TAOS_FORBID #define strtold STR_TO_D_FUNC_TAOS_FORBID #define strtof STR_TO_F_FUNC_TAOS_FORBID +#define strndup STR_TO_F_FUNC_TAOS_FORBID #endif #define tstrncpy(dst, src, size) \ @@ -101,8 +102,9 @@ int8_t taosStr2Int8(const char *str, char **pEnd, int32_t radix); uint8_t taosStr2UInt8(const char *str, char **pEnd, int32_t radix); double taosStr2Double(const char *str, char **pEnd); float taosStr2Float(const char *str, char **pEnd); -int32_t taosHex2Ascii(const char *z, uint32_t n, void** data, uint32_t* size); -int32_t taosAscii2Hex(const char *z, uint32_t n, void** data, uint32_t* size); +int32_t taosHex2Ascii(const char *z, uint32_t n, void **data, uint32_t *size); +int32_t taosAscii2Hex(const char *z, uint32_t n, void **data, uint32_t *size); +char *taosStrndup(const char *s, int n); //int32_t taosBin2Ascii(const char *z, uint32_t n, void** data, uint32_t* size); bool isHex(const char* z, uint32_t n); bool isValidateHex(const char* z, uint32_t n); diff --git a/source/client/src/clientImpl.c b/source/client/src/clientImpl.c index a8555926f1..15bd5795e2 100644 --- a/source/client/src/clientImpl.c +++ b/source/client/src/clientImpl.c @@ -2441,10 +2441,9 @@ char* getDbOfConnection(STscObj* pObj) { (void)taosThreadMutexLock(&pObj->mutex); size_t len = strlen(pObj->db); if (len > 0) { - p = strndup(pObj->db, tListLen(pObj->db)); + p = taosStrndup(pObj->db, tListLen(pObj->db)); if (p == NULL) { - tscError("failed to strndup db name"); - terrno = TSDB_CODE_OUT_OF_MEMORY; + tscError("failed to taosStrndup db name"); } } diff --git a/source/client/src/clientStmt.c b/source/client/src/clientStmt.c index 63bc27c624..430c7759ad 100644 --- a/source/client/src/clientStmt.c +++ b/source/client/src/clientStmt.c @@ -923,9 +923,9 @@ int stmtPrepare(TAOS_STMT* stmt, const char* sql, unsigned long length) { length = strlen(sql); } - pStmt->sql.sqlStr = strndup(sql, length); + pStmt->sql.sqlStr = taosStrndup(sql, length); if (!pStmt->sql.sqlStr) { - return TSDB_CODE_OUT_OF_MEMORY; + return terrno; } pStmt->sql.sqlLen = length; pStmt->sql.stbInterlaceMode = pStmt->stbInterlaceMode; diff --git a/source/client/src/clientStmt2.c b/source/client/src/clientStmt2.c index 56337e5469..c26293e587 100644 --- a/source/client/src/clientStmt2.c +++ b/source/client/src/clientStmt2.c @@ -868,9 +868,9 @@ int stmtPrepare2(TAOS_STMT2* stmt, const char* sql, unsigned long length) { length = strlen(sql); } - pStmt->sql.sqlStr = strndup(sql, length); + pStmt->sql.sqlStr = taosStrndup(sql, length); if (!pStmt->sql.sqlStr) { - return TSDB_CODE_OUT_OF_MEMORY; + return terrno; } pStmt->sql.sqlLen = length; pStmt->sql.stbInterlaceMode = pStmt->stbInterlaceMode; diff --git a/source/dnode/mnode/impl/src/mndStreamTrans.c b/source/dnode/mnode/impl/src/mndStreamTrans.c index e5b4447a39..c16016cf13 100644 --- a/source/dnode/mnode/impl/src/mndStreamTrans.c +++ b/source/dnode/mnode/impl/src/mndStreamTrans.c @@ -316,6 +316,7 @@ int32_t doKillCheckpointTrans(SMnode *pMnode, const char *pDBName, size_t len) { // kill all trans in the dst DB void killAllCheckpointTrans(SMnode *pMnode, SVgroupChangeInfo *pChangeInfo) { mDebug("start to clear checkpoints in all Dbs"); + char p[128] = {0}; void *pIter = NULL; while ((pIter = taosHashIterate(pChangeInfo->pDBMap, pIter)) != NULL) { @@ -323,15 +324,14 @@ void killAllCheckpointTrans(SMnode *pMnode, SVgroupChangeInfo *pChangeInfo) { size_t len = 0; void *pKey = taosHashGetKey(pDb, &len); - char *p = strndup(pKey, len); + stpncpy(p, pKey, 127); int32_t code = doKillCheckpointTrans(pMnode, pKey, len); if (code) { - mError("failed to kill trans, transId:%p", pKey) + mError("failed to kill trans, transId:%p", pKey); } else { mDebug("clear checkpoint trans in Db:%s", p); } - taosMemoryFree(p); } mDebug("complete clear checkpoints in all Dbs"); diff --git a/source/libs/function/src/builtins.c b/source/libs/function/src/builtins.c index a43ae835ff..604375aed2 100644 --- a/source/libs/function/src/builtins.c +++ b/source/libs/function/src/builtins.c @@ -222,10 +222,10 @@ static int32_t addTimezoneParam(SNodeList* pList) { return code; } - pVal->literal = strndup(buf, len); + pVal->literal = taosStrndup(buf, len); if (pVal->literal == NULL) { nodesDestroyNode((SNode*)pVal); - return TSDB_CODE_OUT_OF_MEMORY; + return terrno; } pVal->translate = true; pVal->node.resType.type = TSDB_DATA_TYPE_BINARY; diff --git a/source/libs/function/src/builtinsimpl.c b/source/libs/function/src/builtinsimpl.c index 194b68830b..157d44b3de 100644 --- a/source/libs/function/src/builtinsimpl.c +++ b/source/libs/function/src/builtinsimpl.c @@ -4838,9 +4838,9 @@ int32_t histogramFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResul pInfo->totalCount = 0; pInfo->normalized = 0; - char* binTypeStr = strndup(varDataVal(pCtx->param[1].param.pz), varDataLen(pCtx->param[1].param.pz)); + char* binTypeStr = taosStrndup(varDataVal(pCtx->param[1].param.pz), varDataLen(pCtx->param[1].param.pz)); if (binTypeStr == NULL) { - return TSDB_CODE_OUT_OF_MEMORY; + return terrno; } int8_t binType = getHistogramBinType(binTypeStr); taosMemoryFree(binTypeStr); @@ -4848,9 +4848,9 @@ int32_t histogramFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResul if (binType == UNKNOWN_BIN) { return TSDB_CODE_FUNC_FUNTION_PARA_VALUE; } - char* binDesc = strndup(varDataVal(pCtx->param[2].param.pz), varDataLen(pCtx->param[2].param.pz)); + char* binDesc = taosStrndup(varDataVal(pCtx->param[2].param.pz), varDataLen(pCtx->param[2].param.pz)); if (binDesc == NULL) { - return TSDB_CODE_OUT_OF_MEMORY; + return terrno; } int64_t normalized = pCtx->param[3].param.i; if (normalized != 0 && normalized != 1) { diff --git a/source/libs/index/src/index.c b/source/libs/index/src/index.c index 7d26b30276..f22205786f 100644 --- a/source/libs/index/src/index.c +++ b/source/libs/index/src/index.c @@ -358,11 +358,11 @@ SIndexTerm* indexTermCreate(int64_t suid, SIndexOperOnColumn oper, uint8_t colTy if (colVal != NULL && nColVal != 0) { len = idxConvertDataToStr((void*)colVal, IDX_TYPE_GET_TYPE(colType), (void**)&buf); } else if (colVal == NULL) { - buf = strndup(INDEX_DATA_NULL_STR, (int32_t)strlen(INDEX_DATA_NULL_STR)); + buf = taosStrndup(INDEX_DATA_NULL_STR, (int32_t)strlen(INDEX_DATA_NULL_STR)); len = (int32_t)strlen(INDEX_DATA_NULL_STR); } else { static const char* emptyStr = " "; - buf = strndup(emptyStr, (int32_t)strlen(emptyStr)); + buf = taosStrndup(emptyStr, (int32_t)strlen(emptyStr)); len = (int32_t)strlen(emptyStr); } @@ -370,7 +370,6 @@ SIndexTerm* indexTermCreate(int64_t suid, SIndexOperOnColumn oper, uint8_t colTy if (tm->colVal == NULL) { taosMemoryFree(tm->colName); taosMemoryFree(tm); - terrno = TSDB_CODE_OUT_OF_MEMORY; return NULL; } diff --git a/source/libs/index/src/indexComm.c b/source/libs/index/src/indexComm.c index ce68615c0f..ae6309f292 100644 --- a/source/libs/index/src/indexComm.c +++ b/source/libs/index/src/indexComm.c @@ -345,9 +345,9 @@ int idxUidCompare(const void* a, const void* b) { int32_t idxConvertDataToStr(void* src, int8_t type, void** dst) { if (src == NULL) { - *dst = strndup(INDEX_DATA_NULL_STR, (int)strlen(INDEX_DATA_NULL_STR)); + *dst = taosStrndup(INDEX_DATA_NULL_STR, (int)strlen(INDEX_DATA_NULL_STR)); if (*dst == NULL) { - return TSDB_CODE_OUT_OF_MEMORY; + return terrno; } return (int32_t)strlen(INDEX_DATA_NULL_STR); } diff --git a/source/libs/nodes/src/nodesMsgFuncs.c b/source/libs/nodes/src/nodesMsgFuncs.c index 3ea5ff6611..581c6222d2 100644 --- a/source/libs/nodes/src/nodesMsgFuncs.c +++ b/source/libs/nodes/src/nodesMsgFuncs.c @@ -513,8 +513,8 @@ static int32_t tlvDecodeValueCStr(STlvDecoder* pDecoder, char* pValue) { } static int32_t tlvDecodeCStrP(STlv* pTlv, char** pValue) { - *pValue = strndup(pTlv->value, pTlv->len); - return NULL == *pValue ? TSDB_CODE_OUT_OF_MEMORY : TSDB_CODE_SUCCESS; + *pValue = taosStrndup(pTlv->value, pTlv->len); + return NULL == *pValue ? terrno : TSDB_CODE_SUCCESS; } static int32_t tlvDecodeDynBinary(STlv* pTlv, void** pValue) { diff --git a/source/libs/parser/src/parAstCreater.c b/source/libs/parser/src/parAstCreater.c index e52c8865c7..5ff97da3ec 100644 --- a/source/libs/parser/src/parAstCreater.c +++ b/source/libs/parser/src/parAstCreater.c @@ -404,9 +404,9 @@ SNode* createValueNode(SAstCreateContext* pCxt, int32_t dataType, const SToken* SValueNode* val = NULL; pCxt->errCode = nodesMakeNode(QUERY_NODE_VALUE, (SNode**)&val); CHECK_MAKE_NODE(val); - val->literal = strndup(pLiteral->z, pLiteral->n); + val->literal = taosStrndup(pLiteral->z, pLiteral->n); if(!val->literal) { - pCxt->errCode = TSDB_CODE_OUT_OF_MEMORY; + pCxt->errCode = terrno; nodesDestroyNode((SNode*)val); return NULL; } @@ -434,14 +434,22 @@ SNode* createRawValueNode(SAstCreateContext* pCxt, int32_t dataType, const SToke goto _exit; } if (pLiteral) { - val->literal = strndup(pLiteral->z, pLiteral->n); + val->literal = taosStrndup(pLiteral->z, pLiteral->n); + if (!val->literal) { + pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, terrno, "Out of memory"); + goto _exit; + } } else if (pNode) { SRawExprNode* pRawExpr = (SRawExprNode*)pNode; if (!nodesIsExprNode(pRawExpr->pNode)) { pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, pRawExpr->p); goto _exit; } - val->literal = strndup(pRawExpr->p, pRawExpr->n); + val->literal = taosStrndup(pRawExpr->p, pRawExpr->n); + if (!val->literal) { + pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, terrno, "Out of memory"); + goto _exit; + } } else { pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INTERNAL_ERROR, "Invalid parameters"); goto _exit; @@ -479,8 +487,8 @@ SNode* createRawValueNodeExt(SAstCreateContext* pCxt, int32_t dataType, const ST goto _exit; } if (pLiteral) { - if (!(val->literal = strndup(pLiteral->z, pLiteral->n))) { - pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_OUT_OF_MEMORY, "Out of memory"); + if (!(val->literal = taosStrndup(pLiteral->z, pLiteral->n))) { + pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, terrno, "Out of memory"); goto _exit; } } else { @@ -579,7 +587,7 @@ SNodeList* createHintNodeList(SAstCreateContext* pCxt, const SToken* pLiteral) { return NULL; } SNodeList* pHintList = NULL; - char* hint = strndup(pLiteral->z + 3, pLiteral->n - 5); + char* hint = taosStrndup(pLiteral->z + 3, pLiteral->n - 5); if (!hint) return NULL; int32_t i = 0; bool quit = false; @@ -749,13 +757,13 @@ SNode* createDurationValueNode(SAstCreateContext* pCxt, const SToken* pLiteral) return NULL; } } - val->literal = strndup(pLiteral->z + 1, pLiteral->n - 2); + val->literal = taosStrndup(pLiteral->z + 1, pLiteral->n - 2); } else { - val->literal = strndup(pLiteral->z, pLiteral->n); + val->literal = taosStrndup(pLiteral->z, pLiteral->n); } if (!val->literal) { nodesDestroyNode((SNode*)val); - pCxt->errCode = TSDB_CODE_OUT_OF_MEMORY; + pCxt->errCode = terrno; return NULL; } val->flag |= VALUE_FLAG_IS_DURATION; @@ -801,13 +809,13 @@ SNode* createTimeOffsetValueNode(SAstCreateContext* pCxt, const SToken* pLiteral return NULL; } } - val->literal = strndup(pLiteral->z + 1, pLiteral->n - 2); + val->literal = taosStrndup(pLiteral->z + 1, pLiteral->n - 2); } else { - val->literal = strndup(pLiteral->z, pLiteral->n); + val->literal = taosStrndup(pLiteral->z, pLiteral->n); } if (!val->literal) { nodesDestroyNode((SNode*)val); - pCxt->errCode = TSDB_CODE_OUT_OF_MEMORY; + pCxt->errCode = terrno; return NULL; } val->flag |= VALUE_FLAG_IS_TIME_OFFSET; @@ -853,9 +861,9 @@ SNode* createPlaceholderValueNode(SAstCreateContext* pCxt, const SToken* pLitera SValueNode* val = NULL; pCxt->errCode = nodesMakeNode(QUERY_NODE_VALUE, (SNode**)&val); CHECK_MAKE_NODE(val); - val->literal = strndup(pLiteral->z, pLiteral->n); + val->literal = taosStrndup(pLiteral->z, pLiteral->n); if (!val->literal) { - pCxt->errCode = TSDB_CODE_OUT_OF_MEMORY; + pCxt->errCode = terrno; nodesDestroyNode((SNode*)val); return NULL; } diff --git a/source/libs/parser/src/parInsertSql.c b/source/libs/parser/src/parInsertSql.c index d6841fb492..1c26a7c70e 100644 --- a/source/libs/parser/src/parInsertSql.c +++ b/source/libs/parser/src/parInsertSql.c @@ -1079,9 +1079,9 @@ static int32_t parseTableOptions(SInsertParseContext* pCxt, SVnodeModifyOpStmt* return buildSyntaxErrMsg(&pCxt->msg, "comment too long", token.z); } int32_t len = trimString(token.z, token.n, pCxt->tmpTokenBuf, TSDB_TB_COMMENT_LEN); - pStmt->pCreateTblReq->comment = strndup(pCxt->tmpTokenBuf, len); + pStmt->pCreateTblReq->comment = taosStrndup(pCxt->tmpTokenBuf, len); if (NULL == pStmt->pCreateTblReq->comment) { - return TSDB_CODE_OUT_OF_MEMORY; + return terrno; } pStmt->pCreateTblReq->commentLen = len; } else { diff --git a/source/libs/parser/src/parTranslater.c b/source/libs/parser/src/parTranslater.c index 3781479f36..3ae4583013 100755 --- a/source/libs/parser/src/parTranslater.c +++ b/source/libs/parser/src/parTranslater.c @@ -8725,10 +8725,10 @@ static int32_t makeIntervalVal(SRetention* pRetension, int8_t precision, SNode** } char buf[20] = {0}; int32_t len = snprintf(buf, sizeof(buf), "%" PRId64 "%c", timeVal, pRetension->freqUnit); - pVal->literal = strndup(buf, len); + pVal->literal = taosStrndup(buf, len); if (NULL == pVal->literal) { nodesDestroyNode((SNode*)pVal); - return TSDB_CODE_OUT_OF_MEMORY; + return terrno; } pVal->flag |= VALUE_FLAG_IS_DURATION; pVal->node.resType.type = TSDB_DATA_TYPE_BIGINT; diff --git a/source/libs/scalar/src/sclfunc.c b/source/libs/scalar/src/sclfunc.c index 1129b9c28b..377009a07f 100644 --- a/source/libs/scalar/src/sclfunc.c +++ b/source/libs/scalar/src/sclfunc.c @@ -4352,16 +4352,16 @@ int32_t histogramScalarFunction(SScalarParam *pInput, int32_t inputNum, SScalarP int32_t numOfBins = 0; int32_t totalCount = 0; - char *binTypeStr = strndup(varDataVal(pInput[1].columnData->pData), varDataLen(pInput[1].columnData->pData)); + char *binTypeStr = taosStrndup(varDataVal(pInput[1].columnData->pData), varDataLen(pInput[1].columnData->pData)); if (NULL == binTypeStr) { - SCL_ERR_RET(TSDB_CODE_OUT_OF_MEMORY); + SCL_ERR_RET(terrno); } int8_t binType = getHistogramBinType(binTypeStr); taosMemoryFree(binTypeStr); - char *binDesc = strndup(varDataVal(pInput[2].columnData->pData), varDataLen(pInput[2].columnData->pData)); + char *binDesc = taosStrndup(varDataVal(pInput[2].columnData->pData), varDataLen(pInput[2].columnData->pData)); if (NULL == binDesc) { - SCL_ERR_RET(TSDB_CODE_OUT_OF_MEMORY); + SCL_ERR_RET(terrno); } int64_t normalized = *(int64_t *)(pInput[3].columnData->pData); diff --git a/source/os/src/osString.c b/source/os/src/osString.c index 68392b5050..28ea46ba1a 100644 --- a/source/os/src/osString.c +++ b/source/os/src/osString.c @@ -63,7 +63,8 @@ char *strsep(char **stringp, const char *delim) { /* NOTREACHED */ } /* Duplicate a string, up to at most size characters */ -char *strndup(const char *s, int size) { +char *taosStrndup(const char *s, int size) { + if (s == NULL) return NULL; size_t l; char *s2; l = strlen(s); @@ -72,6 +73,8 @@ char *strndup(const char *s, int size) { if (s2) { strncpy(s2, s, l); s2[l] = '\0'; + } else { + terrno = TSDB_CODE_OUT_OF_MEMORY; } return s2; } @@ -86,6 +89,17 @@ char *stpncpy(char *dest, const char *src, int n) { } #endif +char *taosStrndup(const char *s, int size) { + if (s == NULL) { + return NULL; + } + char* p = strndup(s, size); + if (NULL == p) { + terrno = TSDB_CODE_OUT_OF_MEMORY; + } + return p; +} + int32_t taosStr2int64(const char *str, int64_t *val) { if (str == NULL || val == NULL) { return TSDB_CODE_INVALID_PARA; diff --git a/tools/shell/src/shellAuto.c b/tools/shell/src/shellAuto.c index 9b1c949807..65ae9fad54 100644 --- a/tools/shell/src/shellAuto.c +++ b/tools/shell/src/shellAuto.c @@ -1528,7 +1528,7 @@ bool needInsertFrom(char* sql, int len) { // p is string following select keyword bool appendAfterSelect(TAOS* con, SShellCmd* cmd, char* sql, int32_t len) { - char* p = strndup(sql, len); + char* p = taosStrndup(sql, len); // union all char* p1; @@ -1637,7 +1637,7 @@ bool matchSelectQuery(TAOS* con, SShellCmd* cmd) { } // search - char* sql_cp = strndup(p, len); + char* sql_cp = taosStrndup(p, len); int32_t n = searchAfterSelect(sql_cp, len); taosMemoryFree(sql_cp); if (n == -1 || n > len) return false; @@ -1712,7 +1712,7 @@ bool matchCreateTable(TAOS* con, SShellCmd* cmd) { p += 13; len -= 13; - char* ps = strndup(p, len); + char* ps = taosStrndup(p, len); bool ret = false; char* last = lastWord(ps); @@ -1765,7 +1765,7 @@ bool matchOther(TAOS* con, SShellCmd* cmd) { if (len < 8) return false; // like 'from ( ' - char* sql = strndup(p, len); + char* sql = taosStrndup(p, len); char* last = lastWord(sql); if (strcmp(last, "from(") == 0) { @@ -1828,7 +1828,7 @@ bool matchOther(TAOS* con, SShellCmd* cmd) { bool matchEnd(TAOS* con, SShellCmd* cmd) { // str dump bool ret = false; - char* ps = strndup(cmd->command, cmd->commandSize); + char* ps = taosStrndup(cmd->command, cmd->commandSize); char* last = lastWord(ps); char* elast = strrchr(last, '.'); // find end last if (elast) {