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 1/3] 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) { From 6749067c33456d4386a49f14985a9f465ce81ae3 Mon Sep 17 00:00:00 2001 From: xsren <285808407@qq.com> Date: Thu, 26 Sep 2024 15:47:05 +0800 Subject: [PATCH 2/3] build error --- source/dnode/mnode/impl/src/mndStreamTrans.c | 2 +- source/os/src/osString.c | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/source/dnode/mnode/impl/src/mndStreamTrans.c b/source/dnode/mnode/impl/src/mndStreamTrans.c index c16016cf13..7171d44da4 100644 --- a/source/dnode/mnode/impl/src/mndStreamTrans.c +++ b/source/dnode/mnode/impl/src/mndStreamTrans.c @@ -324,7 +324,7 @@ void killAllCheckpointTrans(SMnode *pMnode, SVgroupChangeInfo *pChangeInfo) { size_t len = 0; void *pKey = taosHashGetKey(pDb, &len); - stpncpy(p, pKey, 127); + tstrncpy(p, pKey, 128); int32_t code = doKillCheckpointTrans(pMnode, pKey, len); if (code) { diff --git a/source/os/src/osString.c b/source/os/src/osString.c index 28ea46ba1a..d265ed510a 100644 --- a/source/os/src/osString.c +++ b/source/os/src/osString.c @@ -87,18 +87,18 @@ char *stpncpy(char *dest, const char *src, int n) { if (size == n) return dest; return memset(dest, '\0', n - size); } -#endif - +#else char *taosStrndup(const char *s, int size) { if (s == NULL) { return NULL; } - char* p = strndup(s, size); + char *p = strndup(s, size); if (NULL == p) { terrno = TSDB_CODE_OUT_OF_MEMORY; } return p; } +#endif int32_t taosStr2int64(const char *str, int64_t *val) { if (str == NULL || val == NULL) { From 9972357c17b020291c91ffcbbc97ded48b7a167d Mon Sep 17 00:00:00 2001 From: xsren <285808407@qq.com> Date: Thu, 26 Sep 2024 18:13:52 +0800 Subject: [PATCH 3/3] file function result --- source/libs/executor/src/groupcacheoperator.c | 8 ++--- source/libs/index/inc/indexFstFile.h | 4 +-- source/libs/index/src/indexFstFile.c | 6 ++-- source/libs/index/src/indexTfile.c | 12 ++++---- source/libs/wal/src/walRead.c | 4 +-- source/libs/wal/src/walWrite.c | 30 +++++++++---------- source/os/src/osRand.c | 2 +- source/os/src/osSysinfo.c | 4 +-- source/os/test/osStringTests.cpp | 2 +- source/util/src/tpagedbuf.c | 11 ++++--- 10 files changed, 41 insertions(+), 42 deletions(-) diff --git a/source/libs/executor/src/groupcacheoperator.c b/source/libs/executor/src/groupcacheoperator.c index e4f6d73b7b..d785a1e619 100644 --- a/source/libs/executor/src/groupcacheoperator.c +++ b/source/libs/executor/src/groupcacheoperator.c @@ -333,14 +333,14 @@ static int32_t saveBlocksToDisk(SGroupCacheOperatorInfo* pGCache, SGcDownstreamC continue; } - int32_t ret = taosLSeekFile(pFd->fd, pHead->basic.offset, SEEK_SET); + int64_t ret = taosLSeekFile(pFd->fd, pHead->basic.offset, SEEK_SET); if (ret < 0) { releaseFdToFileCtx(pFd); code = terrno; goto _return; } - ret = (int32_t)taosWriteFile(pFd->fd, pHead->pBuf, pHead->basic.bufSize); + ret = taosWriteFile(pFd->fd, pHead->pBuf, pHead->basic.bufSize); if (ret != pHead->basic.bufSize) { releaseFdToFileCtx(pFd); code = terrno; @@ -578,7 +578,7 @@ static int32_t readBlockFromDisk(SGroupCacheOperatorInfo* pGCache, SGroupCacheDa return TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR; } - int32_t ret = taosLSeekFile(pFileFd->fd, pBasic->offset, SEEK_SET); + int64_t ret = taosLSeekFile(pFileFd->fd, pBasic->offset, SEEK_SET); if (ret < 0) { code = terrno; goto _return; @@ -590,7 +590,7 @@ static int32_t readBlockFromDisk(SGroupCacheOperatorInfo* pGCache, SGroupCacheDa goto _return; } - ret = (int32_t)taosReadFile(pFileFd->fd, *ppBuf, pBasic->bufSize); + ret = taosReadFile(pFileFd->fd, *ppBuf, pBasic->bufSize); if (ret != pBasic->bufSize) { taosMemoryFreeClear(*ppBuf); code = terrno; diff --git a/source/libs/index/inc/indexFstFile.h b/source/libs/index/inc/indexFstFile.h index b1ff4fee34..145e1040cd 100644 --- a/source/libs/index/inc/indexFstFile.h +++ b/source/libs/index/inc/indexFstFile.h @@ -33,7 +33,7 @@ typedef struct IFileCtx { int (*write)(struct IFileCtx* ctx, uint8_t* buf, int len); int (*read)(struct IFileCtx* ctx, uint8_t* buf, int len); int (*flush)(struct IFileCtx* ctx); - int (*readFrom)(struct IFileCtx* ctx, uint8_t* buf, int len, int32_t offset); + int64_t (*readFrom)(struct IFileCtx* ctx, uint8_t* buf, int len, int32_t offset); int (*size)(struct IFileCtx* ctx); SLRUCache* lru; @@ -64,7 +64,7 @@ typedef struct IFileCtx { static int idxFileCtxDoWrite(IFileCtx* ctx, uint8_t* buf, int len); static int idxFileCtxDoRead(IFileCtx* ctx, uint8_t* buf, int len); -static int idxFileCtxDoReadFrom(IFileCtx* ctx, uint8_t* buf, int len, int32_t offset); +static int64_t idxFileCtxDoReadFrom(IFileCtx* ctx, uint8_t* buf, int len, int32_t offset); static int idxFileCtxDoFlush(IFileCtx* ctx); IFileCtx* idxFileCtxCreate(WriterType type, const char* path, bool readOnly, int32_t capacity); diff --git a/source/libs/index/src/indexFstFile.c b/source/libs/index/src/indexFstFile.c index 73ecb22b3b..793aaa810e 100644 --- a/source/libs/index/src/indexFstFile.c +++ b/source/libs/index/src/indexFstFile.c @@ -96,8 +96,8 @@ static FORCE_INLINE int idxFileCtxDoRead(IFileCtx* ctx, uint8_t* buf, int len) { return nRead; } -static int idxFileCtxDoReadFrom(IFileCtx* ctx, uint8_t* buf, int len, int32_t offset) { - int32_t total = 0, nread = 0; +static int64_t idxFileCtxDoReadFrom(IFileCtx* ctx, uint8_t* buf, int len, int32_t offset) { + int64_t total = 0, nread = 0; int32_t blkId = offset / kBlockSize; int32_t blkOffset = offset % kBlockSize; int32_t blkLeft = kBlockSize - blkOffset; @@ -122,7 +122,7 @@ static int idxFileCtxDoReadFrom(IFileCtx* ctx, uint8_t* buf, int len, int32_t of int32_t left = ctx->file.size - offset; if (left < kBlockSize) { nread = TMIN(left, len); - int32_t bytes = taosPReadFile(ctx->file.pFile, buf + total, nread, offset); + int64_t bytes = taosPReadFile(ctx->file.pFile, buf + total, nread, offset); if (bytes != nread) { total = TSDB_CODE_INDEX_INVALID_FILE; break; diff --git a/source/libs/index/src/indexTfile.c b/source/libs/index/src/indexTfile.c index 1130243e27..e2693ff4a4 100644 --- a/source/libs/index/src/indexTfile.c +++ b/source/libs/index/src/indexTfile.c @@ -45,7 +45,7 @@ static int tfileWriteFooter(TFileWriter* write); // handle file corrupt later static int tfileReaderLoadHeader(TFileReader* reader); -static int tfileReaderLoadFst(TFileReader* reader); +static int32_t tfileReaderLoadFst(TFileReader* reader); static int tfileReaderVerify(TFileReader* reader); static int tfileReaderLoadTableIds(TFileReader* reader, int32_t offset, SArray* result); @@ -1022,7 +1022,7 @@ static int tfileReaderLoadHeader(TFileReader* reader) { int64_t nread = reader->ctx->readFrom(reader->ctx, (uint8_t*)buf, sizeof(buf), 0); - if (nread == -1) { + if (nread < 0) { indexError("actual Read: %d, to read: %d, code:0x%x, filename: %s", (int)(nread), (int)sizeof(buf), errno, reader->ctx->file.buf); } else { @@ -1032,7 +1032,7 @@ static int tfileReaderLoadHeader(TFileReader* reader) { return 0; } -static int tfileReaderLoadFst(TFileReader* reader) { +static int32_t tfileReaderLoadFst(TFileReader* reader) { IFileCtx* ctx = reader->ctx; int size = ctx->size(ctx); @@ -1040,7 +1040,7 @@ static int tfileReaderLoadFst(TFileReader* reader) { int fstSize = size - reader->header.fstOffset - sizeof(FILE_MAGIC_NUMBER); char* buf = taosMemoryCalloc(1, fstSize); if (buf == NULL) { - return -1; + return terrno; } int64_t ts = taosGetTimestampUs(); @@ -1058,7 +1058,7 @@ static int tfileReaderLoadFst(TFileReader* reader) { taosMemoryFree(buf); fstSliceDestroy(&st); - return reader->fst != NULL ? 0 : -1; + return reader->fst != NULL ? 0 : TSDB_CODE_INDEX_INVALID_FILE; } static int32_t tfileReaderLoadTableIds(TFileReader* reader, int32_t offset, SArray* result) { // TODO(yihao): opt later @@ -1067,7 +1067,7 @@ static int32_t tfileReaderLoadTableIds(TFileReader* reader, int32_t offset, SArr IFileCtx* ctx = reader->ctx; // add block cache char block[4096] = {0}; - int32_t nread = ctx->readFrom(ctx, (uint8_t*)block, sizeof(block), offset); + int64_t nread = ctx->readFrom(ctx, (uint8_t*)block, sizeof(block), offset); if (nread < sizeof(uint32_t)) { return TSDB_CODE_INDEX_INVALID_FILE; } diff --git a/source/libs/wal/src/walRead.c b/source/libs/wal/src/walRead.c index d3df76f687..610adfb0e1 100644 --- a/source/libs/wal/src/walRead.c +++ b/source/libs/wal/src/walRead.c @@ -313,8 +313,8 @@ int32_t walSkipFetchBody(SWalReader *pRead) { if (pRead->pWal->cfg.encryptAlgorithm == 1) { cryptedBodyLen = ENCRYPTED_LEN(cryptedBodyLen); } - int64_t code = taosLSeekFile(pRead->pLogFile, cryptedBodyLen, SEEK_CUR); - if (code < 0) { + int64_t ret = taosLSeekFile(pRead->pLogFile, cryptedBodyLen, SEEK_CUR); + if (ret < 0) { TAOS_RETURN(terrno); } diff --git a/source/libs/wal/src/walWrite.c b/source/libs/wal/src/walWrite.c index b89c233465..c8c37b11bc 100644 --- a/source/libs/wal/src/walWrite.c +++ b/source/libs/wal/src/walWrite.c @@ -109,20 +109,20 @@ static int64_t walChangeWrite(SWal *pWal, int64_t ver) { char fnameStr[WAL_FILE_LEN]; if (pWal->pLogFile != NULL) { if (pWal->cfg.level != TAOS_WAL_SKIP && (code = taosFsyncFile(pWal->pLogFile)) != 0) { - TAOS_RETURN(terrno); + return -1; } code = taosCloseFile(&pWal->pLogFile); if (code != 0) { - TAOS_RETURN(terrno); + return -1; } } if (pWal->pIdxFile != NULL) { if (pWal->cfg.level != TAOS_WAL_SKIP && (code = taosFsyncFile(pWal->pIdxFile)) != 0) { - TAOS_RETURN(terrno); + return -1; } code = taosCloseFile(&pWal->pIdxFile); if (code != 0) { - TAOS_RETURN(terrno); + return -1; } } @@ -139,7 +139,7 @@ static int64_t walChangeWrite(SWal *pWal, int64_t ver) { if (pIdxTFile == NULL) { pWal->pIdxFile = NULL; - TAOS_RETURN(terrno); + return -1; } walBuildLogName(pWal, fileFirstVer, fnameStr); pLogTFile = taosOpenFile(fnameStr, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_APPEND); @@ -147,7 +147,7 @@ static int64_t walChangeWrite(SWal *pWal, int64_t ver) { TAOS_UNUSED(taosCloseFile(&pIdxTFile)); pWal->pLogFile = NULL; - TAOS_RETURN(terrno); + return -1; } pWal->pLogFile = pLogTFile; @@ -160,7 +160,7 @@ static int64_t walChangeWrite(SWal *pWal, int64_t ver) { int32_t walRollback(SWal *pWal, int64_t ver) { TAOS_UNUSED(taosThreadRwlockWrlock(&pWal->mutex)); wInfo("vgId:%d, wal rollback for version %" PRId64, pWal->cfg.vgId, ver); - int64_t code; + int64_t ret; char fnameStr[WAL_FILE_LEN]; if (ver > pWal->vers.lastVer || ver <= pWal->vers.commitVer || ver <= pWal->vers.snapshotVer) { TAOS_UNUSED(taosThreadRwlockUnlock(&pWal->mutex)); @@ -171,11 +171,11 @@ int32_t walRollback(SWal *pWal, int64_t ver) { // find correct file if (ver < walGetLastFileFirstVer(pWal)) { // change current files - code = walChangeWrite(pWal, ver); - if (code < 0) { + ret = walChangeWrite(pWal, ver); + if (ret < 0) { TAOS_UNUSED(taosThreadRwlockUnlock(&pWal->mutex)); - TAOS_RETURN(code); + TAOS_RETURN(terrno); } // delete files in descending order @@ -205,8 +205,8 @@ int32_t walRollback(SWal *pWal, int64_t ver) { TAOS_RETURN(terrno); } int64_t idxOff = walGetVerIdxOffset(pWal, ver); - code = taosLSeekFile(pIdxFile, idxOff, SEEK_SET); - if (code < 0) { + ret = taosLSeekFile(pIdxFile, idxOff, SEEK_SET); + if (ret < 0) { TAOS_UNUSED(taosThreadRwlockUnlock(&pWal->mutex)); TAOS_RETURN(terrno); @@ -229,8 +229,8 @@ int32_t walRollback(SWal *pWal, int64_t ver) { TAOS_RETURN(terrno); } - code = taosLSeekFile(pLogFile, entry.offset, SEEK_SET); - if (code < 0) { + ret = taosLSeekFile(pLogFile, entry.offset, SEEK_SET); + if (ret < 0) { // TODO TAOS_UNUSED(taosThreadRwlockUnlock(&pWal->mutex)); @@ -244,7 +244,7 @@ int32_t walRollback(SWal *pWal, int64_t ver) { TAOS_RETURN(terrno); } - code = walValidHeadCksum(&head); + int32_t code = walValidHeadCksum(&head); if (code != 0) { TAOS_UNUSED(taosThreadRwlockUnlock(&pWal->mutex)); diff --git a/source/os/src/osRand.c b/source/os/src/osRand.c index b99017782b..0bf67f96a0 100644 --- a/source/os/src/osRand.c +++ b/source/os/src/osRand.c @@ -65,7 +65,7 @@ uint32_t taosSafeRand(void) { if (pFile == NULL) { seed = (int)taosGetTimestampSec(); } else { - int len = taosReadFile(pFile, &seed, sizeof(seed)); + int64_t len = taosReadFile(pFile, &seed, sizeof(seed)); if (len < 0) { seed = (int)taosGetTimestampSec(); } diff --git a/source/os/src/osSysinfo.c b/source/os/src/osSysinfo.c index 4d97232806..cb6d3a7736 100644 --- a/source/os/src/osSysinfo.c +++ b/source/os/src/osSysinfo.c @@ -1057,7 +1057,7 @@ int32_t taosGetSystemUUID(char *uid, int32_t uidlen) { int n = snprintf(uid, uidlen, "%.*s", (int)sizeof(buf), buf); // though less performance, much safer return 0; #else - int len = 0; + int64_t len = 0; // fd = open("/proc/sys/kernel/random/uuid", 0); TdFilePtr pFile = taosOpenFile("/proc/sys/kernel/random/uuid", TD_FILE_READ); @@ -1067,7 +1067,7 @@ int32_t taosGetSystemUUID(char *uid, int32_t uidlen) { len = taosReadFile(pFile, uid, uidlen); TAOS_SKIP_ERROR(taosCloseFile(&pFile)); if (len < 0) { - return len; + return terrno; } } diff --git a/source/os/test/osStringTests.cpp b/source/os/test/osStringTests.cpp index 5e07636b1f..9565e568fb 100644 --- a/source/os/test/osStringTests.cpp +++ b/source/os/test/osStringTests.cpp @@ -74,7 +74,7 @@ TEST(osStringTests, strsepNullInput) { TEST(osStringTests, strndupNormalInput) { const char s[] = "This is a test string."; int size = strlen(s) + 1; - char * s2 = strndup(s, size); + char * s2 = taosStrndup(s, size); EXPECT_STREQ(s, s2); diff --git a/source/util/src/tpagedbuf.c b/source/util/src/tpagedbuf.c index 25e10a17df..fb8e597163 100644 --- a/source/util/src/tpagedbuf.c +++ b/source/util/src/tpagedbuf.c @@ -134,9 +134,8 @@ static uint64_t allocateNewPositionInFile(SDiskbasedBuf* pBuf, size_t size) { static FORCE_INLINE size_t getAllocPageSize(int32_t pageSize) { return pageSize + POINTER_BYTES + sizeof(SFilePage); } static int32_t doFlushBufPageImpl(SDiskbasedBuf* pBuf, int64_t offset, const char* pData, int32_t size) { - int32_t ret = taosLSeekFile(pBuf->pFile, offset, SEEK_SET); - if (ret == -1) { - terrno = terrno; + int64_t ret = taosLSeekFile(pBuf->pFile, offset, SEEK_SET); + if (ret < 0) { return terrno; } @@ -246,14 +245,14 @@ static int32_t loadPageFromDisk(SDiskbasedBuf* pBuf, SPageInfo* pg) { return TSDB_CODE_INVALID_PARA; } - int32_t ret = taosLSeekFile(pBuf->pFile, pg->offset, SEEK_SET); - if (ret == -1) { + int64_t ret = taosLSeekFile(pBuf->pFile, pg->offset, SEEK_SET); + if (ret < 0) { ret = terrno; return ret; } void* pPage = (void*)GET_PAYLOAD_DATA(pg); - ret = (int32_t)taosReadFile(pBuf->pFile, pPage, pg->length); + ret = taosReadFile(pBuf->pFile, pPage, pg->length); if (ret != pg->length) { ret = terrno; return ret;