diff --git a/source/util/src/tlog.c b/source/util/src/tlog.c index 7a22efc887..6d4bf89c0a 100644 --- a/source/util/src/tlog.c +++ b/source/util/src/tlog.c @@ -19,6 +19,7 @@ #include "tconfig.h" #include "tglobal.h" #include "tjson.h" +#include "tutil.h" #define LOG_MAX_LINE_SIZE (10024) #define LOG_MAX_LINE_BUFFER_SIZE (LOG_MAX_LINE_SIZE + 3) @@ -146,7 +147,7 @@ static int32_t taosStartLog() { TdThreadAttr threadAttr; taosThreadAttrInit(&threadAttr); if (taosThreadCreate(&(tsLogObj.logHandle->asyncThread), &threadAttr, taosAsyncOutputLog, tsLogObj.logHandle) != 0) { - return -1; + return terrno; } taosThreadAttrDestroy(&threadAttr); return 0; @@ -176,13 +177,13 @@ int32_t taosInitSlowLog() { } tsLogObj.slowHandle = taosLogBuffNew(LOG_SLOW_BUF_SIZE); - if (tsLogObj.slowHandle == NULL) return -1; + if (tsLogObj.slowHandle == NULL) return terrno; taosUmaskFile(0); tsLogObj.slowHandle->pFile = taosOpenFile(fullName, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_APPEND); if (tsLogObj.slowHandle->pFile == NULL) { printf("\nfailed to open slow log file:%s, reason:%s\n", fullName, strerror(errno)); - return -1; + return TAOS_SYSTEM_ERROR(errno); } return 0; @@ -209,11 +210,11 @@ int32_t taosInitLog(const char *logName, int32_t maxFiles) { taosUpdateDaylight(); tsLogObj.logHandle = taosLogBuffNew(LOG_DEFAULT_BUF_SIZE); - if (tsLogObj.logHandle == NULL) return -1; - if (taosOpenLogFile(fullName, maxFiles) < 0) return -1; + if (tsLogObj.logHandle == NULL) return terrno; + TAOS_CHECK_RETURN(taosOpenLogFile(fullName, maxFiles)); - if (taosInitSlowLog() < 0) return -1; - if (taosStartLog() < 0) return -1; + TAOS_CHECK_RETURN(taosInitSlowLog()); + TAOS_CHECK_RETURN(taosStartLog()); return 0; } @@ -484,7 +485,7 @@ static int32_t taosOpenLogFile(char *fn, int32_t maxFileNum) { if (tsLogObj.logHandle->pFile == NULL) { printf("\nfailed to open log file:%s, reason:%s\n", fileName, strerror(errno)); - return -1; + return TAOS_SYSTEM_ERROR(errno); } taosLockLogFile(tsLogObj.logHandle->pFile); @@ -492,7 +493,7 @@ static int32_t taosOpenLogFile(char *fn, int32_t maxFileNum) { int64_t filesize = 0; if (taosFStatFile(tsLogObj.logHandle->pFile, &filesize, NULL) < 0) { printf("\nfailed to fstat log file:%s, reason:%s\n", fileName, strerror(errno)); - return -1; + return TAOS_SYSTEM_ERROR(errno); } tsLogObj.lines = (int32_t)(filesize / 60); diff --git a/source/util/src/ttimer.c b/source/util/src/ttimer.c index cb01fb2d13..5f6439d8f6 100644 --- a/source/util/src/ttimer.c +++ b/source/util/src/ttimer.c @@ -516,7 +516,7 @@ static int32_t taosTmrModuleInit(void) { tmrCtrls = taosMemoryMalloc(sizeof(tmr_ctrl_t) * tsMaxTmrCtrl); if (tmrCtrls == NULL) { tmrError("failed to allocate memory for timer controllers."); - return -1; + return terrno; } memset(&timerMap, 0, sizeof(timerMap)); @@ -535,14 +535,14 @@ static int32_t taosTmrModuleInit(void) { time_wheel_t* wheel = wheels + i; if (taosThreadMutexInit(&wheel->mutex, NULL) != 0) { tmrError("failed to create the mutex for wheel, reason:%s", strerror(errno)); - return -1; + return terrno; } wheel->nextScanAt = now + wheel->resolution; wheel->index = 0; wheel->slots = (tmr_obj_t**)taosMemoryCalloc(wheel->size, sizeof(tmr_obj_t*)); if (wheel->slots == NULL) { tmrError("failed to allocate wheel slots"); - return -1; + return terrno; } timerMap.size += wheel->size; } @@ -551,7 +551,7 @@ static int32_t taosTmrModuleInit(void) { timerMap.slots = (timer_list_t*)taosMemoryCalloc(timerMap.size, sizeof(timer_list_t)); if (timerMap.slots == NULL) { tmrError("failed to allocate hash map"); - return -1; + return terrno; } tmrQhandle = taosInitScheduler(10000, taosTmrThreads, "tmr", NULL); @@ -570,7 +570,7 @@ static int32_t taosTmrInitModule(void) { if (atomic_load_32(&tmrModuleInit) < 0) { return -1; } - + while (true) { if (0 == atomic_val_compare_exchange_32(&tmrModuleInit, 0, 1)) { atomic_store_32(&tmrModuleInit, taosTmrModuleInit()); @@ -609,7 +609,7 @@ void* taosTmrInit(int32_t maxNumOfTmrs, int32_t resolution, int32_t longest, con } tstrncpy(ctrl->label, label, sizeof(ctrl->label)); - + tmrDebug("%s timer controller is initialized, number of timer controllers: %d.", label, numOfTmrCtrl); return ctrl; } diff --git a/source/util/src/tutil.c b/source/util/src/tutil.c index f201edcb5e..a14ea1a3cd 100644 --- a/source/util/src/tutil.c +++ b/source/util/src/tutil.c @@ -107,6 +107,9 @@ char **strsplit(char *z, const char *delim, int32_t *num) { int32_t size = 4; char **split = taosMemoryMalloc(POINTER_BYTES * size); + if (split == NULL) { + return NULL; + } for (char *p = strsep(&z, delim); p != NULL; p = strsep(&z, delim)) { size_t len = strlen(p); @@ -118,7 +121,10 @@ char **strsplit(char *z, const char *delim, int32_t *num) { if ((*num) >= size) { size = (size << 1); split = taosMemoryRealloc(split, POINTER_BYTES * size); - ASSERTS(NULL != split, "realloc memory failed. size=%d", (int32_t) POINTER_BYTES * size); + if (split == NULL) { + return NULL; + } + ASSERTS(NULL != split, "realloc memory failed. size=%d", (int32_t)POINTER_BYTES * size); } } @@ -145,10 +151,10 @@ char *strnchr(const char *haystack, char needle, int32_t len, bool skipquote) { return NULL; } -TdUcs4* wcsnchr(const TdUcs4* haystack, TdUcs4 needle, size_t len) { - for(int32_t i = 0; i < len; ++i) { +TdUcs4 *wcsnchr(const TdUcs4 *haystack, TdUcs4 needle, size_t len) { + for (int32_t i = 0; i < len; ++i) { if (haystack[i] == needle) { - return (TdUcs4*) &haystack[i]; + return (TdUcs4 *)&haystack[i]; } } @@ -314,6 +320,9 @@ char *strbetween(char *string, char *begin, char *end) { int32_t size = (int32_t)(_end - _begin); if (_end != NULL && size > 0) { result = (char *)taosMemoryCalloc(1, size); + if (result) { + return NULL; + } memcpy(result, _begin + strlen(begin), size - +strlen(begin)); } } @@ -324,13 +333,13 @@ int32_t tintToHex(uint64_t val, char hex[]) { const char hexstr[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; int32_t j = 0, k = 0; - if (val == 0) { + if (val == 0) { hex[j++] = hexstr[0]; return j; } // ignore the initial 0 - while((val & (((uint64_t)0xfL) << ((15 - k) * 4))) == 0) { + while ((val & (((uint64_t)0xfL) << ((15 - k) * 4))) == 0) { k += 1; } @@ -346,10 +355,10 @@ int32_t titoa(uint64_t val, size_t radix, char str[]) { return 0; } - const char* s = "0123456789abcdef"; - char buf[65] = {0}; + const char *s = "0123456789abcdef"; + char buf[65] = {0}; - int32_t i = 0; + int32_t i = 0; uint64_t v = val; do { buf[i++] = s[v % radix]; @@ -357,7 +366,7 @@ int32_t titoa(uint64_t val, size_t radix, char str[]) { } while (v > 0); // reverse order - for(int32_t j = 0; j < i; ++j) { + for (int32_t j = 0; j < i; ++j) { str[j] = buf[i - j - 1]; } @@ -429,8 +438,8 @@ void taosIpPort2String(uint32_t ip, uint16_t port, char *str) { size_t tstrncspn(const char *str, size_t size, const char *reject, size_t rsize) { if (rsize == 0 || rsize == 1) { - char* p = strnchr(str, reject[0], size, false); - return (p == NULL)? size:(p-str); + char *p = strnchr(str, reject[0], size, false); + return (p == NULL) ? size : (p - str); } /* Use multiple small memsets to enable inlining on most targets. */ @@ -441,15 +450,15 @@ size_t tstrncspn(const char *str, size_t size, const char *reject, size_t rsize) memset(p + 192, 0, 64); unsigned char *s = (unsigned char *)reject; - int32_t index = 0; + int32_t index = 0; do { p[s[index++]] = 1; } while (index < rsize); - s = (unsigned char*) str; + s = (unsigned char *)str; int32_t times = size >> 2; if (times == 0) { - for(int32_t i = 0; i < size; ++i) { + for (int32_t i = 0; i < size; ++i) { if (p[s[i]]) { return i; } @@ -460,7 +469,7 @@ size_t tstrncspn(const char *str, size_t size, const char *reject, size_t rsize) index = 0; uint32_t c0, c1, c2, c3; - for(int32_t i = 0; i < times; ++i, index += 4) { + for (int32_t i = 0; i < times; ++i, index += 4) { int32_t j = index; c0 = p[s[j]]; c1 = p[s[j + 1]]; @@ -474,7 +483,7 @@ size_t tstrncspn(const char *str, size_t size, const char *reject, size_t rsize) } int32_t offset = times * 4; - for(int32_t i = offset; i < size; ++i) { + for (int32_t i = offset; i < size; ++i) { if (p[s[i]]) { return i; } @@ -485,8 +494,8 @@ size_t tstrncspn(const char *str, size_t size, const char *reject, size_t rsize) size_t twcsncspn(const TdUcs4 *wcs, size_t size, const TdUcs4 *reject, size_t rsize) { if (rsize == 0 || rsize == 1) { - TdUcs4* p = wcsnchr(wcs, reject[0], size); - return (p == NULL)? size:(p-wcs); + TdUcs4 *p = wcsnchr(wcs, reject[0], size); + return (p == NULL) ? size : (p - wcs); } size_t index = 0; @@ -497,19 +506,17 @@ size_t twcsncspn(const TdUcs4 *wcs, size_t size, const TdUcs4 *reject, size_t rs return index; } -int32_t parseCfgReal(const char* str, double* out) { +int32_t parseCfgReal(const char *str, double *out) { double val; char *endPtr; errno = 0; val = taosStr2Double(str, &endPtr); if (str == endPtr || errno == ERANGE || isnan(val)) { - terrno = TSDB_CODE_INVALID_CFG_VALUE; - return -1; + return terrno = TSDB_CODE_INVALID_CFG_VALUE; } - while(isspace((unsigned char)*endPtr)) endPtr++; + while (isspace((unsigned char)*endPtr)) endPtr++; if (*endPtr != '\0') { - terrno = TSDB_CODE_INVALID_CFG_VALUE; - return -1; + return terrno = TSDB_CODE_INVALID_CFG_VALUE; } *out = val; return TSDB_CODE_SUCCESS;