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
This commit is contained in:
j4ck.fr0st 2010-02-03 20:29:49 +00:00
parent 8281564b5c
commit 43051ffe41
9 changed files with 88 additions and 67 deletions

View File

@ -481,6 +481,7 @@ bool DeleteDirRecursively(const char *directory)
FindClose(hFind); FindClose(hFind);
#else #else
} }
closedir(dirp);
#endif #endif
File::DeleteDir(directory); File::DeleteDir(directory);

View File

@ -49,18 +49,32 @@ SysConf::~SysConf()
bool SysConf::LoadFromFile(const char *filename) 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"); FILE* f = fopen(filename, "rb");
if (f == NULL) if (f == NULL)
return false; return false;
bool result = LoadFromFileInternal(f);
// Basic check if (result)
if (File::GetSize(filename) != SYSCONF_SIZE)
{ {
PanicAlert("Your SYSCONF file is the wrong size - should be 0x%04x", SYSCONF_SIZE); // OK, done!
return false; m_Filename = filename;
} }
fclose(f);
return result;
}
bool SysConf::LoadFromFileInternal(FILE *f)
{
// Fill in infos // Fill in infos
if (fread(&m_Header.version, sizeof(m_Header.version), 1, f) != 1) return false; 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; 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; return true;
} }

View File

@ -71,7 +71,7 @@ public:
bool IsValid() { return m_IsValid; } bool IsValid() { return m_IsValid; }
void Reload(); void Reload();
template<class T> template<class T>
T GetData(const char* sectionName) T GetData(const char* sectionName)
@ -122,6 +122,8 @@ public:
bool Save(); bool Save();
bool SaveToFile(const char* filename); bool SaveToFile(const char* filename);
bool LoadFromFile(const char* filename); bool LoadFromFile(const char* filename);
private:
bool LoadFromFileInternal(FILE *f);
}; };
#endif // __SYSCONF_MANAGER_h__ #endif // __SYSCONF_MANAGER_h__

View File

@ -151,7 +151,10 @@ bool CBoot::Install_WiiWAD(const char* _pFilename)
DiscIO::WiiWAD Wad(_pFilename); DiscIO::WiiWAD Wad(_pFilename);
if (!Wad.IsValid()) if (!Wad.IsValid())
{
fclose(pTicketFile);
return false; return false;
}
fwrite(Wad.GetTicket(), Wad.GetTicketSize(), 1, pTicketFile); fwrite(Wad.GetTicket(), Wad.GetTicketSize(), 1, pTicketFile);

View File

@ -565,7 +565,7 @@ u32 GCMemcard::DEntry_GetSaveData(u8 index, u8* dest, bool old)
} }
// End DEntry functions // 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; if (!mcdFile) return NOMEMCARD;
@ -670,7 +670,6 @@ u32 GCMemcard::RemoveFile(u8 index) //index in the directory array
{ {
if (!mcdFile) return NOMEMCARD; if (!mcdFile) return NOMEMCARD;
//error checking //error checking
u16 startingblock = 0; u16 startingblock = 0;
for (int i = 0; i < DIRLEN; i++) for (int i = 0; i < DIRLEN; i++)
@ -713,11 +712,12 @@ u32 GCMemcard::RemoveFile(u8 index) //index in the directory array
{ {
case NOMEMCARD: case NOMEMCARD:
delete[] tempSaveData; delete[] tempSaveData;
tempSaveData = NULL;
break; break;
case FAIL: case FAIL:
delete[] tempSaveData; delete[] tempSaveData;
delete tempDEntry;
return FAIL; return FAIL;
break;
} }
} }
} }
@ -781,12 +781,20 @@ u32 GCMemcard::ImportGci(const char *inputFile, std::string outputFile)
FILE *gci = fopen(inputFile, "rb"); FILE *gci = fopen(inputFile, "rb");
if (!gci) return OPENFAIL; 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; int offset;
char * tmp = new char[0xD]; char tmp[0xD];
std::string fileType; std::string fileType;
SplitPath(inputFile, NULL, NULL, &fileType); SplitPath(inputFile, NULL, NULL, &fileType);
if( !strcasecmp(fileType.c_str(), ".gci")) if (!strcasecmp(fileType.c_str(), ".gci"))
offset = GCI; offset = GCI;
else else
{ {
@ -796,30 +804,20 @@ u32 GCMemcard::ImportGci(const char *inputFile, std::string outputFile)
if (!memcmp(tmp, "GCSAVE", 6)) // Header must be uppercase if (!memcmp(tmp, "GCSAVE", 6)) // Header must be uppercase
offset = GCS; offset = GCS;
else else
{
return GCSFAIL; return GCSFAIL;
}
} }
else{ else if (!strcasecmp(fileType.c_str(), ".sav"))
if (!strcasecmp(fileType.c_str(), ".sav")) {
{ if (!memcmp(tmp, "DATELGC_SAVE", 0xC)) // Header must be uppercase
if (!memcmp(tmp, "DATELGC_SAVE", 0xC)) // Header must be uppercase offset = SAV;
offset = SAV;
else
{
return SAVFAIL;
}
}
else else
{ return SAVFAIL;
return OPENFAIL;
}
} }
else
return OPENFAIL;
} }
delete []tmp;
fseek(gci, offset, SEEK_SET); fseek(gci, offset, SEEK_SET);
DEntry *tempDEntry = new DEntry; DEntry *tempDEntry = new DEntry;
fread(tempDEntry, 1, DENTRY_SIZE, gci); fread(tempDEntry, 1, DENTRY_SIZE, gci);
int fStart = (int) ftell(gci); int fStart = (int) ftell(gci);
@ -830,13 +828,10 @@ u32 GCMemcard::ImportGci(const char *inputFile, std::string outputFile)
Gcs_SavConvert(tempDEntry, offset, length); Gcs_SavConvert(tempDEntry, offset, length);
if (length != BE16(tempDEntry->BlockCount) * BLOCK_SIZE) if (length != BE16(tempDEntry->BlockCount) * BLOCK_SIZE)
{
return LENGTHFAIL; return LENGTHFAIL;
}
if (ftell(gci) != offset + DENTRY_SIZE) // Verify correct file position if (ftell(gci) != offset + DENTRY_SIZE) // Verify correct file position
{
return OPENFAIL; return OPENFAIL;
}
u32 size = BE16((tempDEntry->BlockCount)) * BLOCK_SIZE; u32 size = BE16((tempDEntry->BlockCount)) * BLOCK_SIZE;
u8 *tempSaveData = new u8[size]; u8 *tempSaveData = new u8[size];
fread(tempSaveData, 1, size, gci); fread(tempSaveData, 1, size, gci);
@ -844,24 +839,31 @@ u32 GCMemcard::ImportGci(const char *inputFile, std::string outputFile)
u32 ret; u32 ret;
if(!outputFile.empty()) if(!outputFile.empty())
{ {
FILE * gci2 = fopen(outputFile.c_str(), "wb"); FILE *gci2 = fopen(outputFile.c_str(), "wb");
bool completeWrite = true; bool completeWrite = true;
if (!gci2) return OPENFAIL; if (!gci2)
{
delete[] tempSaveData;
delete tempDEntry;
return OPENFAIL;
}
fseek(gci2, 0, SEEK_SET); 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); int fileBlocks = BE16(tempDEntry->BlockCount);
fseek(gci2, DENTRY_SIZE, SEEK_SET); 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; completeWrite = false;
fclose(gci2); fclose(gci2);
if (completeWrite) ret = GCS; if (completeWrite) ret = GCS;
else ret = WRITEFAIL; else ret = WRITEFAIL;
} }
else ret= ImportFile(*tempDEntry, tempSaveData,0); else
ret = ImportFile(*tempDEntry, tempSaveData, 0);
delete []tempSaveData; delete[] tempSaveData;
delete tempDEntry; delete tempDEntry;
return ret; return ret;
} }
@ -870,7 +872,7 @@ u32 GCMemcard::ExportGci(u8 index, const char *fileName, std::string *fileName2)
{ {
FILE *gci; FILE *gci;
int offset = GCI; int offset = GCI;
if (!strcasecmp(fileName,".")) if (!strcasecmp(fileName, "."))
{ {
if (BE32(dir.Dir[index].Gamecode) == 0xFFFFFFFF) return SUCCESS; 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); sprintf(filename, "%s/%s_%s.gci", fileName2->c_str(), GameCode, dir.Dir[index].Filename);
gci = fopen((const char *)filename, "wb"); gci = fopen((const char *)filename, "wb");
delete[] filename;
} }
else else
{ {
@ -907,34 +910,36 @@ u32 GCMemcard::ExportGci(u8 index, const char *fileName, std::string *fileName2)
switch(offset) switch(offset)
{ {
case GCS: case GCS:
{
u8 gcsHDR[GCS]; u8 gcsHDR[GCS];
memset(gcsHDR, 0, GCS); memset(gcsHDR, 0, GCS);
memcpy(gcsHDR, "GCSAVE", 6); memcpy(gcsHDR, "GCSAVE", 6);
if (fwrite(gcsHDR, 1, GCS, gci) != GCS) completeWrite = false; if (fwrite(gcsHDR, 1, GCS, gci) != GCS) completeWrite = false;
break; break;
}
case SAV: case SAV:
{
u8 savHDR[SAV]; u8 savHDR[SAV];
memset(savHDR, 0, SAV); memset(savHDR, 0, SAV);
memcpy(savHDR, "DATELGC_SAVE", 0xC); memcpy(savHDR, "DATELGC_SAVE", 0xC);
if (fwrite(savHDR, 1, SAV, gci) != SAV) completeWrite = false; if (fwrite(savHDR, 1, SAV, gci) != SAV) completeWrite = false;
}
break;
default:
break; break;
} }
DEntry tempDEntry; DEntry tempDEntry;
if (!DEntry_Copy(index, tempDEntry)) return NOMEMCARD; if (!DEntry_Copy(index, tempDEntry))
{
fclose(gci);
return NOMEMCARD;
}
Gcs_SavConvert(&tempDEntry, offset); Gcs_SavConvert(&tempDEntry, offset);
if (fwrite(&tempDEntry, 1, DENTRY_SIZE, gci) != DENTRY_SIZE) completeWrite = false; if (fwrite(&tempDEntry, 1, DENTRY_SIZE, gci) != DENTRY_SIZE) completeWrite = false;
u32 size = DEntry_BlockCount(index); u32 size = DEntry_BlockCount(index);
if (size == 0xFFFF) return FAIL; if (size == 0xFFFF)
{
fclose(gci);
return FAIL;
}
size *= BLOCK_SIZE; size *= BLOCK_SIZE;
u8 *tempSaveData = new u8[size]; u8 *tempSaveData = new u8[size];
@ -942,26 +947,23 @@ u32 GCMemcard::ExportGci(u8 index, const char *fileName, std::string *fileName2)
{ {
case FAIL: case FAIL:
fclose(gci); fclose(gci);
delete []tempSaveData; delete[] tempSaveData;
return FAIL; return FAIL;
case NOMEMCARD: case NOMEMCARD:
fclose(gci); fclose(gci);
delete []tempSaveData; delete[] tempSaveData;
return NOMEMCARD; return NOMEMCARD;
default:
break;
} }
fseek(gci, DENTRY_SIZE + offset, SEEK_SET); fseek(gci, DENTRY_SIZE + offset, SEEK_SET);
if (fwrite(tempSaveData, 1, size, gci) != size) if (fwrite(tempSaveData, 1, size, gci) != size)
completeWrite = false; completeWrite = false;
fclose(gci); fclose(gci);
delete [] tempSaveData; delete[] tempSaveData;
if (completeWrite) return SUCCESS; if (completeWrite) return SUCCESS;
else return WRITEFAIL; 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) switch(saveType)
{ {

View File

@ -229,9 +229,12 @@ public:
u32 DEntry_GetSaveData(u8 index, u8* buffer, bool old); u32 DEntry_GetSaveData(u8 index, u8* buffer, bool old);
// adds the file to the directory and copies its contents // 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); 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 // delete a file from the directory
u32 RemoveFile(u8 index); u32 RemoveFile(u8 index);

View File

@ -176,7 +176,7 @@ bool FifoCommandRunnable()
{ {
SCPFifoStruct &fifo = CommandProcessor::fifo; 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 (at %08x)",cmd_byte,g_pDataReader->GetPtr());
sprintf(szTmp, "Illegal command %02x\n" sprintf(szTmp, "Illegal command %02x\n"
"CPBase: 0x%08x\n" "CPBase: 0x%08x\n"

View File

@ -75,11 +75,10 @@ inline void CON_BlankRow(const int y)
{ {
int columns = 0, rows = 0; int columns = 0, rows = 0;
CON_GetMetrics(&columns, &rows); CON_GetMetrics(&columns, &rows);
char* blank = new char[columns]; char blank[columns];
std::fill(blank, blank + columns, ' '); std::fill(blank, blank + columns, ' ');
blank[columns-1] = '\0'; blank[columns-1] = '\0';
CON_Printf(0, y, "%s", blank); CON_Printf(0, y, "%s", blank);
delete blank;
} }
#define CON_PrintRow(x, y, ...) \ #define CON_PrintRow(x, y, ...) \

View File

@ -123,10 +123,10 @@ public:
void PixelShaderCache::Init() void PixelShaderCache::Init()
{ {
char pprog[1024]; char pprog[2048];
sprintf(pprog, "void main(\n" sprintf(pprog, "void main(\n"
"out float4 ocol0 : COLOR0,\n" "out float4 ocol0 : COLOR0,\n"
" in float4 incol0 : COLOR0){\n" "in float4 incol0 : COLOR0){\n"
"ocol0 = incol0;\n" "ocol0 = incol0;\n"
"}\n"); "}\n");
s_ClearProgram = D3D::CompileAndCreatePixelShader(pprog, (int)strlen(pprog)); s_ClearProgram = D3D::CompileAndCreatePixelShader(pprog, (int)strlen(pprog));
@ -143,7 +143,7 @@ void PixelShaderCache::Init()
"uniform float4 cColMatrix[5] : register(c%d);\n" "uniform float4 cColMatrix[5] : register(c%d);\n"
"void main(\n" "void main(\n"
"out float4 ocol0 : COLOR0,\n" "out float4 ocol0 : COLOR0,\n"
" in float4 uv0 : TEXCOORD0){\n" "in float4 uv0 : TEXCOORD0){\n"
"float4 texcol = tex2D(samp0,uv0.xy);\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" "ocol0 = float4(dot(texcol,cColMatrix[0]),dot(texcol,cColMatrix[1]),dot(texcol,cColMatrix[2]),dot(texcol,cColMatrix[3])) + cColMatrix[4];\n"
"}\n",C_COLORMATRIX); "}\n",C_COLORMATRIX);
@ -153,7 +153,7 @@ void PixelShaderCache::Init()
"uniform float4 cColMatrix[5] : register(c%d);\n" "uniform float4 cColMatrix[5] : register(c%d);\n"
"void main(\n" "void main(\n"
"out float4 ocol0 : COLOR0,\n" "out float4 ocol0 : COLOR0,\n"
" in float4 uv0 : TEXCOORD0){\n" "in float4 uv0 : TEXCOORD0){\n"
"float4 texcol = tex2D(samp0,uv0.xy);\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" "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" "texcol = float4((EncodedDepth.rgb * (16777216.0f/16777215.0f)),1.0f);\n"
@ -165,7 +165,7 @@ void PixelShaderCache::Init()
"uniform sampler samp1 : register(s1);\n" "uniform sampler samp1 : register(s1);\n"
"void main(\n" "void main(\n"
"out float4 ocol0 : COLOR0,\n" "out float4 ocol0 : COLOR0,\n"
" in float4 incol0 : COLOR0,\n" "in float4 incol0 : COLOR0,\n"
"in float4 uv0 : TEXCOORD0,\n" "in float4 uv0 : TEXCOORD0,\n"
"in float4 uv1 : TEXCOORD1,\n" "in float4 uv1 : TEXCOORD1,\n"
"in float4 uv2 : TEXCOORD2,\n" "in float4 uv2 : TEXCOORD2,\n"
@ -194,7 +194,7 @@ void PixelShaderCache::Init()
"uniform float4 cColMatrix[5] : register(c%d);\n" "uniform float4 cColMatrix[5] : register(c%d);\n"
"void main(\n" "void main(\n"
"out float4 ocol0 : COLOR0,\n" "out float4 ocol0 : COLOR0,\n"
" in float4 incol0 : COLOR0,\n" "in float4 incol0 : COLOR0,\n"
"in float4 uv0 : TEXCOORD0,\n" "in float4 uv0 : TEXCOORD0,\n"
"in float4 uv1 : TEXCOORD1,\n" "in float4 uv1 : TEXCOORD1,\n"
"in float4 uv2 : TEXCOORD2,\n" "in float4 uv2 : TEXCOORD2,\n"