From 9a081a9c7f9909ccea4a9f232c59f1acc1058bb7 Mon Sep 17 00:00:00 2001 From: huangyuchen Date: Tue, 16 May 2023 17:39:01 +0800 Subject: [PATCH] Enable Int-Sanitizer for shared library version of c_utils. Add overflow check for `TrimStr()`. Issue: I73WM9 Test: UT Signed-off-by: huangyuchen Change-Id: I2fe8090773513898d7b8f2509c7e2c4920033081 --- base/BUILD.gn | 13 +++++++++++++ base/src/string_ex.cpp | 12 ++++++++++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/base/BUILD.gn b/base/BUILD.gn index 452d3a6..6a57902 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_debug_intsan = false } config("utils_config") { @@ -144,6 +145,18 @@ ohos_shared_library("utils") { } else { sources = sources_utils configs = [ ":utils_coverage_config" ] + + if (c_utils_debug_intsan) { + sanitize = { + integer_overflow = true + debug = true + } + } else { + sanitize = { + integer_overflow = true + } + } + if (c_utils_debug_refbase) { configs += [ ":debug_refbase" ] if (c_utils_track_all) { diff --git a/base/src/string_ex.cpp b/base/src/string_ex.cpp index b279544..abe32fd 100644 --- a/base/src/string_ex.cpp +++ b/base/src/string_ex.cpp @@ -57,8 +57,16 @@ string ReplaceStr(const string& str, const string& src, const string& dst) string TrimStr(const string& str, const char cTrim /*= ' '*/) { 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); + std::string::size_type lastBound = strTmp.find_last_not_of(cTrim); + if (firstBound != std::string::npos) { + strTmp.erase(0, firstBound); + } + + if (lastBound != std::string::npos && lastBound != strTmp.size()) { + strTmp.erase(lastBound + sizeof(char)); + } + return strTmp; } -- Gitee