fix: TD-22674 coverity scan (#20126)
* fix: TD-22674 coverity scan * fix: compile error --------- Co-authored-by: facetosea <25808407@qq.com>
This commit is contained in:
parent
bd7955be14
commit
e3f21ae98f
|
@ -89,6 +89,8 @@ typedef struct dirent TdDirEntry;
|
|||
|
||||
#endif
|
||||
|
||||
#define TDDIRMAXLEN 1024
|
||||
|
||||
void taosRemoveDir(const char *dirname) {
|
||||
TdDirPtr pDir = taosOpenDir(dirname);
|
||||
if (pDir == NULL) return;
|
||||
|
@ -133,8 +135,8 @@ int32_t taosMkDir(const char *dirname) {
|
|||
}
|
||||
|
||||
int32_t taosMulMkDir(const char *dirname) {
|
||||
if (dirname == NULL) return -1;
|
||||
char temp[1024];
|
||||
if (dirname == NULL || strlen(dirname) >= TDDIRMAXLEN) return -1;
|
||||
char temp[TDDIRMAXLEN];
|
||||
char *pos = temp;
|
||||
int32_t code = 0;
|
||||
#ifdef WINDOWS
|
||||
|
@ -192,8 +194,8 @@ int32_t taosMulMkDir(const char *dirname) {
|
|||
}
|
||||
|
||||
int32_t taosMulModeMkDir(const char *dirname, int mode) {
|
||||
if (dirname == NULL) return -1;
|
||||
char temp[1024];
|
||||
if (dirname == NULL || strlen(dirname) >= TDDIRMAXLEN) return -1;
|
||||
char temp[TDDIRMAXLEN];
|
||||
char *pos = temp;
|
||||
int32_t code = 0;
|
||||
#ifdef WINDOWS
|
||||
|
@ -204,8 +206,7 @@ int32_t taosMulModeMkDir(const char *dirname, int mode) {
|
|||
#endif
|
||||
|
||||
if (taosDirExist(temp)) {
|
||||
chmod(temp, mode);
|
||||
return code;
|
||||
return chmod(temp, mode);
|
||||
}
|
||||
|
||||
if (strncmp(temp, TD_DIRSEP, 1) == 0) {
|
||||
|
@ -247,12 +248,10 @@ int32_t taosMulModeMkDir(const char *dirname, int mode) {
|
|||
}
|
||||
|
||||
if (code < 0 && errno == EEXIST) {
|
||||
chmod(temp, mode);
|
||||
return 0;
|
||||
return chmod(temp, mode);
|
||||
}
|
||||
|
||||
chmod(temp, mode);
|
||||
return code;
|
||||
return chmod(temp, mode);
|
||||
}
|
||||
|
||||
void taosRemoveOldFiles(const char *dirname, int32_t keepDays) {
|
||||
|
|
|
@ -132,15 +132,20 @@ int64_t taosCopyFile(const char *from, const char *to) {
|
|||
if (bytes < sizeof(buffer)) break;
|
||||
}
|
||||
|
||||
taosFsyncFile(pFileTo);
|
||||
int code = taosFsyncFile(pFileTo);
|
||||
|
||||
taosCloseFile(&pFileFrom);
|
||||
taosCloseFile(&pFileTo);
|
||||
|
||||
if (code != 0) {
|
||||
return -1;
|
||||
}
|
||||
return size;
|
||||
|
||||
_err:
|
||||
if (pFileFrom != NULL) taosCloseFile(&pFileFrom);
|
||||
if (pFileTo != NULL) taosCloseFile(&pFileTo);
|
||||
/* coverity[+retval] */
|
||||
taosRemoveFile(to);
|
||||
return -1;
|
||||
#endif
|
||||
|
@ -506,13 +511,13 @@ int64_t taosPWriteFile(TdFilePtr pFile, const void *buf, int64_t count, int64_t
|
|||
}
|
||||
|
||||
int64_t taosLSeekFile(TdFilePtr pFile, int64_t offset, int32_t whence) {
|
||||
if (pFile == NULL || pFile->fd < 0) {
|
||||
return -1;
|
||||
}
|
||||
#if FILE_WITH_LOCK
|
||||
taosThreadRwlockRdlock(&(pFile->rwlock));
|
||||
#endif
|
||||
ASSERT(pFile->fd >= 0); // Please check if you have closed the file.
|
||||
if (pFile->fd < 0) {
|
||||
return -1;
|
||||
}
|
||||
#ifdef WINDOWS
|
||||
int64_t ret = _lseeki64(pFile->fd, offset, whence);
|
||||
#else
|
||||
|
|
|
@ -745,8 +745,10 @@ bool taosValidIpAndPort(uint32_t ip, uint16_t port) {
|
|||
#endif
|
||||
serverAdd.sin_port = (uint16_t)htons(port);
|
||||
|
||||
if ((fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) <= 2) {
|
||||
// printf("failed to open TCP socket: %d (%s)", errno, strerror(errno));
|
||||
fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
||||
if (fd < 0) { // exception
|
||||
return false;
|
||||
} else if (fd <= 2) { // in, out, err
|
||||
taosCloseSocketNoCheck1(fd);
|
||||
return false;
|
||||
}
|
||||
|
|
|
@ -439,11 +439,14 @@ int32_t taosGetCpuInfo(char *cpuModel, int32_t maxLen, float *numOfCores) {
|
|||
|
||||
if (code != 0 && (done & 1) == 0) {
|
||||
TdFilePtr pFile1 = taosOpenFile("/proc/device-tree/model", TD_FILE_READ | TD_FILE_STREAM);
|
||||
if (pFile1 == NULL) return code;
|
||||
taosGetsFile(pFile1, maxLen, cpuModel);
|
||||
taosCloseFile(&pFile1);
|
||||
code = 0;
|
||||
done |= 1;
|
||||
if (pFile1 != NULL) {
|
||||
ssize_t bytes = taosGetsFile(pFile1, maxLen, cpuModel);
|
||||
taosCloseFile(&pFile);
|
||||
if (bytes > 0) {
|
||||
code = 0;
|
||||
done |= 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (code != 0 && (done & 1) == 0) {
|
||||
|
@ -498,7 +501,7 @@ void taosGetCpuUsage(double *cpu_system, double *cpu_engine) {
|
|||
curSysTotal = curSysUsed + sysCpu.idle;
|
||||
curProcTotal = procCpu.utime + procCpu.stime + procCpu.cutime + procCpu.cstime;
|
||||
|
||||
if (curSysTotal > lastSysTotal && curSysUsed >= lastSysUsed && curProcTotal >= lastProcTotal) {
|
||||
if (curSysTotal - lastSysTotal > 0 && curSysUsed >= lastSysUsed && curProcTotal >= lastProcTotal) {
|
||||
if (cpu_system != NULL) {
|
||||
*cpu_system = (curSysUsed - lastSysUsed) / (double)(curSysTotal - lastSysTotal) * 100;
|
||||
}
|
||||
|
@ -610,12 +613,6 @@ int32_t taosGetProcMemory(int64_t *usedKB) {
|
|||
}
|
||||
}
|
||||
|
||||
if (strlen(line) < 0) {
|
||||
// printf("read file:%s failed", tsProcMemFile);
|
||||
taosCloseFile(&pFile);
|
||||
return -1;
|
||||
}
|
||||
|
||||
char tmp[10];
|
||||
sscanf(line, "%s %" PRId64, tmp, usedKB);
|
||||
|
||||
|
|
|
@ -909,7 +909,7 @@ void taosGetSystemTimezone(char *outTimezoneStr, enum TdTimezone *tsTimezone) {
|
|||
char buf[4096] = {0};
|
||||
char *tz = NULL;
|
||||
{
|
||||
int n = readlink("/etc/localtime", buf, sizeof(buf));
|
||||
int n = readlink("/etc/localtime", buf, sizeof(buf)-1);
|
||||
if (n < 0) {
|
||||
printf("read /etc/localtime error, reason:%s", strerror(errno));
|
||||
|
||||
|
|
|
@ -845,6 +845,8 @@ void shellReadHistory() {
|
|||
i = (i + SHELL_MAX_HISTORY_SIZE - 1) % SHELL_MAX_HISTORY_SIZE;
|
||||
}
|
||||
taosFprintfFile(pFile, "%s\n", pHistory->hist[endIndex]);
|
||||
|
||||
/* coverity[+retval] */
|
||||
taosFsyncFile(pFile);
|
||||
taosCloseFile(&pFile);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue