From 5289c7bb49411de2ef87fc79796a9120cd1a899b Mon Sep 17 00:00:00 2001 From: peilixia Date: Tue, 27 Jun 2023 16:56:28 +0800 Subject: [PATCH] Enable Int-Sanitizer for c_utils. Issue:I73WM9 Test:NA unittest Signed-off-by: peilixia --- base/BUILD.gn | 8 ++++++ base/src/datetime_ex.cpp | 3 +++ base/src/file_ex.cpp | 26 +++++++++---------- base/src/string_ex.cpp | 16 ++++++++++-- .../unittest/common/utils_datetime_test.cpp | 2 ++ bundle.json | 5 +++- 6 files changed, 44 insertions(+), 16 deletions(-) diff --git a/base/BUILD.gn b/base/BUILD.gn index ae8980d..338aafb 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 12a8145..3ec5508 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 b7e3a6e..897cc41 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 b279544..0082ff7 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 4cc88a5..c10c219 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 19d708d..4366b17 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" -- Gitee