Replace unsafe memory functions with safe versions

This commit is contained in:
yihaoDeng 2024-12-04 13:35:11 +08:00
parent 0f3d290c27
commit 215208a985
10 changed files with 249 additions and 123 deletions

View File

@ -74,10 +74,15 @@ int64_t tsnprintf(char *dst, int64_t size, const char *format, ...);
char *tstrdup(const char *src); char *tstrdup(const char *src);
int32_t taosUcs4len(TdUcs4 *ucs4); int32_t taosUcs4len(TdUcs4 *ucs4);
int32_t taosStr2int64(const char *str, int64_t *val); int32_t taosStr2int64(const char *str, int64_t *val);
int32_t taosStr2int16(const char *str, int16_t *val);
int32_t taosStr2int32(const char *str, int32_t *val); int32_t taosStr2int32(const char *str, int32_t *val);
int32_t taosStr2int16(const char *str, int16_t *val);
int32_t taosStr2int8(const char *str, int8_t *val); int32_t taosStr2int8(const char *str, int8_t *val);
int32_t taosStr2Uint64(const char *str, uint64_t *val);
int32_t taosStr2Uint32(const char *str, uint32_t *val);
int32_t taosStr2Uint16(const char *str, uint16_t *val);
int32_t taosStr2Uint8(const char *str, uint8_t *val);
int32_t taosConvInit(void); int32_t taosConvInit(void);
void taosConvDestroy(); void taosConvDestroy();
iconv_t taosAcquireConv(int32_t *idx, ConvType type); iconv_t taosAcquireConv(int32_t *idx, ConvType type);

View File

@ -300,7 +300,13 @@ void* doConsumeData(void* param) {
int main(int argc, char** argv) { int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv); testing::InitGoogleTest(&argc, argv);
if (argc > 1) { if (argc > 1) {
numOfThreads = atoi(argv[1]); //numOfThreads = atoi(argv[1]);
int32_t code = taosStr2int32(argv[1], &numOfThreads);
if (code != 0) {
return code;
}
} }
numOfThreads = TMAX(numOfThreads, 1); numOfThreads = TMAX(numOfThreads, 1);

View File

@ -27,7 +27,10 @@ int32_t taosGetFqdnPortFromEp(const char* ep, SEp* pEp) {
char* temp = strchr(pEp->fqdn, ':'); char* temp = strchr(pEp->fqdn, ':');
if (temp) { if (temp) {
*temp = 0; *temp = 0;
pEp->port = atoi(temp + 1); pEp->port = taosStr2UInt16(temp + 1, NULL, 10);
if (pEp->port < 0) {
return TSDB_CODE_INVALID_PARA;
}
} }
if (pEp->port == 0) { if (pEp->port == 0) {

View File

@ -16,6 +16,7 @@
#define _DEFAULT_SOURCE #define _DEFAULT_SOURCE
#include "mndDb.h" #include "mndDb.h"
#include "audit.h" #include "audit.h"
#include "command.h"
#include "mndArbGroup.h" #include "mndArbGroup.h"
#include "mndCluster.h" #include "mndCluster.h"
#include "mndDnode.h" #include "mndDnode.h"
@ -34,7 +35,6 @@
#include "systable.h" #include "systable.h"
#include "thttp.h" #include "thttp.h"
#include "tjson.h" #include "tjson.h"
#include "command.h"
#define DB_VER_NUMBER 1 #define DB_VER_NUMBER 1
#define DB_RESERVE_SIZE 27 #define DB_RESERVE_SIZE 27
@ -416,7 +416,12 @@ static int32_t mndCheckDbName(const char *dbName, SUserObj *pUser) {
return TSDB_CODE_MND_INVALID_DB; return TSDB_CODE_MND_INVALID_DB;
} }
int32_t acctId = atoi(dbName); int32_t acctId;
int32_t code = taosStr2int32(dbName, &acctId);
if (code != 0) {
return code;
}
if (acctId != pUser->acctId) { if (acctId != pUser->acctId) {
return TSDB_CODE_MND_INVALID_DB_ACCT; return TSDB_CODE_MND_INVALID_DB_ACCT;
} }
@ -2386,7 +2391,8 @@ 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], sizeof(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++);

View File

@ -487,7 +487,8 @@ static int32_t mndCheckClusterCfgPara(SMnode *pMnode, SDnodeObj *pDnode, const S
CHECK_MONITOR_PARA(tsSlowLogScope, DND_REASON_STATUS_MONITOR_SLOW_LOG_SCOPE_NOT_MATCH); CHECK_MONITOR_PARA(tsSlowLogScope, DND_REASON_STATUS_MONITOR_SLOW_LOG_SCOPE_NOT_MATCH);
if (0 != strcasecmp(pCfg->monitorParas.tsSlowLogExceptDb, tsSlowLogExceptDb)) { if (0 != strcasecmp(pCfg->monitorParas.tsSlowLogExceptDb, tsSlowLogExceptDb)) {
mError("dnode:%d, tsSlowLogExceptDb:%s inconsistent with cluster:%s", pDnode->id, pCfg->monitorParas.tsSlowLogExceptDb, tsSlowLogExceptDb); mError("dnode:%d, tsSlowLogExceptDb:%s inconsistent with cluster:%s", pDnode->id,
pCfg->monitorParas.tsSlowLogExceptDb, tsSlowLogExceptDb);
terrno = TSDB_CODE_DNODE_INVALID_MONITOR_PARAS; terrno = TSDB_CODE_DNODE_INVALID_MONITOR_PARAS;
return DND_REASON_STATUS_MONITOR_NOT_MATCH; return DND_REASON_STATUS_MONITOR_NOT_MATCH;
} }
@ -1881,11 +1882,19 @@ static int32_t mndMCfgGetValInt32(SMCfgDnodeReq *pMCfgReq, int32_t optLen, int32
if (' ' == pMCfgReq->config[optLen]) { if (' ' == pMCfgReq->config[optLen]) {
// 'key value' // 'key value'
if (strlen(pMCfgReq->value) != 0) goto _err; if (strlen(pMCfgReq->value) != 0) goto _err;
*pOutValue = atoi(pMCfgReq->config + optLen + 1); code = taosStr2int32(pMCfgReq->config + optLen + 1, pOutValue);
if (code != 0) {
mError("dnode:%d, failed to get cfg since code: %s", pMCfgReq->dnodeId, tstrerror(code));
goto _err;
}
} else { } else {
// 'key' 'value' // 'key' 'value'
if (strlen(pMCfgReq->value) == 0) goto _err; if (strlen(pMCfgReq->value) == 0) goto _err;
*pOutValue = atoi(pMCfgReq->value); code = taosStr2int32(pMCfgReq->value, pOutValue);
if (code != 0) {
mError("dnode:%d, failed to get cfg since code: %s", pMCfgReq->dnodeId, tstrerror(code));
goto _err;
}
} }
TAOS_RETURN(code); TAOS_RETURN(code);

View File

@ -190,7 +190,14 @@ int32_t vnodeRenameVgroupId(const char *srcPath, const char *dstPath, int32_t sr
char *tsdbFilePrefixPos = strstr(oldRname, tsdbFilePrefix); char *tsdbFilePrefixPos = strstr(oldRname, tsdbFilePrefix);
if (tsdbFilePrefixPos == NULL) continue; if (tsdbFilePrefixPos == NULL) continue;
int32_t tsdbFileVgId = atoi(tsdbFilePrefixPos + prefixLen); int32_t tsdbFileVgId = 0; // atoi(tsdbFilePrefixPos + prefixLen);
ret = taosStr2int32(tsdbFilePrefixPos + prefixLen, &tsdbFileVgId);
if (ret != 0) {
vError("vgId:%d, failed to get tsdb file vgid since %s", dstVgId, tstrerror(ret));
tfsClosedir(tsdbDir);
return ret;
}
if (tsdbFileVgId == srcVgId) { if (tsdbFileVgId == srcVgId) {
char *tsdbFileSurfixPos = tsdbFilePrefixPos + prefixLen + vnodeVgroupIdLen(srcVgId); char *tsdbFileSurfixPos = tsdbFilePrefixPos + prefixLen + vnodeVgroupIdLen(srcVgId);

View File

@ -2872,7 +2872,9 @@ static int32_t getIpV4RangeFromWhitelistItem(char* ipRange, SIpV4Range* pIpRange
*slash = '\0'; *slash = '\0';
struct in_addr addr; struct in_addr addr;
if (uv_inet_pton(AF_INET, ipCopy, &addr) == 0) { if (uv_inet_pton(AF_INET, ipCopy, &addr) == 0) {
int prefix = atoi(slash + 1); int32_t prefix = 0;
code = taosStr2int32(slash + 1, &prefix);
if (code == 0) {
if (prefix < 0 || prefix > 32) { if (prefix < 0 || prefix > 32) {
code = TSDB_CODE_PAR_INVALID_IP_RANGE; code = TSDB_CODE_PAR_INVALID_IP_RANGE;
} else { } else {
@ -2880,6 +2882,7 @@ static int32_t getIpV4RangeFromWhitelistItem(char* ipRange, SIpV4Range* pIpRange
pIpRange->mask = prefix; pIpRange->mask = prefix;
code = TSDB_CODE_SUCCESS; code = TSDB_CODE_SUCCESS;
} }
}
} else { } else {
code = TSDB_CODE_PAR_INVALID_IP_RANGE; code = TSDB_CODE_PAR_INVALID_IP_RANGE;
} }

View File

@ -132,6 +132,20 @@ int32_t taosStr2int64(const char *str, int64_t *val) {
} }
} }
int32_t taosStr2int32(const char *str, int32_t *val) {
OS_PARAM_CHECK(str);
OS_PARAM_CHECK(val);
int64_t tmp = 0;
int32_t code = taosStr2int64(str, &tmp);
if (code) {
return code;
} else if (tmp > INT32_MAX || tmp < INT32_MIN) {
return TAOS_SYSTEM_ERROR(ERANGE);
} else {
*val = (int32_t)tmp;
return 0;
}
}
int32_t taosStr2int16(const char *str, int16_t *val) { int32_t taosStr2int16(const char *str, int16_t *val) {
OS_PARAM_CHECK(str); OS_PARAM_CHECK(str);
OS_PARAM_CHECK(val); OS_PARAM_CHECK(val);
@ -147,21 +161,6 @@ int32_t taosStr2int16(const char *str, int16_t *val) {
} }
} }
int32_t taosStr2int32(const char *str, int32_t *val) {
OS_PARAM_CHECK(str);
OS_PARAM_CHECK(val);
int64_t tmp = 0;
int32_t code = taosStr2int64(str, &tmp);
if (code) {
return code;
} else if (tmp > INT32_MAX || tmp < INT32_MIN) {
return TAOS_SYSTEM_ERROR(ERANGE);
} else {
*val = (int32_t)tmp;
return 0;
}
}
int32_t taosStr2int8(const char *str, int8_t *val) { int32_t taosStr2int8(const char *str, int8_t *val) {
OS_PARAM_CHECK(str); OS_PARAM_CHECK(str);
OS_PARAM_CHECK(val); OS_PARAM_CHECK(val);
@ -177,6 +176,67 @@ int32_t taosStr2int8(const char *str, int8_t *val) {
} }
} }
int32_t taosStr2Uint64(const char *str, uint64_t *val) {
if (str == NULL || val == NULL) {
return TSDB_CODE_INVALID_PARA;
}
char *endptr = NULL;
uint64_t ret = strtoull(str, &endptr, 10);
if (errno == ERANGE && (ret == ULLONG_MAX)) {
return TAOS_SYSTEM_ERROR(errno);
} else if (errno == EINVAL && ret == 0) {
return TSDB_CODE_INVALID_PARA;
} else {
*val = ret;
return 0;
}
}
int32_t taosStr2Uint32(const char *str, uint32_t *val) {
OS_PARAM_CHECK(str);
OS_PARAM_CHECK(val);
uint64_t tmp = 0;
int32_t code = taosStr2Uint64(str, &tmp);
if (code) {
return code;
} else if (tmp > UINT32_MAX) {
return TAOS_SYSTEM_ERROR(ERANGE);
} else {
*val = (int32_t)tmp;
return 0;
}
}
int32_t taosStr2Uint16(const char *str, uint16_t *val) {
OS_PARAM_CHECK(str);
OS_PARAM_CHECK(val);
uint64_t tmp = 0;
int32_t code = taosStr2Uint64(str, &tmp);
if (code) {
return code;
} else if (tmp > UINT16_MAX) {
return TAOS_SYSTEM_ERROR(ERANGE);
} else {
*val = (int16_t)tmp;
return 0;
}
}
int32_t taosStr2Uint8(const char *str, uint8_t *val) {
OS_PARAM_CHECK(str);
OS_PARAM_CHECK(val);
uint64_t tmp = 0;
int32_t code = taosStr2Uint64(str, &tmp);
if (code) {
return code;
} else if (tmp > UINT8_MAX) {
return TAOS_SYSTEM_ERROR(ERANGE);
} else {
*val = (int8_t)tmp;
return 0;
}
}
int32_t tasoUcs4Compare(TdUcs4 *f1_ucs4, TdUcs4 *f2_ucs4, int32_t bytes) { int32_t tasoUcs4Compare(TdUcs4 *f1_ucs4, TdUcs4 *f2_ucs4, int32_t bytes) {
if ((f1_ucs4 == NULL || f2_ucs4 == NULL)) { if ((f1_ucs4 == NULL || f2_ucs4 == NULL)) {
return TSDB_CODE_INVALID_PARA; return TSDB_CODE_INVALID_PARA;
@ -558,7 +618,8 @@ int32_t taosMbsToWchars(TdWchar *pWchars, const char *pStrs, int32_t size) {
int32_t taosWcharToMb(char *pStr, TdWchar wchar) { int32_t taosWcharToMb(char *pStr, TdWchar wchar) {
OS_PARAM_CHECK(pStr); OS_PARAM_CHECK(pStr);
return wctomb(pStr, wchar); } return wctomb(pStr, wchar);
}
char *taosStrCaseStr(const char *str, const char *pattern) { char *taosStrCaseStr(const char *str, const char *pattern) {
if (str == NULL) { if (str == NULL) {

View File

@ -151,17 +151,19 @@ static int32_t cfgCheckAndSetDir(SConfigItem *pItem, const char *inputDir) {
} }
static int32_t cfgSetBool(SConfigItem *pItem, const char *value, ECfgSrcType stype) { static int32_t cfgSetBool(SConfigItem *pItem, const char *value, ECfgSrcType stype) {
int32_t code = 0;
bool tmp = false; bool tmp = false;
if (strcasecmp(value, "true") == 0) { if (strcasecmp(value, "true") == 0) {
tmp = true; tmp = true;
} }
if (atoi(value) > 0) { int32_t val = 0;
if ((code = taosStr2int32(value, &val)) == 0 && val > 0) {
tmp = true; tmp = true;
} }
pItem->bval = tmp; pItem->bval = tmp;
pItem->stype = stype; pItem->stype = stype;
return 0; return code;
} }
static int32_t cfgSetInt32(SConfigItem *pItem, const char *value, ECfgSrcType stype) { static int32_t cfgSetInt32(SConfigItem *pItem, const char *value, ECfgSrcType stype) {
@ -258,6 +260,7 @@ static int32_t cfgSetTimezone(SConfigItem *pItem, const char *value, ECfgSrcType
static int32_t cfgSetTfsItem(SConfig *pCfg, const char *name, const char *value, const char *level, const char *primary, static int32_t cfgSetTfsItem(SConfig *pCfg, const char *name, const char *value, const char *level, const char *primary,
const char *disable, ECfgSrcType stype) { const char *disable, ECfgSrcType stype) {
int32_t code = 0;
(void)taosThreadMutexLock(&pCfg->lock); (void)taosThreadMutexLock(&pCfg->lock);
SConfigItem *pItem = cfgGetItem(pCfg, name); SConfigItem *pItem = cfgGetItem(pCfg, name);
@ -278,9 +281,21 @@ static int32_t cfgSetTfsItem(SConfig *pCfg, const char *name, const char *value,
SDiskCfg cfg = {0}; SDiskCfg cfg = {0};
tstrncpy(cfg.dir, pItem->str, sizeof(cfg.dir)); tstrncpy(cfg.dir, pItem->str, sizeof(cfg.dir));
cfg.level = level ? atoi(level) : 0;
cfg.primary = primary ? atoi(primary) : 1; code = taosStr2int32(level, &cfg.level);
cfg.disable = disable ? atoi(disable) : 0; if (code != 0) {
cfg.level = 0;
}
code = taosStr2int32(primary, &cfg.primary);
if (code != 0) {
cfg.primary = 1;
}
code = taosStr2int8(primary, &cfg.disable);
if (code != 0) {
cfg.disable = 0;
}
void *ret = taosArrayPush(pItem->array, &cfg); void *ret = taosArrayPush(pItem->array, &cfg);
if (ret == NULL) { if (ret == NULL) {
(void)taosThreadMutexUnlock(&pCfg->lock); (void)taosThreadMutexUnlock(&pCfg->lock);
@ -412,6 +427,7 @@ void cfgLock(SConfig *pCfg) {
void cfgUnLock(SConfig *pCfg) { (void)taosThreadMutexUnlock(&pCfg->lock); } void cfgUnLock(SConfig *pCfg) { (void)taosThreadMutexUnlock(&pCfg->lock); }
int32_t cfgCheckRangeForDynUpdate(SConfig *pCfg, const char *name, const char *pVal, bool isServer) { int32_t cfgCheckRangeForDynUpdate(SConfig *pCfg, const char *name, const char *pVal, bool isServer) {
int32_t code = 0;
ECfgDynType dynType = isServer ? CFG_DYN_SERVER : CFG_DYN_CLIENT; ECfgDynType dynType = isServer ? CFG_DYN_SERVER : CFG_DYN_CLIENT;
cfgLock(pCfg); cfgLock(pCfg);
@ -443,8 +459,9 @@ int32_t cfgCheckRangeForDynUpdate(SConfig *pCfg, const char *name, const char *p
} }
} break; } break;
case CFG_DTYPE_BOOL: { case CFG_DTYPE_BOOL: {
int32_t ival = (int32_t)atoi(pVal); int32_t ival = 0;
if (ival != 0 && ival != 1) { code = taosStr2int32(pVal, &ival);
if (code != 0 || (ival != 0 && ival != 1)) {
uError("cfg:%s, type:%s value:%d out of range[0, 1]", pItem->name, cfgDtypeStr(pItem->dtype), ival); uError("cfg:%s, type:%s value:%d out of range[0, 1]", pItem->name, cfgDtypeStr(pItem->dtype), ival);
cfgUnLock(pCfg); cfgUnLock(pCfg);
TAOS_RETURN(TSDB_CODE_OUT_OF_RANGE); TAOS_RETURN(TSDB_CODE_OUT_OF_RANGE);
@ -887,7 +904,8 @@ void cfgDumpCfg(SConfig *pCfg, bool tsc, bool dump) {
for (size_t j = 0; j < sz; ++j) { for (size_t j = 0; j < sz; ++j) {
SDiskCfg *pCfg = taosArrayGet(pItem->array, j); SDiskCfg *pCfg = taosArrayGet(pItem->array, j);
if (dump) { if (dump) {
(void)printf("%s %s %s l:%d p:%d d:%"PRIi8"\n", src, name, pCfg->dir, pCfg->level, pCfg->primary, pCfg->disable); (void)printf("%s %s %s l:%d p:%d d:%" PRIi8 "\n", src, name, pCfg->dir, pCfg->level, pCfg->primary,
pCfg->disable);
} else { } else {
uInfo("%s %s %s l:%d p:%d d:%" PRIi8, src, name, pCfg->dir, pCfg->level, pCfg->primary, pCfg->disable); uInfo("%s %s %s l:%d p:%d d:%" PRIi8, src, name, pCfg->dir, pCfg->level, pCfg->primary, pCfg->disable);
} }

View File

@ -18,6 +18,7 @@
#include "taoserror.h" #include "taoserror.h"
int32_t taosVersionStrToInt(const char *vstr, int32_t *vint) { int32_t taosVersionStrToInt(const char *vstr, int32_t *vint) {
int32_t code = 0;
if (vstr == NULL) { if (vstr == NULL) {
return terrno = TSDB_CODE_INVALID_VERSION_STRING; return terrno = TSDB_CODE_INVALID_VERSION_STRING;
} }
@ -31,7 +32,11 @@ int32_t taosVersionStrToInt(const char *vstr, int32_t *vint) {
if (vstr[spos] != '.') { if (vstr[spos] != '.') {
tmp[spos - tpos] = vstr[spos]; tmp[spos - tpos] = vstr[spos];
} else { } else {
vnum[vpos] = atoi(tmp); code = taosStr2int32(tmp, &vnum[vpos]);
if (code != 0) {
return code;
}
// vnum[vpos] = atoi(tmp);
memset(tmp, 0, sizeof(tmp)); memset(tmp, 0, sizeof(tmp));
vpos++; vpos++;
tpos = spos + 1; tpos = spos + 1;
@ -39,7 +44,10 @@ int32_t taosVersionStrToInt(const char *vstr, int32_t *vint) {
} }
if ('\0' != tmp[0] && vpos < 4) { if ('\0' != tmp[0] && vpos < 4) {
vnum[vpos] = atoi(tmp); code = taosStr2int32(tmp, &vnum[vpos]);
if (code != 0) {
return code;
}
} }
if (vnum[0] <= 0) { if (vnum[0] <= 0) {