diff --git a/include/id_worker.h b/include/id_worker.h index f2c0cba..7e52329 100644 --- a/include/id_worker.h +++ b/include/id_worker.h @@ -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, int64_t> ids_; std::map, ResourceId> sysDefinedIds_; diff --git a/include/resource_item.h b/include/resource_item.h index 9f1c81e..03fee3a 100644 --- a/include/resource_item.h +++ b/include/resource_item.h @@ -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 keyparams_; ResType type_; diff --git a/include/resource_table.h b/include/resource_table.h index 587c971..4930e15 100644 --- a/include/resource_table.h +++ b/include/resource_table.h @@ -74,12 +74,12 @@ private: void SaveLimitKeyConfigs(const std::map &limitKeyConfigs, std::ostringstream &out) const; void SaveIdSets(const std::map &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> &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> &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> &limitKeys, const std::map> &datas, diff --git a/src/cmd_parser.cpp b/src/cmd_parser.cpp index 44294d2..c266527 100644 --- a/src/cmd_parser.cpp +++ b/src/cmd_parser.cpp @@ -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; diff --git a/src/config_parser.cpp b/src/config_parser.cpp index 75f40ef..c66b537 100644 --- a/src/config_parser.cpp +++ b/src/config_parser.cpp @@ -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; } diff --git a/src/id_defined_parser.cpp b/src/id_defined_parser.cpp index 93e54f1..980c859 100755 --- a/src/id_defined_parser.cpp +++ b/src/id_defined_parser.cpp @@ -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(resourceId.id) & static_cast(START_SYS_ID)) == START_SYS_ID && + !ResourceUtil::IsValidName(resourceId.name)) { cerr << "Error: id_defined.json."<< endl; return false; } diff --git a/src/id_worker.cpp b/src/id_worker.cpp index f665c4d..0fcc02f 100644 --- a/src/id_worker.cpp +++ b/src/id_worker.cpp @@ -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; } diff --git a/src/json_compiler.cpp b/src/json_compiler.cpp index 6b72a0e..67026f6 100644 --- a/src/json_compiler.cpp +++ b/src/json_compiler.cpp @@ -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; }); diff --git a/src/key_parser.cpp b/src/key_parser.cpp index 1fdd73e..182471c 100644 --- a/src/key_parser.cpp +++ b/src/key_parser.cpp @@ -282,7 +282,7 @@ void KeyParser::PushMccMnc(const string &folderName, KeyType type, vector(atoi(folderName.substr(MCC_MNC_KEY_LENGHT).c_str())); keyparams.push_back(keyParam); } diff --git a/src/resource_append.cpp b/src/resource_append.cpp index f56bb0c..fcc3a7a 100644 --- a/src/resource_append.cpp +++ b/src/resource_append.cpp @@ -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(&size), sizeof(int32_t)); out.write(reinterpret_cast(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(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(value); keyParams.push_back(keyParam); } if (limitKeyStr != "base" && !limitKeyStr.empty() && !SelectCompileParse::IsSelectCompile(keyParams)) { diff --git a/src/resource_pack.cpp b/src/resource_pack.cpp index 1d21e7d..bb1549b 100644 --- a/src/resource_pack.cpp +++ b/src/resource_pack.cpp @@ -471,7 +471,11 @@ uint32_t ResourcePack::HandleLabel(vector &items, ConfigParser &co idName = it.GetName() + "_entry"; it.SetName(idName); string data(reinterpret_cast(it.GetData())); - if (!it.SetData(reinterpret_cast(data.c_str()), it.GetDataLength()-1)) { + if (it.GetDataLength() - 1 < 0) { + return RESTOOL_ERROR; + } + if (!it.SetData(reinterpret_cast(data.c_str()), + static_cast(it.GetDataLength() - 1))) { return RESTOOL_ERROR; } if (nextId <= 0) { diff --git a/src/resource_table.cpp b/src/resource_table.cpp index d10b8e7..03cf69c 100644 --- a/src/resource_table.cpp +++ b/src/resource_table.cpp @@ -115,7 +115,7 @@ uint32_t ResourceTable::LoadResTable(const string path, map> { 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> 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> 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 &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> &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> & } bool ResourceTable::ReadIdTables(std::ifstream &in, std::map> &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 length) { diff --git a/src/resource_util.cpp b/src/resource_util.cpp index 2ede156..6db039d 100644 --- a/src/resource_util.cpp +++ b/src/resource_util.cpp @@ -178,7 +178,7 @@ vector 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) {