From 1978c7729a5799e8c375792adadf5e4be92fbcb3 Mon Sep 17 00:00:00 2001 From: hope Date: Wed, 30 Jun 2021 15:29:42 +0800 Subject: [PATCH] 1. move gnu compile flags "-pipe -Wshadow" from root "CMakeLists.txt" to "cmake/define.inc"; and change compile flag "/O3" to "/O2", because "/O3" is not supported by MSVC. 2. fix compile warnings in "src/util/src/tbuffer.c", by using "union" to transform integer with float and double. 3. use "memcpy()" instead of directly type convert in "rc/util/src/tutil.c" to resolve memory alignment problem in ARM platform. 4. reduce other compile warnings. --- CMakeLists.txt | 7 ---- cmake/define.inc | 6 +++- src/os/src/detail/osSysinfo.c | 26 +++++++++----- src/plugins/http/src/httpJson.c | 2 ++ src/util/src/tbuffer.c | 62 ++++++++++++++++++++++++++++----- src/util/src/tconfig.c | 20 ++++++++--- src/util/src/tutil.c | 18 +++++++--- 7 files changed, 108 insertions(+), 33 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6f50aca079..fe2cc6b42e 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -42,13 +42,6 @@ INCLUDE(cmake/env.inc) INCLUDE(cmake/version.inc) INCLUDE(cmake/install.inc) -IF (CMAKE_SYSTEM_NAME MATCHES "Linux") - SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pipe -Wall -Wshadow -Werror") - SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pipe -Wall -Wshadow -Werror") -ENDIF () -MESSAGE(STATUS "CMAKE_C_FLAGS: ${CMAKE_C_FLAGS}") -MESSAGE(STATUS "CMAKE_CXX_FLAGS: ${CMAKE_CXX_FLAGS}") - ADD_SUBDIRECTORY(deps) ADD_SUBDIRECTORY(src) ADD_SUBDIRECTORY(tests) diff --git a/cmake/define.inc b/cmake/define.inc index d6a3f2b915..dc3ea3a0b2 100755 --- a/cmake/define.inc +++ b/cmake/define.inc @@ -178,7 +178,7 @@ IF (TD_WINDOWS) MESSAGE("memory sanitizer detected as false") SET(DEBUG_FLAGS "/Zi /W3 /GL") ENDIF () - SET(RELEASE_FLAGS "/W0 /O3 /GL") + SET(RELEASE_FLAGS "/W0 /O2 /GL") # MSVC only support O2 ENDIF () INCLUDE_DIRECTORIES(${TD_COMMUNITY_DIR}/deps/pthread) @@ -201,6 +201,10 @@ IF (TD_WINDOWS_32) MESSAGE(STATUS "windows32 is defined") ENDIF () +IF (TD_LINUX) + SET(COMMON_FLAGS "${COMMON_FLAGS} -pipe -Wshadow") +ENDIF () + INCLUDE_DIRECTORIES(${TD_COMMUNITY_DIR}/src/inc) INCLUDE_DIRECTORIES(${TD_COMMUNITY_DIR}/src/os/inc) INCLUDE_DIRECTORIES(${TD_COMMUNITY_DIR}/src/util/inc) diff --git a/src/os/src/detail/osSysinfo.c b/src/os/src/detail/osSysinfo.c index 891dccaf97..04b1efe7bf 100644 --- a/src/os/src/detail/osSysinfo.c +++ b/src/os/src/detail/osSysinfo.c @@ -74,13 +74,14 @@ bool taosGetProcMemory(float *memoryUsedMB) { return false; } + ssize_t _bytes = 0; size_t len; char * line = NULL; while (!feof(fp)) { tfree(line); len = 0; - getline(&line, &len, fp); - if (line == NULL) { + _bytes = getline(&line, &len, fp); + if ((_bytes < 0) || (line == NULL)) { break; } if (strstr(line, "VmRSS:") != NULL) { @@ -113,8 +114,8 @@ static bool taosGetSysCpuInfo(SysCpuInfo *cpuInfo) { size_t len; char * line = NULL; - getline(&line, &len, fp); - if (line == NULL) { + ssize_t _bytes = getline(&line, &len, fp); + if ((_bytes < 0) || (line == NULL)) { uError("read file:%s failed", tsSysCpuFile); fclose(fp); return false; @@ -138,8 +139,8 @@ static bool taosGetProcCpuInfo(ProcCpuInfo *cpuInfo) { size_t len = 0; char * line = NULL; - getline(&line, &len, fp); - if (line == NULL) { + ssize_t _bytes = getline(&line, &len, fp); + if ((_bytes < 0) || (line == NULL)) { uError("read file:%s failed", tsProcCpuFile); fclose(fp); return false; @@ -339,6 +340,7 @@ static bool taosGetCardInfo(int64_t *bytes) { return false; } + ssize_t _bytes = 0; size_t len = 2048; char * line = calloc(1, len); @@ -357,7 +359,12 @@ static bool taosGetCardInfo(int64_t *bytes) { int64_t nouse6 = 0; char nouse0[200] = {0}; - getline(&line, &len, fp); + _bytes = getline(&line, &len, fp); + if (_bytes < 0) + { + break; + } + line[len - 1] = 0; if (strstr(line, "lo:") != NULL) { @@ -420,6 +427,7 @@ static bool taosReadProcIO(int64_t *readbyte, int64_t *writebyte) { return false; } + ssize_t _bytes = 0; size_t len; char * line = NULL; char tmp[10]; @@ -428,8 +436,8 @@ static bool taosReadProcIO(int64_t *readbyte, int64_t *writebyte) { while (!feof(fp)) { tfree(line); len = 0; - getline(&line, &len, fp); - if (line == NULL) { + _bytes = getline(&line, &len, fp); + if ((_bytes < 0) || (line == NULL)) { break; } if (strstr(line, "rchar:") != NULL) { diff --git a/src/plugins/http/src/httpJson.c b/src/plugins/http/src/httpJson.c index df4c4fbc63..10300e9367 100644 --- a/src/plugins/http/src/httpJson.c +++ b/src/plugins/http/src/httpJson.c @@ -297,6 +297,7 @@ void httpJsonTimestamp(JsonBuf* buf, int64_t t, int32_t timePrecision) { } default: + fractionLen = 0; assert(false); } @@ -342,6 +343,7 @@ void httpJsonUtcTimestamp(JsonBuf* buf, int64_t t, int32_t timePrecision) { } default: + fractionLen = 0; assert(false); } diff --git a/src/util/src/tbuffer.c b/src/util/src/tbuffer.c index abfa35f42c..c06d1e59bd 100644 --- a/src/util/src/tbuffer.c +++ b/src/util/src/tbuffer.c @@ -18,6 +18,24 @@ #include "exception.h" #include "taoserror.h" +typedef union Un4B { + uint32_t ui; + float f; +} Un4B; +#if __STDC_VERSION__ >= 201112L +static_assert(sizeof(Un4B) == sizeof(uint32_t), "sizeof(Un4B) must equal to sizeof(uint32_t)"); +static_assert(sizeof(Un4B) == sizeof(float), "sizeof(Un4B) must equal to sizeof(float)"); +#endif + +typedef union Un8B { + uint64_t ull; + double d; +} Un8B; +#if __STDC_VERSION__ >= 201112L +static_assert(sizeof(Un8B) == sizeof(uint64_t), "sizeof(Un8B) must equal to sizeof(uint64_t)"); +static_assert(sizeof(Un8B) == sizeof(double), "sizeof(Un8B) must equal to sizeof(double)"); +#endif + //////////////////////////////////////////////////////////////////////////////// // reader functions @@ -175,13 +193,21 @@ uint64_t tbufReadUint64( SBufferReader* buf ) { } float tbufReadFloat( SBufferReader* buf ) { - uint32_t ret = tbufReadUint32( buf ); - return *(float*)( &ret ); + Un4B _un; + tbufReadToBuffer( buf, &_un, sizeof(_un) ); + if( buf->endian ) { + _un.ui = ntohl( _un.ui ); + } + return _un.f; } double tbufReadDouble(SBufferReader* buf) { - uint64_t ret = tbufReadUint64( buf ); - return *(double*)( &ret ); + Un8B _un; + tbufReadToBuffer( buf, &_un, sizeof(_un) ); + if( buf->endian ) { + _un.ull = htobe64( _un.ull ); + } + return _un.d; } //////////////////////////////////////////////////////////////////////////////// @@ -381,17 +407,37 @@ void tbufWriteUint64At( SBufferWriter* buf, size_t pos, uint64_t data ) { } void tbufWriteFloat( SBufferWriter* buf, float data ) { - tbufWriteUint32( buf, *(uint32_t*)(&data) ); + Un4B _un; + _un.f = data; + if( buf->endian ) { + _un.ui = htonl( _un.ui ); + } + tbufWrite( buf, &_un, sizeof(_un) ); } void tbufWriteFloatAt( SBufferWriter* buf, size_t pos, float data ) { - tbufWriteUint32At( buf, pos, *(uint32_t*)(&data) ); + Un4B _un; + _un.f = data; + if( buf->endian ) { + _un.ui = htonl( _un.ui ); + } + tbufWriteAt( buf, pos, &_un, sizeof(_un) ); } void tbufWriteDouble( SBufferWriter* buf, double data ) { - tbufWriteUint64( buf, *(uint64_t*)(&data) ); + Un8B _un; + _un.d = data; + if( buf->endian ) { + _un.ull = htobe64( _un.ull ); + } + tbufWrite( buf, &_un, sizeof(_un) ); } void tbufWriteDoubleAt( SBufferWriter* buf, size_t pos, double data ) { - tbufWriteUint64At( buf, pos, *(uint64_t*)(&data) ); + Un8B _un; + _un.d = data; + if( buf->endian ) { + _un.ull = htobe64( _un.ull ); + } + tbufWriteAt( buf, pos, &_un, sizeof(_un) ); } diff --git a/src/util/src/tconfig.c b/src/util/src/tconfig.c index 442e83bb4f..30d53af4b9 100644 --- a/src/util/src/tconfig.c +++ b/src/util/src/tconfig.c @@ -327,7 +327,8 @@ void taosReadGlobalLogCfg() { printf("\nconfig file:%s not found, all variables are set to default\n", fileName); return; } - + + ssize_t _bytes = 0; size_t len = 1024; line = calloc(1, len); @@ -337,7 +338,12 @@ void taosReadGlobalLogCfg() { option = value = NULL; olen = vlen = 0; - tgetline(&line, &len, fp); + _bytes = tgetline(&line, &len, fp); + if (_bytes < 0) + { + break; + } + line[len - 1] = 0; paGetToken(line, &option, &olen); @@ -373,7 +379,8 @@ bool taosReadGlobalCfg() { return false; } } - + + ssize_t _bytes = 0; size_t len = 1024; line = calloc(1, len); @@ -383,7 +390,12 @@ bool taosReadGlobalCfg() { option = value = value2 = value3 = NULL; olen = vlen = vlen2 = vlen3 = 0; - tgetline(&line, &len, fp); + _bytes = tgetline(&line, &len, fp); + if (_bytes < 0) + { + break; + } + line[len - 1] = 0; paGetToken(line, &option, &olen); diff --git a/src/util/src/tutil.c b/src/util/src/tutil.c index bda52936f9..1a73991ade 100644 --- a/src/util/src/tutil.c +++ b/src/util/src/tutil.c @@ -427,13 +427,23 @@ char *taosIpStr(uint32_t ipInt) { } FORCE_INLINE float taos_align_get_float(const char* pBuf) { - float fv = 0; - *(int32_t*)(&fv) = *(int32_t*)pBuf; +#if __STDC_VERSION__ >= 201112L + static_assert(sizeof(float) == sizeof(uint32_t), "sizeof(float) must equal to sizeof(uint32_t)"); +#else + assert(sizeof(float) == sizeof(uint32_t)); +#endif + float fv = 0; + memcpy(&fv, pBuf, sizeof(fv)); // in ARM, return *((const float*)(pBuf)) may cause problem return fv; } FORCE_INLINE double taos_align_get_double(const char* pBuf) { - double dv = 0; - *(int64_t*)(&dv) = *(int64_t*)pBuf; +#if __STDC_VERSION__ >= 201112L + static_assert(sizeof(double) == sizeof(uint64_t), "sizeof(double) must equal to sizeof(uint64_t)"); +#else + assert(sizeof(double) == sizeof(uint64_t)); +#endif + double dv = 0; + memcpy(&dv, pBuf, sizeof(dv)); // in ARM, return *((const double*)(pBuf)) may cause problem return dv; } -- GitLab