fuzz问题修改

Signed-off-by: zhangdd_ewan <zhangdongdong50@huawei.com>
This commit is contained in:
zhangdd_ewan 2023-12-11 16:00:15 +08:00
parent b9b034c4b0
commit 7b0f01e7ae
10 changed files with 47 additions and 34 deletions

View File

@ -39,6 +39,8 @@ private:
std::vector<MatchedDateTimeInfo> GetMatches(icu::UnicodeString& message);
std::vector<MatchedDateTimeInfo> ClearFind(icu::UnicodeString& message);
std::vector<MatchedDateTimeInfo> PastFind(icu::UnicodeString& message);
void GetMatchedInfo(std::vector<MatchedDateTimeInfo>& matches, MatchedDateTimeInfo match,
icu::UnicodeString& message);
std::string locale;
DateTimeRule* dateTimeRule;

View File

@ -20,6 +20,8 @@
#include <set>
#include <string>
#include <vector>
#include <thread>
#include <mutex>
namespace OHOS {
namespace Global {
@ -49,11 +51,11 @@ private:
void ReadResourceList();
std::string GetLanguageFromFileName(const std::string& fileName);
std::set<std::string> supportedRegions; // Indicates which regions support name replacement using taboo data.
std::set<std::string> supportedLanguages; // Indicates which languages support name replacement using taboo data.
static std::set<std::string> supportedRegions; // Indicates which regions support name replacement using taboo data.
static std::set<std::string> supportedLanguages; // Indicates which languages support name replacement using taboo data.
// cache the name replacement taboo data of different locale.
std::map<std::string, std::map<std::string, std::string>> localeTabooData;
std::map<std::string, std::string> resources; // Indicates which locales are supported to find taboo data.
static std::map<std::string, std::string> RESOURCES; // Indicates which locales are supported to find taboo data.
std::string tabooDataPath = "";
std::string tabooConfigFileName = "taboo-config.xml";
@ -64,6 +66,7 @@ private:
std::string regionKey = "region_"; // start part of region name replacement data key.
std::string languageKey = "language_"; // start part of language name replacement data key.
bool isTabooDataExist = false;
static std::mutex tabooMutex;
static const char* ROOT_TAG;
static const char* ITEM_TAG;

View File

@ -114,30 +114,30 @@ std::vector<MatchedDateTimeInfo> DateRuleInit::GetMatches(icu::UnicodeString& me
std::vector<MatchedDateTimeInfo> matches;
for (auto& detect : universalAndLocaleRules) {
std::vector<MatchedDateTimeInfo> tempMatches = detect.Match(message);
matches.insert(matches.end(), tempMatches.begin(), tempMatches.end());
}
auto matchIterator = matches.begin();
std::vector<MatchedDateTimeInfo> subResult;
while (matchIterator != matches.end()) {
MatchedDateTimeInfo match = (*matchIterator);
// splitting results based on subRules.
if (subDetectsMap.find(match.GetRegex()) != subDetectsMap.end()) {
RulesEngine subRulesEngine = subDetectsMap[match.GetRegex()];
icu::UnicodeString subMessage = message.tempSubString(match.GetBegin(), match.GetEnd() - match.GetBegin());
std::vector<MatchedDateTimeInfo> subMatches = subRulesEngine.Match(subMessage);
for (auto& subMatch : subMatches) {
subMatch.SetBegin(subMatch.GetBegin() + match.GetBegin());
subMatch.SetEnd(subMatch.GetEnd() + match.GetBegin());
}
subResult.insert(subResult.end(), subMatches.begin(), subMatches.end());
matches.erase(matchIterator);
} else {
matchIterator++;
for (MatchedDateTimeInfo& match : tempMatches) {
GetMatchedInfo(matches, match, message);
}
}
matches.insert(matches.end(), subResult.begin(), subResult.end());
return matches;
}
void DateRuleInit::GetMatchedInfo(std::vector<MatchedDateTimeInfo>& matches, MatchedDateTimeInfo match,
icu::UnicodeString& message)
{
// splitting results based on subRules.
if (subDetectsMap.find(match.GetRegex()) != subDetectsMap.end()) {
RulesEngine subRulesEngine = subDetectsMap[match.GetRegex()];
icu::UnicodeString subMessage = message.tempSubString(match.GetBegin(), match.GetEnd() - match.GetBegin());
std::vector<MatchedDateTimeInfo> subMatches = subRulesEngine.Match(subMessage);
for (auto& subMatch : subMatches) {
subMatch.SetBegin(subMatch.GetBegin() + match.GetBegin());
subMatch.SetEnd(subMatch.GetEnd() + match.GetBegin());
}
matches.insert(matches.end(), subMatches.begin(), subMatches.end());
} else {
matches.push_back(match);
}
}
} // namespace I18n
} // namespace Global
} // namespace OHOS

View File

@ -32,6 +32,10 @@ const char* Taboo::ROOT_TAG = "taboo";
const char* Taboo::ITEM_TAG = "item";
const char* Taboo::NAME_TAG = "name";
const char* Taboo::VALUE_TAG = "value";
std::set<std::string> Taboo::supportedRegions;
std::set<std::string> Taboo::supportedLanguages;
std::map<std::string, std::string> Taboo::RESOURCES = {};
std::mutex Taboo::tabooMutex;
Taboo::Taboo()
{
@ -45,9 +49,12 @@ Taboo::Taboo(const std::string& path) : tabooDataPath(path)
struct stat s;
isTabooDataExist = stat(tabooConfigFilePath.c_str(), &s) == 0;
if (isTabooDataExist) {
// parse taboo-config.xml to obtain supported regions and languages for name replacement.
ParseTabooData(tabooConfigFilePath, DataFileType::CONFIG_FILE);
ReadResourceList();
std::lock_guard<std::mutex> tabooLock(tabooMutex);
if (RESOURCES.size() == 0) {
// parse taboo-config.xml to obtain supported regions and languages for name replacement.
ParseTabooData(tabooConfigFilePath, DataFileType::CONFIG_FILE);
ReadResourceList();
}
}
}
@ -167,6 +174,7 @@ void Taboo::ParseTabooData(const std::string& path, DataFileType fileType, const
xmlFree(value);
cur = cur->next;
}
xmlFreeDoc(doc);
}
void Taboo::ProcessTabooConfigData(const std::string& name, const std::string& value)
@ -217,7 +225,7 @@ std::tuple<std::string, std::string> Taboo::LanguageFallBack(const std::string&
std::string fileName;
int32_t bestScore = -1;
for (auto it = resources.begin(); it != resources.end(); ++it) {
for (auto it = RESOURCES.begin(); it != RESOURCES.end(); ++it) {
std::string resLanguage = it->first;
int32_t score = LocaleCompare::Compare(language, resLanguage);
if (score > bestScore) {
@ -245,7 +253,7 @@ void Taboo::ReadResourceList()
if (s.st_mode & S_IFDIR) {
std::string fileName = path.substr(tabooDataPath.length());
std::string language = GetLanguageFromFileName(fileName);
resources[language] = fileName;
RESOURCES[language] = fileName;
}
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) Huawei Technologies Co., Ltd. 2023-2024. All rights reserved.
* Copyright (c) 2023 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except", "in compliance with the License.
* You may obtain a copy of the License at

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) Huawei Technologies Co., Ltd. 2023-2024. All rights reserved.
* Copyright (c) 2023 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except", "in compliance with the License.
* You may obtain a copy of the License at

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) Huawei Technologies Co., Ltd. 2023-2024. All rights reserved.
* Copyright (c) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except", "in compliance with the License.
* You may obtain a copy of the License at

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) Huawei Technologies Co., Ltd. 2023-2024. All rights reserved.
* Copyright (c) 2023 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except", "in compliance with the License.
* You may obtain a copy of the License at

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) Huawei Technologies Co., Ltd. 2023-2024. All rights reserved.
* Copyright (c) 2023 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except", "in compliance with the License.
* You may obtain a copy of the License at

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) Huawei Technologies Co., Ltd. 2023-2024. All rights reserved.
* Copyright (c) 2023 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except", "in compliance with the License.
* You may obtain a copy of the License at