From 3b6516eae63458c7ae7c2350b770c62412f7114b Mon Sep 17 00:00:00 2001 From: hkddd <1540123646@qq.com> Date: Thu, 29 Jun 2023 16:27:57 +0800 Subject: [PATCH] Added WildcardToRegex function to prevent crash --- services/hilogd/log_buffer.cpp | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/services/hilogd/log_buffer.cpp b/services/hilogd/log_buffer.cpp index d8d0be5..1875c8a 100644 --- a/services/hilogd/log_buffer.cpp +++ b/services/hilogd/log_buffer.cpp @@ -18,6 +18,7 @@ #include #include #include +#include #include #include @@ -126,6 +127,27 @@ size_t HilogBuffer::Insert(const HilogMsg& msg) return elemSize; } +// Replace wildcard with regex +static std::string WildcardToRegex(const std::string& wildcard) +{ + // Original and Replacement char array + const static char* WILDCARDS = "*?[]+.^&"; + const static std::string REPLACEMENT_S[] = {".*", ".", "\\[", "\\]", "\\+", "\\.", "\\^", "\\&"}; + // Modify every wildcard to regex + std::string result = ""; + for (char c : wildcard) { + // strchr matches wildcard and char + if (std::strchr(WILDCARDS, c) != nullptr) { + size_t index = std::strchr(WILDCARDS, c) - WILDCARDS; + result += REPLACEMENT_S[index]; + } + else { + result += c; + } + } + return result; +} + static bool LogMatchFilter(const LogFilter& filter, const HilogData& logData) { // types & levels match @@ -179,7 +201,9 @@ static bool LogMatchFilter(const LogFilter& filter, const HilogData& logData) } // regular expression match if (filter.regex[0] != 0) { - std::regex regExpress(filter.regex); + // Added a WildcardToRegex function for invalid regex. + std::string wildcardRegex = WildcardToRegex(filter.regex); + std::regex regExpress(wildcardRegex); if (std::regex_search(logData.content, regExpress) == false) { return false; } -- Gitee