diff --git a/base/BUILD.gn b/base/BUILD.gn index ae8980d1c9600b772842d50e19078dace1b74f41..338aafbe5898b0c07cf3576c1c8f00377bcc8e3d 100644 --- a/base/BUILD.gn +++ b/base/BUILD.gn @@ -19,6 +19,7 @@ declare_args() { c_utils_track_all = false c_utils_print_track_at_once = false c_utils_debug_log_enabled = false + c_utils_feature_intsan = true } config("utils_config") { @@ -148,6 +149,13 @@ ohos_shared_library("utils") { } else { sources = sources_utils configs = [ ":utils_coverage_config" ] + + if (c_utils_feature_intsan) { + sanitize = { + integer_overflow = true + } + } + if (c_utils_debug_refbase) { configs += [ ":debug_refbase" ] if (c_utils_track_all) { diff --git a/base/src/datetime_ex.cpp b/base/src/datetime_ex.cpp index 12a814562a9c1fc4e4b4761a271198bae1fd3839..3ec5508108617dc7e3f7c98b6a98253f5b3d2f27 100644 --- a/base/src/datetime_ex.cpp +++ b/base/src/datetime_ex.cpp @@ -46,6 +46,9 @@ int64_t GetSecondsBetween(struct tm inputTm1, struct tm inputTm2) { int64_t second1 = GetSecondsSince1970ToPointTime(inputTm1); int64_t second2 = GetSecondsSince1970ToPointTime(inputTm2); + if (second1 == -1 || second2 == -1) { + return -1; + } return second1 >= second2 ? (second1 - second2) : (second2 - second1); } diff --git a/base/src/file_ex.cpp b/base/src/file_ex.cpp index b7e3a6e1016890e2a946dfb5f564cafba08e067c..897cc412c083f2bd33ebbeedb1a40751ebe004fd 100644 --- a/base/src/file_ex.cpp +++ b/base/src/file_ex.cpp @@ -116,9 +116,9 @@ bool LoadStringFromFile(const string& filePath, string& content) } file.seekg(0, ios::end); - const long fileLength = file.tellg(); + const long long fileLength = file.tellg(); if (fileLength > MAX_FILE_LENGTH) { - UTILS_LOGD("invalid file length(%{public}ld)!", fileLength); + UTILS_LOGD("invalid file length(%{public}lld)!", fileLength); return false; } @@ -137,7 +137,7 @@ string GetFileNameByFd(const int fd) string fdPath = "/proc/self/fd/" + std::to_string(fd); char fileName[PATH_MAX + 1] = {0}; - int ret = readlink(fdPath.c_str(), fileName, PATH_MAX); + ssize_t ret = readlink(fdPath.c_str(), fileName, PATH_MAX); if (ret < 0 || ret > PATH_MAX) { UTILS_LOGD("Get fileName failed, ret is: %{public}d!", ret); return string(); @@ -168,9 +168,9 @@ bool LoadStringFromFd(int fd, string& content) return false; } - const long fileLength = lseek(fd, 0, SEEK_END); + const off_t fileLength = lseek(fd, 0, SEEK_END); if (fileLength > MAX_FILE_LENGTH) { - UTILS_LOGE("invalid file length(%{public}ld)!", fileLength); + UTILS_LOGE("invalid file length(%{public}jd)!", static_cast(fileLength)); return false; } @@ -184,16 +184,16 @@ bool LoadStringFromFd(int fd, string& content) } content.resize(fileLength); - int loc = lseek(fd, 0, SEEK_SET); + off_t loc = lseek(fd, 0, SEEK_SET); if (loc == -1) { UTILS_LOGE("lseek file to begin failed!"); return false; } - const long len = read(fd, content.data(), fileLength); + const ssize_t len = read(fd, content.data(), fileLength); if (len != fileLength) { - UTILS_LOGE("the length read from file is not equal to fileLength!len:%{public}ld,fileLen:%{public}ld", - len, fileLength); + UTILS_LOGE("the length read from file is not equal to fileLength!len:%{public}zd,fileLen:%{public}jd", + len, static_cast(fileLength)); return false; } @@ -240,14 +240,14 @@ bool SaveStringToFd(int fd, const std::string& content) return true; } - const long len = write(fd, content.c_str(), content.length()); + const ssize_t len = write(fd, content.c_str(), content.length()); if (len < 0) { UTILS_LOGE("write file failed!errno:%{public}d, err:%{public}s", errno, strerror(errno)); return false; } if (static_cast(len) != content.length()) { - UTILS_LOGE("the length write to file is not equal to fileLength!len:%{public}ld, fileLen:%{public}zu", + UTILS_LOGE("the length write to file is not equal to fileLength!len:%{public}zd, fileLen:%{public}zu", len, content.length()); return false; } @@ -301,9 +301,9 @@ bool LoadBufferFromFile(const string& filePath, vector& content) } file.seekg(0, std::ios::end); - const long fileLength = file.tellg(); + const long long fileLength = file.tellg(); if (fileLength > MAX_FILE_LENGTH) { - UTILS_LOGD("invalid file length(%{public}ld)!", fileLength); + UTILS_LOGD("invalid file length(%{public}lld)!", fileLength); return false; } diff --git a/base/src/string_ex.cpp b/base/src/string_ex.cpp index b279544be72336255a93c0006acb6f2bebeed0f8..0082ff75420c97f1c88826a7a6052247e1a4bd9e 100644 --- a/base/src/string_ex.cpp +++ b/base/src/string_ex.cpp @@ -56,9 +56,21 @@ string ReplaceStr(const string& str, const string& src, const string& dst) string TrimStr(const string& str, const char cTrim /*= ' '*/) { + if (str.size() == 1 && str[0] == cTrim) { + return string{}; + } + string strTmp = str; - strTmp.erase(0, strTmp.find_first_not_of(cTrim)); - strTmp.erase(strTmp.find_last_not_of(cTrim) + sizeof(char)); + std::string::size_type firstBound = strTmp.find_first_not_of(cTrim); + if (firstBound != std::string::npos) { + strTmp.erase(0, firstBound); + } + + std::string::size_type lastBound = strTmp.find_last_not_of(cTrim); + if (lastBound != std::string::npos && lastBound != strTmp.size() - 1) { + strTmp.erase(lastBound + sizeof(char)); + } + return strTmp; } diff --git a/base/test/unittest/common/utils_datetime_test.cpp b/base/test/unittest/common/utils_datetime_test.cpp index 4cc88a53daea22ea66cc9e19be914824239a3fdd..c10c2194b25cbf17a1004aa738918abb570e0497 100644 --- a/base/test/unittest/common/utils_datetime_test.cpp +++ b/base/test/unittest/common/utils_datetime_test.cpp @@ -91,6 +91,8 @@ HWTEST_F(UtilsDateTimeTest, testTime001, TestSize.Level0) info.tm_isdst = 0; second2 = GetSecondsSince1970ToPointTime(info); EXPECT_EQ(-1, second2); + int64_t ret2 = GetSecondsBetween(curTime, info); + EXPECT_TRUE(ret2 = -1); } /* diff --git a/bundle.json b/bundle.json index 19d708d1d512dab454ef2b3245484159c7bccbc2..4366b17f3a2b7b01632d8a22924cd58e4428e730 100644 --- a/bundle.json +++ b/bundle.json @@ -16,7 +16,10 @@ "name": "c_utils", "subsystem": "commonlibrary", "adapted_system_type": [ "standard" ], - "features":[ "c_utils_feature_coverage = false" ], + "features":[ + "c_utils_feature_coverage = false", + "c_utils_feature_intsan = true" + ], "deps": { "components": [ "hilog"