未验证 提交 e3f21ae9 编写于 作者: X xinsheng Ren 提交者: GitHub

fix: TD-22674 coverity scan (#20126)

* fix: TD-22674 coverity scan

* fix: compile error

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