From 0636a65ad9e88dcdd038331f1f4ffad3b7b88b6f Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sun, 8 Dec 2013 12:02:37 -0800 Subject: [PATCH] Use ReadEntireFile() a few more places. This fixes one or two minor memory leaks. --- Core/Dialog/SavedataParam.cpp | 99 +++++++++++++---------------- Core/ELF/ParamSFO.h | 4 ++ Core/FileSystems/MetaFileSystem.cpp | 2 +- Core/FileSystems/MetaFileSystem.h | 2 +- Core/HLE/sceFont.cpp | 84 ++++++++++++------------ Core/MIPS/MIPSAnalyst.cpp | 2 +- Core/PSPLoaders.cpp | 18 ++---- Core/Util/GameManager.cpp | 2 +- Core/Util/PPGeDraw.cpp | 2 +- UI/NativeApp.cpp | 2 +- 10 files changed, 105 insertions(+), 112 deletions(-) diff --git a/Core/Dialog/SavedataParam.cpp b/Core/Dialog/SavedataParam.cpp index 4003860ce7..6a97db9de5 100644 --- a/Core/Dialog/SavedataParam.cpp +++ b/Core/Dialog/SavedataParam.cpp @@ -323,15 +323,11 @@ bool SavedataParam::Save(SceUtilitySavedataParam* param, const std::string &save ParamSFOData sfoFile; std::string sfopath = dirPath+"/" + SFO_FILENAME; PSPFileInfo sfoInfo = pspFileSystem.GetFileInfo(sfopath); - if(sfoInfo.exists) // Read old sfo if exist + if (sfoInfo.exists) // Read old sfo if exist { - u8 *sfoData = new u8[(size_t)sfoInfo.size]; - size_t sfoSize = (size_t)sfoInfo.size; - if(ReadPSPFile(sfopath,&sfoData,sfoSize, NULL)) - { - sfoFile.ReadSFO(sfoData,sfoSize); - delete[] sfoData; - } + std::vector sfoData; + if (pspFileSystem.ReadEntireFile(sfopath, sfoData) >= 0) + sfoFile.ReadSFO(sfoData); } // Update values @@ -566,12 +562,11 @@ void SavedataParam::LoadSFO(SceUtilitySavedataParam *param, const std::string di ParamSFOData sfoFile; std::string sfopath = dirPath+"/" + SFO_FILENAME; PSPFileInfo sfoInfo = pspFileSystem.GetFileInfo(sfopath); - if(sfoInfo.exists) { - // Read sfo - u8 *sfoData = new u8[(size_t)sfoInfo.size]; - size_t sfoSize = (size_t)sfoInfo.size; - if(ReadPSPFile(sfopath,&sfoData,sfoSize, NULL)) { - sfoFile.ReadSFO(sfoData,sfoSize); + if (sfoInfo.exists) { + // Read sfo + std::vector sfoData; + if (pspFileSystem.ReadEntireFile(sfopath, sfoData) >= 0) { + sfoFile.ReadSFO(sfoData); // copy back info in request strncpy(param->sfoParam.title,sfoFile.GetValueString("TITLE").c_str(),128); @@ -579,43 +574,40 @@ void SavedataParam::LoadSFO(SceUtilitySavedataParam *param, const std::string di strncpy(param->sfoParam.detail,sfoFile.GetValueString("SAVEDATA_DETAIL").c_str(),1024); param->sfoParam.parentalLevel = sfoFile.GetValueInt("PARENTAL_LEVEL"); } - delete[] sfoData; } } std::set SavedataParam::getSecureFileNames(std::string dirPath) { - PSPFileInfo sfoFileInfo = pspFileSystem.GetFileInfo(dirPath + "/" + SFO_FILENAME); - std::set secureFileNames; - if(!sfoFileInfo.exists) - return secureFileNames; - - ParamSFOData sfoFile; - size_t sfoSize = (size_t)sfoFileInfo.size; - u8 *sfoData = new u8[sfoSize]; - if (ReadPSPFile(dirPath + "/" + SFO_FILENAME, &sfoData, sfoSize, NULL)){ - sfoFile.ReadSFO(sfoData, sfoSize); - } - delete[] sfoData; - - u32 sfoFileListSize = 0; - char *sfoFileList = (char *)sfoFile.GetValueData("SAVEDATA_FILE_LIST", &sfoFileListSize); - const int FILE_LIST_ITEM_SIZE = 13 + 16 + 3; - const u32 FILE_LIST_COUNT_MAX = 99; - - // Filenames are 13 bytes long at most. Add a NULL so there's no surprises. - char temp[14]; - temp[13] = '\0'; - - for (u32 i = 0; i < FILE_LIST_COUNT_MAX; ++i) { - // Ends at a NULL filename. - if (i * FILE_LIST_ITEM_SIZE >= sfoFileListSize || sfoFileList[i * FILE_LIST_ITEM_SIZE] == '\0') { - break; - } - - strncpy(temp, &sfoFileList[i * FILE_LIST_ITEM_SIZE], 13); - secureFileNames.insert(temp); - } + PSPFileInfo sfoFileInfo = pspFileSystem.GetFileInfo(dirPath + "/" + SFO_FILENAME); + std::set secureFileNames; + if (!sfoFileInfo.exists) return secureFileNames; + + ParamSFOData sfoFile; + std::vector sfoData; + if (pspFileSystem.ReadEntireFile(dirPath + "/" + SFO_FILENAME, sfoData) >= 0) { + sfoFile.ReadSFO(sfoData); + } + + u32 sfoFileListSize = 0; + char *sfoFileList = (char *)sfoFile.GetValueData("SAVEDATA_FILE_LIST", &sfoFileListSize); + const int FILE_LIST_ITEM_SIZE = 13 + 16 + 3; + const u32 FILE_LIST_COUNT_MAX = 99; + + // Filenames are 13 bytes long at most. Add a NULL so there's no surprises. + char temp[14]; + temp[13] = '\0'; + + for (u32 i = 0; i < FILE_LIST_COUNT_MAX; ++i) { + // Ends at a NULL filename. + if (i * FILE_LIST_ITEM_SIZE >= sfoFileListSize || sfoFileList[i * FILE_LIST_ITEM_SIZE] == '\0') { + break; + } + + strncpy(temp, &sfoFileList[i * FILE_LIST_ITEM_SIZE], 13); + secureFileNames.insert(temp); + } + return secureFileNames; } void SavedataParam::LoadFile(const std::string dirPath, const std::string filename, PspUtilitySavedataFileData *fileData) { @@ -1272,16 +1264,15 @@ void SavedataParam::SetFileInfo(SaveFileInfo &saveInfo, PSPFileInfo &info, std:: info2 = pspFileSystem.GetFileInfo(fileDataPath2); if (info2.exists) { - u8 *sfoParam = new u8[(size_t)info2.size]; - ReadPSPFile(fileDataPath2, &sfoParam, info2.size, NULL); + std::vector sfoData; + pspFileSystem.ReadEntireFile(fileDataPath2, sfoData); ParamSFOData sfoFile; - if (sfoFile.ReadSFO(sfoParam,(size_t)info2.size)) + if (sfoFile.ReadSFO(sfoData)) { SetStringFromSFO(sfoFile, "TITLE", saveInfo.title, sizeof(saveInfo.title)); SetStringFromSFO(sfoFile, "SAVEDATA_TITLE", saveInfo.saveTitle, sizeof(saveInfo.saveTitle)); SetStringFromSFO(sfoFile, "SAVEDATA_DETAIL", saveInfo.saveDetail, sizeof(saveInfo.saveDetail)); } - delete [] sfoParam; } } @@ -1501,11 +1492,10 @@ bool SavedataParam::IsSaveEncrypted(SceUtilitySavedataParam* param, const std::s PSPFileInfo sfoInfo = pspFileSystem.GetFileInfo(sfopath); if(sfoInfo.exists) // Read sfo { - u8 *sfoData = new u8[(size_t)sfoInfo.size]; - size_t sfoSize = (size_t)sfoInfo.size; - if(ReadPSPFile(sfopath,&sfoData,sfoSize, NULL)) + std::vector sfoData; + if (pspFileSystem.ReadEntireFile(sfopath, sfoData) >= 0) { - sfoFile.ReadSFO(sfoData,sfoSize); + sfoFile.ReadSFO(sfoData); // save created in PPSSPP and not encrypted has '0' in SAVEDATA_PARAMS u32 tmpDataSize = 0; @@ -1519,7 +1509,6 @@ bool SavedataParam::IsSaveEncrypted(SceUtilitySavedataParam* param, const std::s } } } - delete[] sfoData; } return isCrypted; } diff --git a/Core/ELF/ParamSFO.h b/Core/ELF/ParamSFO.h index 90d2a516e7..dfca87f50b 100644 --- a/Core/ELF/ParamSFO.h +++ b/Core/ELF/ParamSFO.h @@ -37,6 +37,10 @@ public: bool ReadSFO(const u8 *paramsfo, size_t size); bool WriteSFO(u8 **paramsfo, size_t *size); + bool ReadSFO(const std::vector ¶msfo) { + return ReadSFO(¶msfo[0], paramsfo.size()); + } + int GetDataOffset(const u8 *paramsfo, std::string dataName); private: diff --git a/Core/FileSystems/MetaFileSystem.cpp b/Core/FileSystems/MetaFileSystem.cpp index 937b1c7d72..117b543c4f 100644 --- a/Core/FileSystems/MetaFileSystem.cpp +++ b/Core/FileSystems/MetaFileSystem.cpp @@ -517,7 +517,7 @@ size_t MetaFileSystem::SeekFile(u32 handle, s32 position, FileMove type) return 0; } -u32 MetaFileSystem::ReadEntireFile(const std::string &filename, std::string &data) { +u32 MetaFileSystem::ReadEntireFile(const std::string &filename, std::vector &data) { int error = 0; u32 handle = pspFileSystem.OpenWithError(error, filename, FILEACCESS_READ); if (handle == 0) diff --git a/Core/FileSystems/MetaFileSystem.h b/Core/FileSystems/MetaFileSystem.h index e64b47fe5b..9906fbb648 100644 --- a/Core/FileSystems/MetaFileSystem.h +++ b/Core/FileSystems/MetaFileSystem.h @@ -106,7 +106,7 @@ public: // TODO: void IoCtl(...) // Convenience helper - returns < 0 on failure. - u32 ReadEntireFile(const std::string &filename, std::string &data); + u32 ReadEntireFile(const std::string &filename, std::vector &data); void SetStartingDirectory(const std::string &dir) { lock_guard guard(lock); diff --git a/Core/HLE/sceFont.cpp b/Core/HLE/sceFont.cpp index a12a41a621..0f5a71ed6f 100644 --- a/Core/HLE/sceFont.cpp +++ b/Core/HLE/sceFont.cpp @@ -130,30 +130,19 @@ public: } Font(const u8 *data, size_t dataSize) { - pgf_.ReadPtr(data, dataSize); - style_.fontH = (float)pgf_.header.hSize / 64.0f; - style_.fontV = (float)pgf_.header.vSize / 64.0f; - style_.fontHRes = (float)pgf_.header.hResolution / 64.0f; - style_.fontVRes = (float)pgf_.header.vResolution / 64.0f; + Init(data, dataSize); } Font(const u8 *data, size_t dataSize, const FontRegistryEntry &entry) { - pgf_.ReadPtr(data, dataSize); - style_.fontH = entry.hSize / 64.f; - style_.fontV = entry.vSize / 64.f; - style_.fontHRes = entry.hResolution / 64.f; - style_.fontVRes = entry.vResolution / 64.f; - style_.fontWeight = (float)entry.weight; - style_.fontFamily = (u16)entry.familyCode; - style_.fontStyle = (u16)entry.style; - style_.fontStyleSub = (u16)entry.styleSub; - style_.fontLanguage = (u16)entry.languageCode; - style_.fontRegion = (u16)entry.regionCode; - style_.fontCountry = (u16)entry.countryCode; - strncpy(style_.fontName, entry.fontName, sizeof(style_.fontName)); - strncpy(style_.fontFileName, entry.fileName, sizeof(style_.fontFileName)); - style_.fontAttributes = entry.extraAttributes; - style_.fontExpire = entry.expireDate; + Init(data, dataSize, entry); + } + + Font (const std::vector &data) { + Init(&data[0], data.size()); + } + + Font (const std::vector &data, const FontRegistryEntry &entry) { + Init(&data[0], data.size(), entry); } const PGFFontStyle &GetFontStyle() const { return style_; } @@ -210,6 +199,33 @@ public: } private: + void Init(const u8 *data, size_t dataSize) { + pgf_.ReadPtr(data, dataSize); + style_.fontH = (float)pgf_.header.hSize / 64.0f; + style_.fontV = (float)pgf_.header.vSize / 64.0f; + style_.fontHRes = (float)pgf_.header.hResolution / 64.0f; + style_.fontVRes = (float)pgf_.header.vResolution / 64.0f; + } + + void Init(const u8 *data, size_t dataSize, const FontRegistryEntry &entry) { + pgf_.ReadPtr(data, dataSize); + style_.fontH = entry.hSize / 64.f; + style_.fontV = entry.vSize / 64.f; + style_.fontHRes = entry.hResolution / 64.f; + style_.fontVRes = entry.vResolution / 64.f; + style_.fontWeight = (float)entry.weight; + style_.fontFamily = (u16)entry.familyCode; + style_.fontStyle = (u16)entry.style; + style_.fontStyleSub = (u16)entry.styleSub; + style_.fontLanguage = (u16)entry.languageCode; + style_.fontRegion = (u16)entry.regionCode; + style_.fontCountry = (u16)entry.countryCode; + strncpy(style_.fontName, entry.fontName, sizeof(style_.fontName)); + strncpy(style_.fontFileName, entry.fileName, sizeof(style_.fontFileName)); + style_.fontAttributes = entry.extraAttributes; + style_.fontExpire = entry.expireDate; + } + PGF pgf_; PGFFontStyle style_; DISALLOW_COPY_AND_ASSIGN(Font); @@ -499,11 +515,9 @@ void __LoadInternalFonts() { if (info.exists) { INFO_LOG(SCEFONT, "Loading font %s (%i bytes)", fontFilename.c_str(), (int)info.size); - u8 *buffer = new u8[(size_t)info.size]; - u32 handle = pspFileSystem.OpenFile(fontFilename, FILEACCESS_READ); - if (!handle) { + std::vector buffer; + if (pspFileSystem.ReadEntireFile(fontFilename, buffer) < 0) { ERROR_LOG(SCEFONT, "Failed opening font"); - delete [] buffer; continue; } // Our provided font of jpn0.pgf has size 4367080 bytes @@ -511,16 +525,12 @@ void __LoadInternalFonts() { if (std::string(entry.fileName) == "jpn0.pgf" && (int)info.size == 4367080) { packagedFont = true; } - - pspFileSystem.ReadFile(handle, buffer, info.size); - pspFileSystem.CloseFile(handle); - internalFonts.push_back(new Font(buffer, (size_t)info.size, entry)); + internalFonts.push_back(new Font(buffer, entry)); - delete [] buffer; - INFO_LOG(SCEFONT, "Loaded font %s", fontFilename.c_str()); + DEBUG_LOG(SCEFONT, "Loaded font %s", fontFilename.c_str()); } else { - INFO_LOG(SCEFONT, "Font file not found: %s", fontFilename.c_str()); + WARN_LOG(SCEFONT, "Font file not found: %s", fontFilename.c_str()); } } } @@ -707,13 +717,9 @@ u32 sceFontOpenUserFile(u32 libHandle, const char *fileName, u32 mode, u32 error return 0; } - u8 *buffer = new u8[(size_t)info.size]; - - u32 fileHandle = pspFileSystem.OpenFile(fileName, FILEACCESS_READ); - pspFileSystem.ReadFile(fileHandle, buffer, info.size); - pspFileSystem.CloseFile(fileHandle); - - LoadedFont *font = fontLib->OpenFont(new Font(buffer, (size_t)info.size)); + std::vector buffer; + pspFileSystem.ReadEntireFile(fileName, buffer); + LoadedFont *font = fontLib->OpenFont(new Font(buffer)); if (font) { fontMap[font->Handle()] = font; Memory::Write_U32(0, errorCodePtr); diff --git a/Core/MIPS/MIPSAnalyst.cpp b/Core/MIPS/MIPSAnalyst.cpp index 2077a737a8..a9f6f9ecd3 100644 --- a/Core/MIPS/MIPSAnalyst.cpp +++ b/Core/MIPS/MIPSAnalyst.cpp @@ -488,7 +488,7 @@ namespace MIPSAnalyst { // Yay, found a function. Function &f = *(iter->second); - if (f.hash = mf->hash && f.size == mf->size) { + if (f.hash == mf->hash && f.size == mf->size) { strncpy(f.name, mf->name, sizeof(mf->name) - 1); const char *existingLabel = symbolMap.GetLabelName(f.start); diff --git a/Core/PSPLoaders.cpp b/Core/PSPLoaders.cpp index 7dfce911a8..825d1e20a6 100644 --- a/Core/PSPLoaders.cpp +++ b/Core/PSPLoaders.cpp @@ -81,11 +81,9 @@ void InitMemoryForGameISO(std::string fileToStart) { if (fileInfo.exists) { - u8 *paramsfo = new u8[(size_t)fileInfo.size]; - u32 fd = pspFileSystem.OpenFile(sfoPath, FILEACCESS_READ); - pspFileSystem.ReadFile(fd, paramsfo, fileInfo.size); - pspFileSystem.CloseFile(fd); - if (g_paramSFO.ReadSFO(paramsfo, (size_t)fileInfo.size)) + std::vector paramsfo; + pspFileSystem.ReadEntireFile(sfoPath, paramsfo); + if (g_paramSFO.ReadSFO(paramsfo)) { gameID = g_paramSFO.GetValueString("DISC_ID"); @@ -100,7 +98,6 @@ void InitMemoryForGameISO(std::string fileToStart) { } DEBUG_LOG(LOADER, "HDRemaster mode is %s", g_RemasterMode? "true": "false"); } - delete [] paramsfo; } } @@ -112,18 +109,15 @@ bool Load_PSP_ISO(const char *filename, std::string *error_string) PSPFileInfo fileInfo = pspFileSystem.GetFileInfo(sfoPath.c_str()); if (fileInfo.exists) { - u8 *paramsfo = new u8[(size_t)fileInfo.size]; - u32 fd = pspFileSystem.OpenFile(sfoPath, FILEACCESS_READ); - pspFileSystem.ReadFile(fd, paramsfo, fileInfo.size); - pspFileSystem.CloseFile(fd); - if (g_paramSFO.ReadSFO(paramsfo, (size_t)fileInfo.size)) + std::vector paramsfo; + pspFileSystem.ReadEntireFile(sfoPath, paramsfo); + if (g_paramSFO.ReadSFO(paramsfo)) { char title[1024]; sprintf(title, "%s : %s", g_paramSFO.GetValueString("DISC_ID").c_str(), g_paramSFO.GetValueString("TITLE").c_str()); INFO_LOG(LOADER, "%s", title); host->SetWindowTitle(title); } - delete [] paramsfo; } diff --git a/Core/Util/GameManager.cpp b/Core/Util/GameManager.cpp index 4ca4988611..0976e25a1d 100644 --- a/Core/Util/GameManager.cpp +++ b/Core/Util/GameManager.cpp @@ -158,7 +158,7 @@ bool GameManager::InstallGame(std::string zipfile, bool deleteAfter) { if (zippedName[i] == '/') { slashCount++; slashLocation = lastSlashLocation; - lastSlashLocation = i; + lastSlashLocation = (int)i; } } if (slashCount >= 1 && (!isPSP || slashLocation < stripChars + 1)) { diff --git a/Core/Util/PPGeDraw.cpp b/Core/Util/PPGeDraw.cpp index 613c952388..db6f9a15ed 100644 --- a/Core/Util/PPGeDraw.cpp +++ b/Core/Util/PPGeDraw.cpp @@ -830,7 +830,7 @@ bool PPGeImage::Load() { if (filename_.empty()) { success = pngLoadPtr(Memory::GetPointer(png_), size_, &width_, &height_, &textureData, false); } else { - std::string pngData; + std::vector pngData; if (pspFileSystem.ReadEntireFile(filename_, pngData) < 0) { WARN_LOG(SCEGE, "Bad PPGeImage - cannot load file"); return false; diff --git a/UI/NativeApp.cpp b/UI/NativeApp.cpp index 826100ea12..a76a72c176 100644 --- a/UI/NativeApp.cpp +++ b/UI/NativeApp.cpp @@ -571,7 +571,7 @@ void DrawDownloadsOverlay(UIContext &ctx) { ctx.Begin(); int h = 5; - for (int i = 0; i < progress.size(); i++) { + for (size_t i = 0; i < progress.size(); i++) { float barWidth = 10 + (dp_xres - 10) * progress[i]; Bounds bounds(0, h * i, barWidth, h); UI::Drawable solid(colors[i & 3]);