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);
#else
}
closedir(dirp);
#endif
File::DeleteDir(directory);

View File

@ -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;
}

View File

@ -71,7 +71,7 @@ public:
bool IsValid() { return m_IsValid; }
void Reload();
void Reload();
template<class T>
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__

View File

@ -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);

View File

@ -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)
{

View File

@ -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);

View File

@ -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"

View File

@ -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, ...) \

View File

@ -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"