From f0f6787b74403487e74ad4c8502f4a12a2d55667 Mon Sep 17 00:00:00 2001 From: dapan1121 Date: Wed, 24 Jul 2024 15:00:23 +0800 Subject: [PATCH] fix: os return code validation --- include/os/osFile.h | 2 - include/os/osLocale.h | 2 +- include/os/osSignal.h | 8 +-- include/os/osString.h | 2 +- source/common/src/ttszip.c | 4 +- source/os/src/osFile.c | 139 ++++++++++++++++++++++++------------ source/os/src/osLocale.c | 32 +++++---- source/os/src/osMemory.c | 7 +- source/os/src/osSemaphore.c | 113 ++++++++++++++++++++--------- source/os/src/osSignal.c | 43 +++++++++-- source/os/src/osString.c | 110 +++++++++++++++++++++------- source/os/src/osTimezone.c | 2 +- 12 files changed, 319 insertions(+), 145 deletions(-) diff --git a/include/os/osFile.h b/include/os/osFile.h index 4c56244278..8bacb1bf7c 100644 --- a/include/os/osFile.h +++ b/include/os/osFile.h @@ -114,8 +114,6 @@ int64_t taosFSendFile(TdFilePtr pFileOut, TdFilePtr pFileIn, int64_t *offset, in bool taosValidFile(TdFilePtr pFile); -int32_t taosGetErrorFile(TdFilePtr pFile); - int32_t taosCompressFile(char *srcFileName, char *destFileName); int32_t taosSetFileHandlesLimit(); diff --git a/include/os/osLocale.h b/include/os/osLocale.h index 19627f1c73..5f2a1c35de 100644 --- a/include/os/osLocale.h +++ b/include/os/osLocale.h @@ -30,7 +30,7 @@ extern "C" { char *taosCharsetReplace(char *charsetstr); void taosGetSystemLocale(char *outLocale, char *outCharset); -void taosSetSystemLocale(const char *inLocale, const char *inCharSet); +int32_t taosSetSystemLocale(const char *inLocale, const char *inCharSet); #ifdef __cplusplus } diff --git a/include/os/osSignal.h b/include/os/osSignal.h index 3917c6cefc..8b8fb67cff 100644 --- a/include/os/osSignal.h +++ b/include/os/osSignal.h @@ -49,11 +49,11 @@ typedef BOOL (*FSignalHandler)(DWORD fdwCtrlType); #else typedef void (*FSignalHandler)(int32_t signum, void *sigInfo, void *context); #endif -void taosSetSignal(int32_t signum, FSignalHandler sigfp); -void taosIgnSignal(int32_t signum); -void taosDflSignal(int32_t signum); +int32_t taosSetSignal(int32_t signum, FSignalHandler sigfp); +int32_t taosIgnSignal(int32_t signum); +int32_t taosDflSignal(int32_t signum); -void taosKillChildOnParentStopped(); +int32_t taosKillChildOnParentStopped(); #ifdef __cplusplus } diff --git a/include/os/osString.h b/include/os/osString.h index 05492763c2..80755de031 100644 --- a/include/os/osString.h +++ b/include/os/osString.h @@ -75,7 +75,7 @@ int32_t taosUcs4ToMbs(TdUcs4 *ucs4, int32_t ucs4_max_len, char *mbs); int32_t taosUcs4ToMbsEx(TdUcs4 *ucs4, int32_t ucs4_max_len, char *mbs, iconv_t conv); bool taosMbsToUcs4(const char *mbs, size_t mbs_len, TdUcs4 *ucs4, int32_t ucs4_max_len, int32_t *len); int32_t tasoUcs4Compare(TdUcs4 *f1_ucs4, TdUcs4 *f2_ucs4, int32_t bytes); -TdUcs4 *tasoUcs4Copy(TdUcs4 *target_ucs4, TdUcs4 *source_ucs4, int32_t len_ucs4); +int32_t tasoUcs4Copy(TdUcs4 *target_ucs4, TdUcs4 *source_ucs4, int32_t len_ucs4); bool taosValidateEncodec(const char *encodec); int32_t taosHexEncode(const unsigned char *src, char *dst, int32_t len); int32_t taosHexDecode(const char *src, char *dst, int32_t len); diff --git a/source/common/src/ttszip.c b/source/common/src/ttszip.c index 38659eea44..4cf2100190 100644 --- a/source/common/src/ttszip.c +++ b/source/common/src/ttszip.c @@ -1109,14 +1109,14 @@ int32_t dumpFileBlockByGroupId(STSBuf* pTSBuf, int32_t groupIndex, void* buf, in *numOfBlocks = 0; if (taosLSeekFile(pTSBuf->pFile, pBlockInfo->offset, SEEK_SET) != 0) { - int32_t code = TAOS_SYSTEM_ERROR(taosGetErrorFile(pTSBuf->pFile)); + int32_t code = terrno; // qError("%p: fseek failed: %s", pSql, tstrerror(code)); return code; } size_t s = taosReadFile(pTSBuf->pFile, buf, pBlockInfo->compLen); if (s != pBlockInfo->compLen) { - int32_t code = TAOS_SYSTEM_ERROR(taosGetErrorFile(pTSBuf->pFile)); + int32_t code = terrno; // tscError("%p: fread didn't return expected data: %s", pSql, tstrerror(code)); return code; } diff --git a/source/os/src/osFile.c b/source/os/src/osFile.c index 6503ddd8d0..fe89114f3e 100644 --- a/source/os/src/osFile.c +++ b/source/os/src/osFile.c @@ -1026,11 +1026,12 @@ int32_t taosFtruncateFile(TdFilePtr pFile, int64_t l_size) { int64_t taosFSendFile(TdFilePtr pFileOut, TdFilePtr pFileIn, int64_t *offset, int64_t size) { if (pFileOut == NULL || pFileIn == NULL) { - return 0; + terrno = TSDB_CODE_INVALID_PARA; + return terrno; } - ASSERT(pFileIn->fd >= 0 && pFileOut->fd >= 0); - if (pFileIn->fd < 0 || pFileOut->fd < 0) { - return 0; + if (pFileIn->fd < 0 || pFileOut->fd < 0) { + terrno = TSDB_CODE_INVALID_PARA; + return terrno; } #ifdef WINDOWS @@ -1110,7 +1111,8 @@ int64_t taosFSendFile(TdFilePtr pFileOut, TdFilePtr pFileIn, int64_t *offset, in if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) { continue; } else { - return -1; + terrno = TAOS_SYSTEM_ERROR(errno); + return terrno; } } else if (sentbytes == 0) { return (int64_t)(size - leftbytes); @@ -1123,7 +1125,7 @@ int64_t taosFSendFile(TdFilePtr pFileOut, TdFilePtr pFileIn, int64_t *offset, in #endif } -bool lastErrorIsFileNotExist() { return errno == ENOENT; } +bool lastErrorIsFileNotExist() { return terrno == TAOS_SYSTEM_ERROR(ENOENT); } #endif // WINDOWS @@ -1230,22 +1232,26 @@ int32_t taosCloseFile(TdFilePtr *ppFile) { int64_t taosPReadFile(TdFilePtr pFile, void *buf, int64_t count, int64_t offset) { if (pFile == NULL) { - return 0; + terrno = TSDB_CODE_INVALID_PARA; + return terrno; } int32_t code = 0; #ifdef WINDOWS #if FILE_WITH_LOCK - taosThreadRwlockRdlock(&(pFile->rwlock)); + (void)taosThreadRwlockRdlock(&(pFile->rwlock)); #endif - ASSERT(pFile->hFile != NULL); // Please check if you have closed the file. + if (pFile->hFile == NULL) { #if FILE_WITH_LOCK - taosThreadRwlockUnlock(&(pFile->rwlock)); + (void)taosThreadRwlockUnlock(&(pFile->rwlock)); #endif - return -1; + + terrno = TSDB_CODE_INVALID_PARA; + return terrno; } + DWORD ret = 0; OVERLAPPED ol = {0}; ol.OffsetHigh = (uint32_t)((offset & 0xFFFFFFFF00000000LL) >> 0x20); @@ -1261,13 +1267,13 @@ int64_t taosPReadFile(TdFilePtr pFile, void *buf, int64_t count, int64_t offset) #if FILE_WITH_LOCK (void)taosThreadRwlockRdlock(&(pFile->rwlock)); #endif - ASSERT(pFile->fd >= 0); // Please check if you have closed the file. + if (pFile->fd < 0) { #if FILE_WITH_LOCK (void)taosThreadRwlockUnlock(&(pFile->rwlock)); #endif terrno = TSDB_CODE_INVALID_PARA; - return -1; + return terrno; } int64_t ret = pread(pFile->fd, buf, count, offset); if (-1 == ret) { @@ -1280,12 +1286,13 @@ int64_t taosPReadFile(TdFilePtr pFile, void *buf, int64_t count, int64_t offset) terrno = code; - return ret; + return code; } int32_t taosFsyncFile(TdFilePtr pFile) { if (pFile == NULL) { - return 0; + terrno = TSDB_CODE_INVALID_PARA; + return terrno; } int32_t code = 0; @@ -1298,7 +1305,7 @@ int32_t taosFsyncFile(TdFilePtr pFile) { return terrno; } - return code; + return 0; } #ifdef WINDOWS @@ -1327,7 +1334,7 @@ void taosFprintfFile(TdFilePtr pFile, const char *format, ...) { } va_list ap; va_start(ap, format); - vfprintf(pFile->fp, format, ap); + (void)vfprintf(pFile->fp, format, ap); va_end(ap); } @@ -1347,22 +1354,26 @@ int32_t taosUmaskFile(int32_t maskVal) { #endif } -int32_t taosGetErrorFile(TdFilePtr pFile) { return errno; } int64_t taosGetLineFile(TdFilePtr pFile, char **__restrict ptrBuf) { int64_t ret = -1; + int32_t code = 0; + #if FILE_WITH_LOCK - taosThreadRwlockRdlock(&(pFile->rwlock)); + (void)taosThreadRwlockRdlock(&(pFile->rwlock)); #endif if (pFile == NULL || ptrBuf == NULL) { + terrno = TSDB_CODE_INVALID_PARA; goto END; } if (*ptrBuf != NULL) { taosMemoryFreeClear(*ptrBuf); } - ASSERT(pFile->fp != NULL); + if (pFile->fp == NULL) { + terrno = TSDB_CODE_INVALID_PARA; goto END; } + #ifdef WINDOWS size_t bufferSize = 512; *ptrBuf = taosMemoryMalloc(bufferSize); @@ -1399,35 +1410,50 @@ int64_t taosGetLineFile(TdFilePtr pFile, char **__restrict ptrBuf) { #else size_t len = 0; ret = getline(ptrBuf, &len, pFile->fp); + if (-1 == ret) { + code = TAOS_SYSTEM_ERROR(errno); + } #endif END: + #if FILE_WITH_LOCK - taosThreadRwlockUnlock(&(pFile->rwlock)); + (void)taosThreadRwlockUnlock(&(pFile->rwlock)); #endif + + if (code) { + terrno = code; + } + return ret; } int64_t taosGetsFile(TdFilePtr pFile, int32_t maxSize, char *__restrict buf) { if (pFile == NULL || buf == NULL) { - return -1; + terrno = TSDB_CODE_INVALID_PARA; + return terrno; } - ASSERT(pFile->fp != NULL); + if (pFile->fp == NULL) { - return -1; + terrno = TSDB_CODE_INVALID_PARA; + return terrno; } + if (fgets(buf, maxSize, pFile->fp) == NULL) { - return -1; + return 0; } + return strlen(buf); } int32_t taosEOFFile(TdFilePtr pFile) { if (pFile == NULL) { + terrno = TSDB_CODE_INVALID_PARA; return -1; } ASSERT(pFile->fp != NULL); if (pFile->fp == NULL) { + terrno = TSDB_CODE_INVALID_PARA; return -1; } @@ -1461,14 +1487,17 @@ int32_t taosCompressFile(char *srcFileName, char *destFileName) { int32_t compressSize = 163840; int32_t ret = 0; int32_t len = 0; - char *data = taosMemoryMalloc(compressSize); gzFile dstFp = NULL; - TdFilePtr pSrcFile = NULL; + char *data = taosMemoryMalloc(compressSize); + if (NULL == data) { + return terrno; + } + pSrcFile = taosOpenFile(srcFileName, TD_FILE_READ | TD_FILE_STREAM); if (pSrcFile == NULL) { - ret = -1; + ret = terrno; goto cmp_end; } @@ -1479,39 +1508,45 @@ int32_t taosCompressFile(char *srcFileName, char *destFileName) { int32_t pmode = S_IRWXU | S_IRWXG | S_IRWXO; #endif int fd = open(destFileName, access, pmode); - if (fd < 0) { - ret = -2; + if (-1 == fd) { + terrno = TAOS_SYSTEM_ERROR(errno); + ret = terrno; goto cmp_end; } // Both gzclose() and fclose() will close the associated fd, so they need to have different fds. FileFd gzFd = dup(fd); - if (gzFd < 0) { - ret = -4; + if (-1 == gzFd) { + terrno = TAOS_SYSTEM_ERROR(errno); + ret = terrno; goto cmp_end; } dstFp = gzdopen(gzFd, "wb6f"); if (dstFp == NULL) { - ret = -3; - close(gzFd); + terrno = TAOS_SYSTEM_ERROR(errno); + ret = terrno; + (void)close(gzFd); goto cmp_end; } while (!feof(pSrcFile->fp)) { len = (int32_t)fread(data, 1, compressSize, pSrcFile->fp); - (void)gzwrite(dstFp, data, len); + if (len > 0) { + (void)gzwrite(dstFp, data, len); + } } cmp_end: + if (fd >= 0) { - close(fd); + (void)close(fd); } if (pSrcFile) { - taosCloseFile(&pSrcFile); + (void)taosCloseFile(&pSrcFile); } if (dstFp) { - gzclose(dstFp); + (void)gzclose(dstFp); } taosMemoryFree(data); @@ -1530,23 +1565,31 @@ int32_t taosSetFileHandlesLimit() { int32_t taosLinkFile(char *src, char *dst) { #ifndef WINDOWS - if (link(src, dst) != 0) { - if (errno == EXDEV || errno == ENOTSUP) { - return -1; - } - return errno; + if (-1 == link(src, dst)) { + terrno = TAOS_SYSTEM_ERROR(errno); + return terrno; } #endif return 0; } -FILE *taosOpenCFile(const char *filename, const char *mode) { return fopen(filename, mode); } +FILE *taosOpenCFile(const char *filename, const char *mode) { + FILE* f = fopen(filename, mode); + if (NULL == f) { + terrno = TAOS_SYSTEM_ERROR(errno); + } + return f; +} int taosSeekCFile(FILE *file, int64_t offset, int whence) { #ifdef WINDOWS return _fseeki64(file, offset, whence); #else - return fseeko(file, offset, whence); + int code = fseeko(file, offset, whence); + if (-1 == code) { + terrno = TAOS_SYSTEM_ERROR(errno); + } + return terrno; #endif } @@ -1564,6 +1607,10 @@ int taosSetAutoDelFile(char *path) { #ifdef WINDOWS return SetFileAttributes(path, FILE_ATTRIBUTE_TEMPORARY); #else - return unlink(path); + if (-1 == unlink(path)) { + terrno = TAOS_SYSTEM_ERROR(errno); + return terrno; + } + return 0; #endif } diff --git a/source/os/src/osLocale.c b/source/os/src/osLocale.c index 136b8cf022..c846ca82a3 100644 --- a/source/os/src/osLocale.c +++ b/source/os/src/osLocale.c @@ -75,18 +75,18 @@ char *taosCharsetReplace(char *charsetstr) { * * In case that the setLocale failed to be executed, the right charset needs to be set. */ -void taosSetSystemLocale(const char *inLocale, const char *inCharSet) { - char *locale = setlocale(LC_CTYPE, inLocale); - - // default locale or user specified locale is not valid, abort launch - if (inLocale == NULL || strlen(inLocale) == 0) { - // printf("Invalid locale:%s, please set the valid locale in config file\n", inLocale); - } - +int32_t taosSetSystemLocale(const char *inLocale, const char *inCharSet) {\ if (!taosValidateEncodec(inCharSet)) { - printf("Invalid charset:%s, please set the valid charset in config file\n", inCharSet); - exit(-1); + return terrno; } + + char *locale = setlocale(LC_CTYPE, inLocale); + if (NULL == locale) { + terrno = TSDB_CODE_INVALID_PARA; + return terrno; + } + + return 0; } void taosGetSystemLocale(char *outLocale, char *outCharset) { @@ -168,7 +168,7 @@ void taosGetSystemLocale(char *outLocale, char *outCharset) { locale = setlocale(LC_CTYPE, ""); if (locale == NULL) { // printf("can't get locale from system, set it to en_US.UTF-8 since error:%d:%s", errno, strerror(errno)); - strcpy(outLocale, "en_US.UTF-8"); + (void)strcpy(outLocale, "en_US.UTF-8"); } else { tstrncpy(outLocale, locale, TD_LOCALE_LEN); //printf("locale not configured, set to system default:%s\n", outLocale); @@ -180,12 +180,16 @@ void taosGetSystemLocale(char *outLocale, char *outCharset) { str++; char *revisedCharset = taosCharsetReplace(str); - tstrncpy(outCharset, revisedCharset, TD_LOCALE_LEN); + if (NULL == revisedCharset) { + (void)strcpy(outCharset, "UTF-8"); + } else { + tstrncpy(outCharset, revisedCharset, TD_LOCALE_LEN); - taosMemoryFree(revisedCharset); + taosMemoryFree(revisedCharset); + } // printf("charset not configured, set to system default:%s", outCharset); } else { - strcpy(outCharset, "UTF-8"); + (void)strcpy(outCharset, "UTF-8"); // printf("can't get locale and charset from system, set it to UTF-8"); } diff --git a/source/os/src/osMemory.c b/source/os/src/osMemory.c index 3e49808dad..2c9abe4db5 100644 --- a/source/os/src/osMemory.c +++ b/source/os/src/osMemory.c @@ -342,12 +342,7 @@ char *taosStrdup(const char *ptr) { return (char *)tmp + sizeof(TdMemoryInfo); #else - char* p = tstrdup(ptr); - if (ptr != NULL && NULL == p) { - terrno = TSDB_CODE_OUT_OF_MEMORY; - } - return p; - + return tstrdup(ptr); #endif } diff --git a/source/os/src/osSemaphore.c b/source/os/src/osSemaphore.c index dd962a157f..e959174f11 100644 --- a/source/os/src/osSemaphore.c +++ b/source/os/src/osSemaphore.c @@ -194,18 +194,19 @@ int32_t taosGetAppName(char* name, int32_t* len) { const char* self = "/proc/self/exe"; char path[PATH_MAX] = {0}; - if (readlink(self, path, PATH_MAX) <= 0) { - return -1; + if (-1 == readlink(self, path, PATH_MAX)) { + terrno = TAOS_SYSTEM_ERROR(errno); + return terrno; } path[PATH_MAX - 1] = 0; char* end = strrchr(path, '/'); if (end == NULL) { - return -1; + end = path; + } else { + ++end; } - - ++end; - + tstrncpy(name, end, TSDB_APP_NAME_LEN); if (len != NULL) { @@ -221,14 +222,22 @@ int32_t tsem_timewait(tsem_t* sem, int64_t ms) { struct timespec ts = {0}; if (clock_gettime(CLOCK_REALTIME, &ts) == -1) { - return -1; + terrno = TAOS_SYSTEM_ERROR(errno); + return terrno; } ts.tv_nsec += ms * 1000000; ts.tv_sec += ts.tv_nsec / 1000000000; ts.tv_nsec %= 1000000000; - while ((ret = sem_timedwait(sem, &ts)) == -1 && errno == EINTR) continue; + while ((ret = sem_timedwait(sem, &ts)) == -1 && errno == EINTR) { + continue; + } + + if (-1 == ret) { + terrno = TAOS_SYSTEM_ERROR(errno); + return terrno; + } return ret; } @@ -237,78 +246,113 @@ int32_t tsem_wait(tsem_t* sem) { int ret = 0; do { ret = sem_wait(sem); - } while (ret != 0 && errno == EINTR); + } while (-1 == ret && errno == EINTR); + + if (-1 == ret) { + terrno = TAOS_SYSTEM_ERROR(errno); + return terrno; + } + return ret; } int tsem2_init(tsem2_t* sem, int pshared, unsigned int value) { int ret = taosThreadMutexInit(&sem->mutex, NULL); if (ret != 0) return ret; + ret = taosThreadCondAttrInit(&sem->attr); - if (ret != 0) - { - taosThreadMutexDestroy(&sem->mutex); + if (ret != 0) { + (void)taosThreadMutexDestroy(&sem->mutex); return ret; } + ret = taosThreadCondAttrSetclock(&sem->attr, CLOCK_MONOTONIC); - if (ret != 0) - { - taosThreadMutexDestroy(&sem->mutex); - taosThreadCondAttrDestroy(&sem->attr); + if (ret != 0) { + (void)taosThreadMutexDestroy(&sem->mutex); + (void)taosThreadCondAttrDestroy(&sem->attr); return ret; } + ret = taosThreadCondInit(&sem->cond, &sem->attr); - if (ret != 0) - { - taosThreadMutexDestroy(&sem->mutex); - taosThreadCondAttrDestroy(&sem->attr); + if (ret != 0) { + (void)taosThreadMutexDestroy(&sem->mutex); + (void)taosThreadCondAttrDestroy(&sem->attr); return ret; } sem->count = value; + return 0; } int tsem2_post(tsem2_t *sem) { - taosThreadMutexLock(&sem->mutex); + int32_t code = taosThreadMutexLock(&sem->mutex); + if (code) { + return code; + } + sem->count++; - taosThreadCondSignal(&sem->cond); - taosThreadMutexUnlock(&sem->mutex); + code = taosThreadCondSignal(&sem->cond); + if (code) { + return code; + } + + code = taosThreadMutexUnlock(&sem->mutex); + if (code) { + return code; + } + return 0; } int tsem2_destroy(tsem2_t* sem) { - taosThreadMutexDestroy(&sem->mutex); - taosThreadCondDestroy(&sem->cond); - taosThreadCondAttrDestroy(&sem->attr); + (void)taosThreadMutexDestroy(&sem->mutex); + (void)taosThreadCondDestroy(&sem->cond); + (void)taosThreadCondAttrDestroy(&sem->attr); + return 0; } int32_t tsem2_wait(tsem2_t* sem) { - taosThreadMutexLock(&sem->mutex); + int32_t code = taosThreadMutexLock(&sem->mutex); + if (code) { + return code; + } + while (sem->count <= 0) { int ret = taosThreadCondWait(&sem->cond, &sem->mutex); if (0 == ret) { continue; } else { - taosThreadMutexUnlock(&sem->mutex); + (void)taosThreadMutexUnlock(&sem->mutex); return ret; } } sem->count--; - taosThreadMutexUnlock(&sem->mutex); + + code = taosThreadMutexUnlock(&sem->mutex); + if (code) { + return code; + } + return 0; } int32_t tsem2_timewait(tsem2_t* sem, int64_t ms) { int ret = 0; - taosThreadMutexLock(&sem->mutex); + ret = taosThreadMutexLock(&sem->mutex); + if (ret) { + return ret; + } + if (sem->count <= 0) { struct timespec ts = {0}; if (clock_gettime(CLOCK_MONOTONIC, &ts) == -1) { - taosThreadMutexUnlock(&sem->mutex); - return -1; + ret = TAOS_SYSTEM_ERROR(errno); + (void)taosThreadMutexUnlock(&sem->mutex); + terrno = ret; + return ret; } ts.tv_sec += ms / 1000; @@ -319,14 +363,15 @@ int32_t tsem2_timewait(tsem2_t* sem, int64_t ms) { while (sem->count <= 0) { ret = taosThreadCondTimedWait(&sem->cond, &sem->mutex, &ts); if (ret != 0) { - taosThreadMutexUnlock(&sem->mutex); + (void)taosThreadMutexUnlock(&sem->mutex); return ret; } } } sem->count--; - taosThreadMutexUnlock(&sem->mutex); + + ret = taosThreadMutexUnlock(&sem->mutex); return ret; } diff --git a/source/os/src/osSignal.c b/source/os/src/osSignal.c index cb13523b24..3d5e384718 100644 --- a/source/os/src/osSignal.c +++ b/source/os/src/osSignal.c @@ -56,26 +56,55 @@ void taosKillChildOnParentStopped() {} */ typedef void (*FLinuxSignalHandler)(int32_t signum, siginfo_t *sigInfo, void *context); +typedef void (*sighandler_t)(int); -void taosSetSignal(int32_t signum, FSignalHandler sigfp) { +int32_t taosSetSignal(int32_t signum, FSignalHandler sigfp) { struct sigaction act; - memset(&act, 0, sizeof(act)); + (void)memset(&act, 0, sizeof(act)); #if 1 act.sa_flags = SA_SIGINFO | SA_RESTART; act.sa_sigaction = (FLinuxSignalHandler)sigfp; #else act.sa_handler = sigfp; #endif - sigaction(signum, &act, NULL); + int32_t code = sigaction(signum, &act, NULL); + if (-1 == code) { + terrno = TAOS_SYSTEM_ERROR(errno); + return terrno; + } + + return code; } -void taosIgnSignal(int32_t signum) { signal(signum, SIG_IGN); } +int32_t taosIgnSignal(int32_t signum) { + sighandler_t h = signal(signum, SIG_IGN); + if (SIG_ERR == h) { + terrno = TAOS_SYSTEM_ERROR(errno); + return terrno; + } -void taosDflSignal(int32_t signum) { signal(signum, SIG_DFL); } + return 0; +} -void taosKillChildOnParentStopped() { +int32_t taosDflSignal(int32_t signum) { + sighandler_t h = signal(signum, SIG_DFL); + if (SIG_ERR == h) { + terrno = TAOS_SYSTEM_ERROR(errno); + return terrno; + } + + return 0; +} + +int32_t taosKillChildOnParentStopped() { #ifndef _TD_DARWIN_64 - prctl(PR_SET_PDEATHSIG, SIGKILL); + int32_t code = prctl(PR_SET_PDEATHSIG, SIGKILL); + if (-1 == code) { + terrno = TAOS_SYSTEM_ERROR(errno); + return terrno; + } + + return code; #endif } diff --git a/source/os/src/osString.c b/source/os/src/osString.c index 99a3cdd03d..0356c2e55b 100644 --- a/source/os/src/osString.c +++ b/source/os/src/osString.c @@ -28,7 +28,12 @@ char *tstrdup(const char *str) { #ifdef WINDOWS return _strdup(str); #else - return strdup(str); + char* p = strdup(str); + if (str != NULL && NULL == p) { + terrno = TSDB_CODE_OUT_OF_MEMORY; + } + return p; + #endif } @@ -119,9 +124,15 @@ int32_t tasoUcs4Compare(TdUcs4 *f1_ucs4, TdUcs4 *f2_ucs4, int32_t bytes) { //#endif } -TdUcs4 *tasoUcs4Copy(TdUcs4 *target_ucs4, TdUcs4 *source_ucs4, int32_t len_ucs4) { - ASSERT(taosMemorySize(target_ucs4) >= len_ucs4 * sizeof(TdUcs4)); - return memcpy(target_ucs4, source_ucs4, len_ucs4 * sizeof(TdUcs4)); +int32_t tasoUcs4Copy(TdUcs4 *target_ucs4, TdUcs4 *source_ucs4, int32_t len_ucs4) { + if (taosMemorySize(target_ucs4) < len_ucs4 * sizeof(TdUcs4)) { + terrno = TSDB_CODE_INVALID_PARA; + return terrno; + } + + (void)memcpy(target_ucs4, source_ucs4, len_ucs4 * sizeof(TdUcs4)); + + return TSDB_CODE_SUCCESS; } typedef struct { @@ -141,23 +152,28 @@ int32_t taosConvInit(void) { gConvMaxNum[1 - M2C] = 512; gConv[M2C] = taosMemoryCalloc(gConvMaxNum[M2C], sizeof(SConv)); + if (gConv[M2C] == NULL) { + return terrno; + } + gConv[1 - M2C] = taosMemoryCalloc(gConvMaxNum[1 - M2C], sizeof(SConv)); - if (gConv[M2C] == NULL || gConv[1 - M2C] == NULL) { + if (gConv[1 - M2C] == NULL) { taosMemoryFree(gConv[M2C]); - taosMemoryFree(gConv[1 - M2C]); - return TSDB_CODE_OUT_OF_MEMORY; + return terrno; } for (int32_t i = 0; i < gConvMaxNum[M2C]; ++i) { gConv[M2C][i].conv = iconv_open(DEFAULT_UNICODE_ENCODEC, tsCharset); if ((iconv_t)-1 == gConv[M2C][i].conv || (iconv_t)0 == gConv[M2C][i].conv) { - return TAOS_SYSTEM_ERROR(errno); + terrno = TAOS_SYSTEM_ERROR(errno); + return terrno; } } for (int32_t i = 0; i < gConvMaxNum[1 - M2C]; ++i) { gConv[1 - M2C][i].conv = iconv_open(tsCharset, DEFAULT_UNICODE_ENCODEC); if ((iconv_t)-1 == gConv[1 - M2C][i].conv || (iconv_t)0 == gConv[1 - M2C][i].conv) { - return TAOS_SYSTEM_ERROR(errno); + terrno = TAOS_SYSTEM_ERROR(errno); + return terrno; } } @@ -167,10 +183,10 @@ int32_t taosConvInit(void) { void taosConvDestroy() { int8_t M2C = 0; for (int32_t i = 0; i < gConvMaxNum[M2C]; ++i) { - iconv_close(gConv[M2C][i].conv); + (void)iconv_close(gConv[M2C][i].conv); } for (int32_t i = 0; i < gConvMaxNum[1 - M2C]; ++i) { - iconv_close(gConv[1 - M2C][i].conv); + (void)iconv_close(gConv[1 - M2C][i].conv); } taosMemoryFreeClear(gConv[M2C]); taosMemoryFreeClear(gConv[1 - M2C]); @@ -182,9 +198,17 @@ iconv_t taosAcquireConv(int32_t *idx, ConvType type) { if (gConvMaxNum[type] <= 0) { *idx = -1; if (type == M2C) { - return iconv_open(DEFAULT_UNICODE_ENCODEC, tsCharset); + iconv_t c = iconv_open(DEFAULT_UNICODE_ENCODEC, tsCharset); + if ((iconv_t)-1 == c || (iconv_t)0 == c) { + terrno = TAOS_SYSTEM_ERROR(errno); + return c; + } } else { - return iconv_open(tsCharset, DEFAULT_UNICODE_ENCODEC); + iconv_t c = iconv_open(tsCharset, DEFAULT_UNICODE_ENCODEC); + if ((iconv_t)-1 == c || (iconv_t)0 == c) { + terrno = TAOS_SYSTEM_ERROR(errno); + return c; + } } } @@ -192,7 +216,7 @@ iconv_t taosAcquireConv(int32_t *idx, ConvType type) { int32_t used = atomic_add_fetch_32(&convUsed[type], 1); if (used > gConvMaxNum[type]) { used = atomic_sub_fetch_32(&convUsed[type], 1); - sched_yield(); + (void)sched_yield(); continue; } @@ -218,12 +242,12 @@ iconv_t taosAcquireConv(int32_t *idx, ConvType type) { void taosReleaseConv(int32_t idx, iconv_t conv, ConvType type) { if (idx < 0) { - iconv_close(conv); + (void)iconv_close(conv); return; } atomic_store_8(&gConv[type][idx].inUse, 0); - atomic_sub_fetch_32(&convUsed[type], 1); + (void)atomic_sub_fetch_32(&convUsed[type], 1); } bool taosMbsToUcs4(const char *mbs, size_t mbsLength, TdUcs4 *ucs4, int32_t ucs4_max_len, int32_t *len) { @@ -231,14 +255,21 @@ bool taosMbsToUcs4(const char *mbs, size_t mbsLength, TdUcs4 *ucs4, int32_t ucs4 printf("Nchar cannot be read and written without iconv, please install iconv library and recompile.\n"); return -1; #else - memset(ucs4, 0, ucs4_max_len); + (void)memset(ucs4, 0, ucs4_max_len); int32_t idx = -1; + int32_t code = 0; iconv_t conv = taosAcquireConv(&idx, M2C); + if ((iconv_t)-1 == conv || (iconv_t)0 == conv) { + return false; + } + size_t ucs4_input_len = mbsLength; size_t outLeft = ucs4_max_len; if (iconv(conv, (char **)&mbs, &ucs4_input_len, (char **)&ucs4, &outLeft) == -1) { + code = TAOS_SYSTEM_ERROR(errno); taosReleaseConv(idx, conv, M2C); + terrno = code; return false; } @@ -261,14 +292,23 @@ int32_t taosUcs4ToMbs(TdUcs4 *ucs4, int32_t ucs4_max_len, char *mbs) { #else int32_t idx = -1; + int32_t code = 0; iconv_t conv = taosAcquireConv(&idx, C2M); + if ((iconv_t)-1 == conv || (iconv_t)0 == conv) { + return false; + } + size_t ucs4_input_len = ucs4_max_len; size_t outLen = ucs4_max_len; if (iconv(conv, (char **)&ucs4, &ucs4_input_len, &mbs, &outLen) == -1) { + code = TAOS_SYSTEM_ERROR(errno); taosReleaseConv(idx, conv, C2M); - return -1; + terrno = code; + return code; } + taosReleaseConv(idx, conv, C2M); + return (int32_t)(ucs4_max_len - outLen); #endif } @@ -282,8 +322,10 @@ int32_t taosUcs4ToMbsEx(TdUcs4 *ucs4, int32_t ucs4_max_len, char *mbs, iconv_t c size_t ucs4_input_len = ucs4_max_len; size_t outLen = ucs4_max_len; if (iconv(conv, (char **)&ucs4, &ucs4_input_len, &mbs, &outLen) == -1) { - return -1; + terrno = TAOS_SYSTEM_ERROR(errno); + return terrno; } + return (int32_t)(ucs4_max_len - outLen); #endif } @@ -295,10 +337,11 @@ bool taosValidateEncodec(const char *encodec) { #else iconv_t cd = iconv_open(encodec, DEFAULT_UNICODE_ENCODEC); if (cd == (iconv_t)(-1)) { + terrno = TAOS_SYSTEM_ERROR(errno); return false; } - iconv_close(cd); + (void)iconv_close(cd); return true; #endif } @@ -323,11 +366,12 @@ int32_t taosUcs4len(TdUcs4 *ucs4) { // dst buffer size should be at least 2*len + 1 int32_t taosHexEncode(const unsigned char *src, char *dst, int32_t len) { if (!dst) { - return -1; + terrno = TSDB_CODE_INVALID_PARA; + return terrno; } for (int32_t i = 0; i < len; ++i) { - sprintf(dst + i * 2, "%02x", src[i]); + (void)sprintf(dst + i * 2, "%02x", src[i]); } return 0; @@ -335,7 +379,8 @@ int32_t taosHexEncode(const unsigned char *src, char *dst, int32_t len) { int32_t taosHexDecode(const char *src, char *dst, int32_t len) { if (!dst) { - return -1; + terrno = TSDB_CODE_INVALID_PARA; + return terrno; } uint8_t hn, ln, out; @@ -344,7 +389,7 @@ int32_t taosHexDecode(const char *src, char *dst, int32_t len) { ln = src[i + 1] > '9' ? src[i + 1] - 'a' + 10 : src[i + 1] - '0'; out = (hn << 4) | ln; - memcpy(dst + j, &out, 1); + (void)memcpy(dst + j, &out, 1); } return 0; @@ -518,11 +563,17 @@ int32_t taosHex2Ascii(const char *z, uint32_t n, void **data, uint32_t *size) { z += HEX_PREFIX_LEN; *size = n / HEX_PREFIX_LEN; if (*size == 0) { - if (!(*data = taosStrdup(""))) return -1; + if (!(*data = taosStrdup(""))) { + return terrno; + } return 0; } + uint8_t *tmp = (uint8_t *)taosMemoryCalloc(*size, 1); - if (tmp == NULL) return -1; + if (tmp == NULL) { + return terrno; + } + int8_t num = 0; uint8_t *byte = tmp + *size - 1; @@ -542,6 +593,7 @@ int32_t taosHex2Ascii(const char *z, uint32_t n, void **data, uint32_t *size) { } } *data = tmp; + return 0; } @@ -614,7 +666,10 @@ static char valueOf(uint8_t symbol) { int32_t taosAscii2Hex(const char *z, uint32_t n, void **data, uint32_t *size) { *size = n * 2 + HEX_PREFIX_LEN; uint8_t *tmp = (uint8_t *)taosMemoryCalloc(*size + 1, 1); - if (tmp == NULL) return -1; + if (tmp == NULL) { + return terrno; + } + *data = tmp; *(tmp++) = '\\'; *(tmp++) = 'x'; @@ -623,5 +678,6 @@ int32_t taosAscii2Hex(const char *z, uint32_t n, void **data, uint32_t *size) { tmp[i * 2] = valueOf(val >> 4); tmp[i * 2 + 1] = valueOf(val & 0x0F); } + return 0; } diff --git a/source/os/src/osTimezone.c b/source/os/src/osTimezone.c index 8780fb2bc2..ef45b773e8 100644 --- a/source/os/src/osTimezone.c +++ b/source/os/src/osTimezone.c @@ -945,7 +945,7 @@ void taosGetSystemTimezone(char *outTimezoneStr, enum TdTimezone *tsTimezone) { char buf[68] = {0}; if (pFile != NULL) { int len = taosReadFile(pFile, buf, 64); - if (len < 64 && taosGetErrorFile(pFile)) { + if (len < 0) { (void)taosCloseFile(&pFile); (void)printf("read /etc/timezone error, reason:%s", strerror(errno)); return;