From 43051ffe4100a8f59bd93dcb3002a3455a117018 Mon Sep 17 00:00:00 2001 From: "j4ck.fr0st" Date: Wed, 3 Feb 2010 20:29:49 +0000 Subject: [PATCH] some cleanup, housekeeping for error cases and increased a few buffer sizes that are overflown by its content. Thanks to Fatalis for the initial patch and pointing us to cppcheck. git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@5004 8ced0084-cf51-0410-be5f-012b33b47a6e --- Source/Core/Common/Src/FileUtil.cpp | 1 + Source/Core/Common/Src/SysConf.cpp | 27 +++-- Source/Core/Common/Src/SysConf.h | 4 +- Source/Core/Core/Src/Boot/Boot_WiiWAD.cpp | 3 + .../DolphinWX/Src/MemoryCards/GCMemcard.cpp | 98 ++++++++++--------- .../DolphinWX/Src/MemoryCards/GCMemcard.h | 5 +- .../Core/VideoCommon/Src/OpcodeDecoding.cpp | 2 +- Source/DSPSpy/ConsoleHelper.h | 3 +- .../Plugin_VideoDX9/Src/PixelShaderCache.cpp | 12 +-- 9 files changed, 88 insertions(+), 67 deletions(-) diff --git a/Source/Core/Common/Src/FileUtil.cpp b/Source/Core/Common/Src/FileUtil.cpp index e0b64e1f43..34c93d82cf 100644 --- a/Source/Core/Common/Src/FileUtil.cpp +++ b/Source/Core/Common/Src/FileUtil.cpp @@ -481,6 +481,7 @@ bool DeleteDirRecursively(const char *directory) FindClose(hFind); #else } + closedir(dirp); #endif File::DeleteDir(directory); diff --git a/Source/Core/Common/Src/SysConf.cpp b/Source/Core/Common/Src/SysConf.cpp index 3477770816..c9a86e6e15 100644 --- a/Source/Core/Common/Src/SysConf.cpp +++ b/Source/Core/Common/Src/SysConf.cpp @@ -49,18 +49,32 @@ SysConf::~SysConf() bool SysConf::LoadFromFile(const char *filename) { + // Basic check + u64 size = File::GetSize(filename); + if (size == 0) + return false; //most likely: file does not exist + if (size != SYSCONF_SIZE) + { + PanicAlert("Your SYSCONF file is the wrong size - should be 0x%04x (but is 0x%04x)", + SYSCONF_SIZE, size); + return false; + } FILE* f = fopen(filename, "rb"); if (f == NULL) return false; - - // Basic check - if (File::GetSize(filename) != SYSCONF_SIZE) + bool result = LoadFromFileInternal(f); + if (result) { - PanicAlert("Your SYSCONF file is the wrong size - should be 0x%04x", SYSCONF_SIZE); - return false; + // OK, done! + m_Filename = filename; } + fclose(f); + return result; +} +bool SysConf::LoadFromFileInternal(FILE *f) +{ // Fill in infos if (fread(&m_Header.version, sizeof(m_Header.version), 1, f) != 1) return false; if (fread(&m_Header.numEntries, sizeof(m_Header.numEntries), 1, f) != 1) return false; @@ -123,9 +137,6 @@ bool SysConf::LoadFromFile(const char *filename) } } - // OK!, done! - m_Filename = filename; - fclose(f); return true; } diff --git a/Source/Core/Common/Src/SysConf.h b/Source/Core/Common/Src/SysConf.h index e3bba9336f..d91cc471ac 100644 --- a/Source/Core/Common/Src/SysConf.h +++ b/Source/Core/Common/Src/SysConf.h @@ -71,7 +71,7 @@ public: bool IsValid() { return m_IsValid; } - void Reload(); + void Reload(); template T GetData(const char* sectionName) @@ -122,6 +122,8 @@ public: bool Save(); bool SaveToFile(const char* filename); bool LoadFromFile(const char* filename); +private: + bool LoadFromFileInternal(FILE *f); }; #endif // __SYSCONF_MANAGER_h__ diff --git a/Source/Core/Core/Src/Boot/Boot_WiiWAD.cpp b/Source/Core/Core/Src/Boot/Boot_WiiWAD.cpp index a777b15b6f..0f1d2b249b 100644 --- a/Source/Core/Core/Src/Boot/Boot_WiiWAD.cpp +++ b/Source/Core/Core/Src/Boot/Boot_WiiWAD.cpp @@ -151,7 +151,10 @@ bool CBoot::Install_WiiWAD(const char* _pFilename) DiscIO::WiiWAD Wad(_pFilename); if (!Wad.IsValid()) + { + fclose(pTicketFile); return false; + } fwrite(Wad.GetTicket(), Wad.GetTicketSize(), 1, pTicketFile); diff --git a/Source/Core/DolphinWX/Src/MemoryCards/GCMemcard.cpp b/Source/Core/DolphinWX/Src/MemoryCards/GCMemcard.cpp index e131a113b1..cfd45532ef 100644 --- a/Source/Core/DolphinWX/Src/MemoryCards/GCMemcard.cpp +++ b/Source/Core/DolphinWX/Src/MemoryCards/GCMemcard.cpp @@ -565,7 +565,7 @@ u32 GCMemcard::DEntry_GetSaveData(u8 index, u8* dest, bool old) } // End DEntry functions -u32 GCMemcard::ImportFile(DEntry& direntry, u8* contents, int remove) +u32 GCMemcard::ImportFile(DEntry& direntry, u8* contents, int remove) { if (!mcdFile) return NOMEMCARD; @@ -670,7 +670,6 @@ u32 GCMemcard::RemoveFile(u8 index) //index in the directory array { if (!mcdFile) return NOMEMCARD; - //error checking u16 startingblock = 0; for (int i = 0; i < DIRLEN; i++) @@ -713,11 +712,12 @@ u32 GCMemcard::RemoveFile(u8 index) //index in the directory array { case NOMEMCARD: delete[] tempSaveData; + tempSaveData = NULL; break; case FAIL: delete[] tempSaveData; + delete tempDEntry; return FAIL; - break; } } } @@ -781,12 +781,20 @@ u32 GCMemcard::ImportGci(const char *inputFile, std::string outputFile) FILE *gci = fopen(inputFile, "rb"); if (!gci) return OPENFAIL; + u32 result = ImportGciInternal(gci, inputFile, outputFile); + fclose(gci); + + return result; +} + +u32 GCMemcard::ImportGciInternal(FILE *gci, const char *inputFile, std::string outputFile) +{ int offset; - char * tmp = new char[0xD]; + char tmp[0xD]; std::string fileType; SplitPath(inputFile, NULL, NULL, &fileType); - if( !strcasecmp(fileType.c_str(), ".gci")) + if (!strcasecmp(fileType.c_str(), ".gci")) offset = GCI; else { @@ -796,30 +804,20 @@ u32 GCMemcard::ImportGci(const char *inputFile, std::string outputFile) if (!memcmp(tmp, "GCSAVE", 6)) // Header must be uppercase offset = GCS; else - { return GCSFAIL; - } } - else{ - if (!strcasecmp(fileType.c_str(), ".sav")) - { - if (!memcmp(tmp, "DATELGC_SAVE", 0xC)) // Header must be uppercase - offset = SAV; - else - { - return SAVFAIL; - } - } + else if (!strcasecmp(fileType.c_str(), ".sav")) + { + if (!memcmp(tmp, "DATELGC_SAVE", 0xC)) // Header must be uppercase + offset = SAV; else - { - return OPENFAIL; - } + return SAVFAIL; } + else + return OPENFAIL; } - delete []tmp; fseek(gci, offset, SEEK_SET); - DEntry *tempDEntry = new DEntry; fread(tempDEntry, 1, DENTRY_SIZE, gci); int fStart = (int) ftell(gci); @@ -830,13 +828,10 @@ u32 GCMemcard::ImportGci(const char *inputFile, std::string outputFile) Gcs_SavConvert(tempDEntry, offset, length); if (length != BE16(tempDEntry->BlockCount) * BLOCK_SIZE) - { return LENGTHFAIL; - } if (ftell(gci) != offset + DENTRY_SIZE) // Verify correct file position - { return OPENFAIL; - } + u32 size = BE16((tempDEntry->BlockCount)) * BLOCK_SIZE; u8 *tempSaveData = new u8[size]; fread(tempSaveData, 1, size, gci); @@ -844,24 +839,31 @@ u32 GCMemcard::ImportGci(const char *inputFile, std::string outputFile) u32 ret; if(!outputFile.empty()) { - FILE * gci2 = fopen(outputFile.c_str(), "wb"); + FILE *gci2 = fopen(outputFile.c_str(), "wb"); bool completeWrite = true; - if (!gci2) return OPENFAIL; + if (!gci2) + { + delete[] tempSaveData; + delete tempDEntry; + return OPENFAIL; + } fseek(gci2, 0, SEEK_SET); - if (fwrite(tempDEntry, 1, DENTRY_SIZE, gci2) != DENTRY_SIZE) completeWrite = false; + if (fwrite(tempDEntry, 1, DENTRY_SIZE, gci2) != DENTRY_SIZE) + completeWrite = false; int fileBlocks = BE16(tempDEntry->BlockCount); fseek(gci2, DENTRY_SIZE, SEEK_SET); - if (fwrite(tempSaveData, 1, BLOCK_SIZE * fileBlocks, gci2) != (unsigned) (BLOCK_SIZE * fileBlocks)) + if (fwrite(tempSaveData, 1, BLOCK_SIZE * fileBlocks, gci2) != (unsigned)(BLOCK_SIZE * fileBlocks)) completeWrite = false; fclose(gci2); if (completeWrite) ret = GCS; else ret = WRITEFAIL; } - else ret= ImportFile(*tempDEntry, tempSaveData,0); + else + ret = ImportFile(*tempDEntry, tempSaveData, 0); - delete []tempSaveData; + delete[] tempSaveData; delete tempDEntry; return ret; } @@ -870,7 +872,7 @@ u32 GCMemcard::ExportGci(u8 index, const char *fileName, std::string *fileName2) { FILE *gci; int offset = GCI; - if (!strcasecmp(fileName,".")) + if (!strcasecmp(fileName, ".")) { if (BE32(dir.Dir[index].Gamecode) == 0xFFFFFFFF) return SUCCESS; @@ -882,6 +884,7 @@ u32 GCMemcard::ExportGci(u8 index, const char *fileName, std::string *fileName2) sprintf(filename, "%s/%s_%s.gci", fileName2->c_str(), GameCode, dir.Dir[index].Filename); gci = fopen((const char *)filename, "wb"); + delete[] filename; } else { @@ -907,34 +910,36 @@ u32 GCMemcard::ExportGci(u8 index, const char *fileName, std::string *fileName2) switch(offset) { case GCS: - { u8 gcsHDR[GCS]; memset(gcsHDR, 0, GCS); memcpy(gcsHDR, "GCSAVE", 6); if (fwrite(gcsHDR, 1, GCS, gci) != GCS) completeWrite = false; break; - } case SAV: - { u8 savHDR[SAV]; memset(savHDR, 0, SAV); memcpy(savHDR, "DATELGC_SAVE", 0xC); if (fwrite(savHDR, 1, SAV, gci) != SAV) completeWrite = false; - } - break; - default: break; } DEntry tempDEntry; - if (!DEntry_Copy(index, tempDEntry)) return NOMEMCARD; + if (!DEntry_Copy(index, tempDEntry)) + { + fclose(gci); + return NOMEMCARD; + } Gcs_SavConvert(&tempDEntry, offset); if (fwrite(&tempDEntry, 1, DENTRY_SIZE, gci) != DENTRY_SIZE) completeWrite = false; u32 size = DEntry_BlockCount(index); - if (size == 0xFFFF) return FAIL; + if (size == 0xFFFF) + { + fclose(gci); + return FAIL; + } size *= BLOCK_SIZE; u8 *tempSaveData = new u8[size]; @@ -942,26 +947,23 @@ u32 GCMemcard::ExportGci(u8 index, const char *fileName, std::string *fileName2) { case FAIL: fclose(gci); - delete []tempSaveData; + delete[] tempSaveData; return FAIL; case NOMEMCARD: fclose(gci); - delete []tempSaveData; + delete[] tempSaveData; return NOMEMCARD; - default: - break; } fseek(gci, DENTRY_SIZE + offset, SEEK_SET); if (fwrite(tempSaveData, 1, size, gci) != size) completeWrite = false; fclose(gci); - delete [] tempSaveData; + delete[] tempSaveData; if (completeWrite) return SUCCESS; else return WRITEFAIL; - } -void GCMemcard::Gcs_SavConvert(DEntry* tempDEntry, int saveType, int length) +void GCMemcard::Gcs_SavConvert(DEntry* tempDEntry, int saveType, int length) { switch(saveType) { diff --git a/Source/Core/DolphinWX/Src/MemoryCards/GCMemcard.h b/Source/Core/DolphinWX/Src/MemoryCards/GCMemcard.h index a58aa656e0..ac3b201f82 100644 --- a/Source/Core/DolphinWX/Src/MemoryCards/GCMemcard.h +++ b/Source/Core/DolphinWX/Src/MemoryCards/GCMemcard.h @@ -229,9 +229,12 @@ public: u32 DEntry_GetSaveData(u8 index, u8* buffer, bool old); // adds the file to the directory and copies its contents - // if remove > 0 it will pad bat.map with 0's sifeof remove + // if remove > 0 it will pad bat.map with 0's sizeof remove u32 ImportFile(DEntry& direntry, u8* contents, int remove); +private: + u32 ImportGciInternal(FILE *gci, const char *inputFile, std::string outputFile); +public: // delete a file from the directory u32 RemoveFile(u8 index); diff --git a/Source/Core/VideoCommon/Src/OpcodeDecoding.cpp b/Source/Core/VideoCommon/Src/OpcodeDecoding.cpp index f65f0565bb..8511b38864 100644 --- a/Source/Core/VideoCommon/Src/OpcodeDecoding.cpp +++ b/Source/Core/VideoCommon/Src/OpcodeDecoding.cpp @@ -176,7 +176,7 @@ bool FifoCommandRunnable() { SCPFifoStruct &fifo = CommandProcessor::fifo; - char szTmp[256]; + char szTmp[512]; // sprintf(szTmp, "Illegal command %02x (at %08x)",cmd_byte,g_pDataReader->GetPtr()); sprintf(szTmp, "Illegal command %02x\n" "CPBase: 0x%08x\n" diff --git a/Source/DSPSpy/ConsoleHelper.h b/Source/DSPSpy/ConsoleHelper.h index d3a0ee1ed7..6c64035c7a 100644 --- a/Source/DSPSpy/ConsoleHelper.h +++ b/Source/DSPSpy/ConsoleHelper.h @@ -75,11 +75,10 @@ inline void CON_BlankRow(const int y) { int columns = 0, rows = 0; CON_GetMetrics(&columns, &rows); - char* blank = new char[columns]; + char blank[columns]; std::fill(blank, blank + columns, ' '); blank[columns-1] = '\0'; CON_Printf(0, y, "%s", blank); - delete blank; } #define CON_PrintRow(x, y, ...) \ diff --git a/Source/Plugins/Plugin_VideoDX9/Src/PixelShaderCache.cpp b/Source/Plugins/Plugin_VideoDX9/Src/PixelShaderCache.cpp index c31ed4f7c5..f0b1259064 100644 --- a/Source/Plugins/Plugin_VideoDX9/Src/PixelShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoDX9/Src/PixelShaderCache.cpp @@ -123,10 +123,10 @@ public: void PixelShaderCache::Init() { - char pprog[1024]; + char pprog[2048]; sprintf(pprog, "void main(\n" "out float4 ocol0 : COLOR0,\n" - " in float4 incol0 : COLOR0){\n" + "in float4 incol0 : COLOR0){\n" "ocol0 = incol0;\n" "}\n"); s_ClearProgram = D3D::CompileAndCreatePixelShader(pprog, (int)strlen(pprog)); @@ -143,7 +143,7 @@ void PixelShaderCache::Init() "uniform float4 cColMatrix[5] : register(c%d);\n" "void main(\n" "out float4 ocol0 : COLOR0,\n" - " in float4 uv0 : TEXCOORD0){\n" + "in float4 uv0 : TEXCOORD0){\n" "float4 texcol = tex2D(samp0,uv0.xy);\n" "ocol0 = float4(dot(texcol,cColMatrix[0]),dot(texcol,cColMatrix[1]),dot(texcol,cColMatrix[2]),dot(texcol,cColMatrix[3])) + cColMatrix[4];\n" "}\n",C_COLORMATRIX); @@ -153,7 +153,7 @@ void PixelShaderCache::Init() "uniform float4 cColMatrix[5] : register(c%d);\n" "void main(\n" "out float4 ocol0 : COLOR0,\n" - " in float4 uv0 : TEXCOORD0){\n" + "in float4 uv0 : TEXCOORD0){\n" "float4 texcol = tex2D(samp0,uv0.xy);\n" "float4 EncodedDepth = frac((texcol.r * (16777215.0f/16777216.0f)) * float4(1.0f,255.0f,255.0f*255.0f,255.0f*255.0f*255.0f));\n" "texcol = float4((EncodedDepth.rgb * (16777216.0f/16777215.0f)),1.0f);\n" @@ -165,7 +165,7 @@ void PixelShaderCache::Init() "uniform sampler samp1 : register(s1);\n" "void main(\n" "out float4 ocol0 : COLOR0,\n" - " in float4 incol0 : COLOR0,\n" + "in float4 incol0 : COLOR0,\n" "in float4 uv0 : TEXCOORD0,\n" "in float4 uv1 : TEXCOORD1,\n" "in float4 uv2 : TEXCOORD2,\n" @@ -194,7 +194,7 @@ void PixelShaderCache::Init() "uniform float4 cColMatrix[5] : register(c%d);\n" "void main(\n" "out float4 ocol0 : COLOR0,\n" - " in float4 incol0 : COLOR0,\n" + "in float4 incol0 : COLOR0,\n" "in float4 uv0 : TEXCOORD0,\n" "in float4 uv1 : TEXCOORD1,\n" "in float4 uv2 : TEXCOORD2,\n"