安全规范告警

Signed-off-by: fangyunzhong <fangyunzhong2@huawei.com>
This commit is contained in:
fangyunzhong
2024-04-16 17:02:39 +08:00
parent 1d2ee3c7ca
commit 40cb8239ed
13 changed files with 69 additions and 22 deletions
+2 -2
View File
@@ -38,10 +38,10 @@ public:
private:
int64_t GenerateAppId(ResType resType, const std::string &name);
int64_t GenerateSysId(ResType resType, const std::string &name);
int64_t GetMaxId(int64_t startId) const;
uint64_t GetMaxId(uint64_t startId) const;
int64_t GetCurId();
int64_t appId_;
int64_t maxId_;
uint64_t maxId_;
ResourceIdCluster type_;
std::map<std::pair<ResType, std::string>, int64_t> ids_;
std::map<std::pair<ResType, std::string>, ResourceId> sysDefinedIds_;
+1 -1
View File
@@ -48,7 +48,7 @@ private:
void ReleaseData();
void CopyFrom(const ResourceItem &other);
int8_t *data_ = nullptr;
uint32_t dataLen_;
uint32_t dataLen_ = 0;
std::string name_;
std::vector<KeyParam> keyparams_;
ResType type_;
+4 -4
View File
@@ -74,12 +74,12 @@ private:
void SaveLimitKeyConfigs(const std::map<std::string, LimitKeyConfig> &limitKeyConfigs,
std::ostringstream &out) const;
void SaveIdSets(const std::map<std::string, IdSet> &idSets, std::ostringstream &out) const;
bool ReadFileHeader(std::ifstream &in, IndexHeader &indexHeader, int64_t &pos, int64_t length) const;
bool ReadFileHeader(std::ifstream &in, IndexHeader &indexHeader, uint64_t &pos, int64_t length) const;
bool ReadLimitKeys(std::ifstream &in, std::map<int64_t, std::vector<KeyParam>> &limitKeys,
uint32_t count, int64_t &pos, int64_t length) const;
uint32_t count, uint64_t &pos, int64_t length) const;
bool ReadIdTables(std::ifstream &in, std::map<int64_t, std::pair<int64_t, int64_t>> &datas,
uint32_t count, int64_t &pos, int64_t length) const;
bool ReadDataRecordPrepare(std::ifstream &in, RecordItem &record, int64_t &pos, int64_t length) const;
uint32_t count, uint64_t &pos, int64_t length) const;
bool ReadDataRecordPrepare(std::ifstream &in, RecordItem &record, uint64_t &pos, int64_t length) const;
bool ReadDataRecordStart(std::ifstream &in, RecordItem &record,
const std::map<int64_t, std::vector<KeyParam>> &limitKeys,
const std::map<int64_t, std::pair<int64_t, int64_t>> &datas,
+8 -1
View File
@@ -464,7 +464,7 @@ uint32_t PackageParser::ParseFileList(const string& fileListPath)
{
isFileList_ = true;
ResConfigParser resConfigParser;
if (resConfigParser.Init(fileListPath, [this](int c, const string &argValue) -> int32_t {
if (resConfigParser.Init(fileListPath, [this](int c, const string &argValue) -> uint32_t {
return HandleProcess(c, argValue);
}) != RESTOOL_SUCCESS) {
return RESTOOL_ERROR;
@@ -480,6 +480,10 @@ uint32_t PackageParser::ParseCommand(int argc, char *argv[])
opterr = 0;
int c = getopt_long(argc, argv, CMD_PARAMS.c_str(), CMD_OPTS, &optIndex);
if (optIndex != -1) {
// 1 or 2 menas optind offset value
if ((optarg == nullptr && optind - 1 < 0) || (optarg != nullptr && optind - 2 < 0)) {
return RESTOOL_ERROR;
}
string curOpt = (optarg == nullptr) ? argv[optind - 1] : argv[optind - 2];
if (curOpt != ("--" + string(CMD_OPTS[optIndex].name))) {
cerr << "Error: unknown option " << curOpt << endl;
@@ -523,6 +527,9 @@ uint32_t PackageParser::ParseCommand(int argc, char *argv[])
bool PackageParser::IsLongOpt(char *argv[]) const
{
if (optind - 1 < 0) {
return false;
}
for (auto iter : CMD_OPTS) {
if (optopt == iter.val && argv[optind - 1] == ("--" + string(iter.name))) {
return true;
+3
View File
@@ -367,12 +367,14 @@ bool ConfigParser::ParseJsonArrayRef(cJSON *parent, const string &key, cJSON *no
for (cJSON *item = node->child; item; item = item->next) {
if (!cJSON_IsString(item)) {
cerr << "Error: '" << key << "' invalid value." << NEW_LINE_PATH << filePath_ << endl;
cJSON_Delete(array);
return false;
}
string value = item->valuestring;
bool update = false;
if (!GetRefIdFromString(value, update, JSON_ARRAY_IDS.at(key))) {
cerr << "Error: '" << key << "' value " << value << " invalid." << NEW_LINE_PATH << filePath_ << endl;
cJSON_Delete(array);
return false;
}
if (update) {
@@ -380,6 +382,7 @@ bool ConfigParser::ParseJsonArrayRef(cJSON *parent, const string &key, cJSON *no
}
}
cJSON_AddItemToObject(parent, (key + "Id").c_str(), array);
cJSON_Delete(array);
return true;
}
+2 -1
View File
@@ -247,7 +247,8 @@ bool IdDefinedParser::ParseName(const cJSON *name, ResourceId &resourceId)
}
resourceId.name = name->valuestring;
if (type_ == ResourceIdCluster::RES_ID_SYS &&
(resourceId.id & START_SYS_ID) == START_SYS_ID && !ResourceUtil::IsValidName(resourceId.name)) {
(static_cast<uint64_t>(resourceId.id) & static_cast<uint64_t>(START_SYS_ID)) == START_SYS_ID &&
!ResourceUtil::IsValidName(resourceId.name)) {
cerr << "Error: id_defined.json."<< endl;
return false;
}
+2 -2
View File
@@ -182,9 +182,9 @@ int64_t IdWorker::GenerateSysId(ResType resType, const string &name)
return -1;
}
int64_t IdWorker::GetMaxId(int64_t startId) const
uint64_t IdWorker::GetMaxId(uint64_t startId) const
{
int64_t flag = 1;
uint64_t flag = 1;
while ((flag & startId) == 0) {
flag = flag << 1;
}
+10 -1
View File
@@ -177,8 +177,11 @@ bool JsonCompiler::HandleInteger(const cJSON *objectNode, ResourceItem &resource
}
if (cJSON_IsString(valueNode)) {
return PushString(valueNode->valuestring, resourceItem);
} else if (cJSON_IsNumber(valueNode)) {
return PushString(to_string(valueNode->valueint), resourceItem);
} else {
return false;
}
return PushString(to_string(valueNode->valueint), resourceItem);
}
bool JsonCompiler::HandleBoolean(const cJSON *objectNode, ResourceItem &resourceItem) const
@@ -274,6 +277,9 @@ bool JsonCompiler::HandlePlural(const cJSON *objectNode, ResourceItem &resourceI
return false;
}
cJSON *quantityNode = cJSON_GetObjectItem(arrayItem, TAG_QUANTITY.c_str());
if (!quantityNode || !cJSON_IsString(quantityNode)) {
return false;
}
string quantityValue = quantityNode->valuestring;
if (find(attrs.begin(), attrs.end(), quantityValue) != attrs.end()) {
cerr << "Error: Plural '" << resourceItem.GetName() << "' quantity '" << quantityValue;
@@ -283,6 +289,9 @@ bool JsonCompiler::HandlePlural(const cJSON *objectNode, ResourceItem &resourceI
attrs.push_back(quantityValue);
values.push_back(quantityValue);
cJSON *valueNode = cJSON_GetObjectItem(arrayItem, TAG_VALUE.c_str());
if (!valueNode || !cJSON_IsString(valueNode)) {
return false;
}
values.push_back(valueNode->valuestring);
return true;
});
+1 -1
View File
@@ -282,7 +282,7 @@ void KeyParser::PushMccMnc(const string &folderName, KeyType type, vector<KeyPar
{
KeyParam keyParam;
keyParam.keyType = type;
keyParam.value = atoi(folderName.substr(MCC_MNC_KEY_LENGHT).c_str());
keyParam.value = static_cast<uint32_t>(atoi(folderName.substr(MCC_MNC_KEY_LENGHT).c_str()));
keyparams.push_back(keyParam);
}
+6 -2
View File
@@ -357,7 +357,7 @@ bool ResourceAppend::WriteFileInner(ostringstream &outStream, const string &outp
bool ResourceAppend::WriteResourceItem(const ResourceItem &resourceItem, ostringstream &out)
{
int32_t size = resourceItem.GetName().length();
uint32_t size = resourceItem.GetName().length();
out.write(reinterpret_cast<const char *>(&size), sizeof(int32_t));
out.write(reinterpret_cast<const char *>(resourceItem.GetName().c_str()), size);
@@ -540,7 +540,11 @@ bool ResourceAppend::LoadResourceItemFromMem(const char buffer[], int32_t length
for (int i = 0; i < keyParamSize; i++) {
KeyParam keyParam;
keyParam.keyType = static_cast<KeyType>(ParseInt32(buffer, length, offset));
keyParam.value = ParseInt32(buffer, length, offset);
int32_t value = ParseInt32(buffer, length, offset);
if (value == -1) {
return false;
}
keyParam.value = static_cast<uint32_t>(value);
keyParams.push_back(keyParam);
}
if (limitKeyStr != "base" && !limitKeyStr.empty() && !SelectCompileParse::IsSelectCompile(keyParams)) {
+5 -1
View File
@@ -471,7 +471,11 @@ uint32_t ResourcePack::HandleLabel(vector<ResourceItem> &items, ConfigParser &co
idName = it.GetName() + "_entry";
it.SetName(idName);
string data(reinterpret_cast<const char *>(it.GetData()));
if (!it.SetData(reinterpret_cast<const int8_t *>(data.c_str()), it.GetDataLength()-1)) {
if (it.GetDataLength() - 1 < 0) {
return RESTOOL_ERROR;
}
if (!it.SetData(reinterpret_cast<const int8_t *>(data.c_str()),
static_cast<uint32_t>(it.GetDataLength() - 1))) {
return RESTOOL_ERROR;
}
if (nextId <= 0) {
+24 -5
View File
@@ -115,7 +115,7 @@ uint32_t ResourceTable::LoadResTable(const string path, map<int64_t, vector<Reso
}
in.seekg(0, ios::beg);
int64_t pos = 0;
uint64_t pos = 0;
IndexHeader indexHeader;
if (!ReadFileHeader(in, indexHeader, pos, length)) {
in.close();
@@ -150,9 +150,24 @@ uint32_t ResourceTable::CreateIdDefined(const map<int64_t, vector<ResourceItem>>
{
cJSON *root = cJSON_CreateObject();
cJSON *recordArray = cJSON_CreateArray();
if (recordArray == nullptr) {
cerr << "Error: failed to create cJSON object for record array." << endl;
cJSON_Delete(root);
return RESTOOL_ERROR;
}
cJSON_AddItemToObject(root, "record", recordArray);
for (const auto &pairPtr : allResource) {
if (pairPtr.second.empty()) {
cerr << "Error: resource item vector is empty." << endl;
cJSON_Delete(root);
return RESTOOL_ERROR;
}
cJSON *jsonItem = cJSON_CreateObject();
if (jsonItem == nullptr) {
cerr << "Error: failed to create cJSON object for resource item." << endl;
cJSON_Delete(root);
return RESTOOL_ERROR;
}
ResourceItem item = pairPtr.second.front();
ResType resType = item.GetResType();
string type = ResourceUtil::ResTypeToString(resType);
@@ -161,6 +176,8 @@ uint32_t ResourceTable::CreateIdDefined(const map<int64_t, vector<ResourceItem>>
if (type.empty()) {
cerr << "Error : name = " << name << " ,ResType must is";
cerr << ResourceUtil::GetAllRestypeString() << endl;
cJSON_Delete(jsonItem);
cJSON_Delete(root);
return RESTOOL_ERROR;
}
cJSON_AddStringToObject(jsonItem, "type", type.c_str());
@@ -169,8 +186,10 @@ uint32_t ResourceTable::CreateIdDefined(const map<int64_t, vector<ResourceItem>>
cJSON_AddItemToArray(recordArray, jsonItem);
}
if (!ResourceUtil::SaveToJsonFile(idDefinedPath_, root)) {
cJSON_Delete(root);
return RESTOOL_ERROR;
}
cJSON_Delete(root);
return RESTOOL_SUCCESS;
}
@@ -328,7 +347,7 @@ void ResourceTable::SaveIdSets(const map<string, IdSet> &idSets, ostringstream &
}
}
bool ResourceTable::ReadFileHeader(ifstream &in, IndexHeader &indexHeader, int64_t &pos, int64_t length) const
bool ResourceTable::ReadFileHeader(ifstream &in, IndexHeader &indexHeader, uint64_t &pos, int64_t length) const
{
pos += sizeof(indexHeader);
if (pos > length) {
@@ -342,7 +361,7 @@ bool ResourceTable::ReadFileHeader(ifstream &in, IndexHeader &indexHeader, int64
}
bool ResourceTable::ReadLimitKeys(ifstream &in, map<int64_t, vector<KeyParam>> &limitKeys,
uint32_t count, int64_t &pos, int64_t length) const
uint32_t count, uint64_t &pos, int64_t length) const
{
for (uint32_t i = 0; i< count; i++) {
pos = pos + TAG_LEN + INT_TO_BYTES + INT_TO_BYTES;
@@ -378,7 +397,7 @@ bool ResourceTable::ReadLimitKeys(ifstream &in, map<int64_t, vector<KeyParam>> &
}
bool ResourceTable::ReadIdTables(std::ifstream &in, std::map<int64_t, std::pair<int64_t, int64_t>> &datas,
uint32_t count, int64_t &pos, int64_t length) const
uint32_t count, uint64_t &pos, int64_t length) const
{
for (uint32_t i = 0; i< count; i++) {
pos = pos + TAG_LEN + INT_TO_BYTES;
@@ -411,7 +430,7 @@ bool ResourceTable::ReadIdTables(std::ifstream &in, std::map<int64_t, std::pair<
return true;
}
bool ResourceTable::ReadDataRecordPrepare(ifstream &in, RecordItem &record, int64_t &pos, int64_t length) const
bool ResourceTable::ReadDataRecordPrepare(ifstream &in, RecordItem &record, uint64_t &pos, int64_t length) const
{
pos = pos + INT_TO_BYTES;
if (pos > length) {
+1 -1
View File
@@ -178,7 +178,7 @@ vector<string> ResourceUtil::DecomposeStrings(const string &content)
result.clear();
return result;
}
uint16_t size = (content[pos] & 0xff) | ((content[pos + 1] & 0xff) << 8);
uint16_t size = (content[pos] & 0xff) | ((content[pos + 1] & 0xff) << 8); // Move 8 bits to the left
pos += HEAD_LENGTH;
if (pos + size >= length) {