Fix many cases of return values from ftell and fseek being ignored

These could lead to undefined behaviour if e.g. a negative value from ftell
was used to allocate memory.

Also store result of ftell in a long;
The result may previously have been truncated on some platforms
This commit is contained in:
Ben Hymers 2016-03-14 09:33:17 +00:00
parent 632ae709d7
commit 3eb9c808c1
4 changed files with 68 additions and 13 deletions

View File

@ -166,10 +166,26 @@ bool InputGeom::loadGeomSet(rcContext* ctx, const std::string& filepath)
char* buf = 0;
FILE* fp = fopen(filepath.c_str(), "rb");
if (!fp)
{
return false;
fseek(fp, 0, SEEK_END);
int bufSize = ftell(fp);
fseek(fp, 0, SEEK_SET);
}
if (fseek(fp, 0, SEEK_END) != 0)
{
fclose(fp);
return false;
}
long bufSize = ftell(fp);
if (bufSize < 0)
{
fclose(fp);
return false;
}
if (fseek(fp, 0, SEEK_SET) != 0)
{
fclose(fp);
return false;
}
buf = new char[bufSize];
if (!buf)
{

View File

@ -141,9 +141,22 @@ bool rcMeshLoaderObj::load(const std::string& filename)
FILE* fp = fopen(filename.c_str(), "rb");
if (!fp)
return false;
fseek(fp, 0, SEEK_END);
int bufSize = ftell(fp);
fseek(fp, 0, SEEK_SET);
if (fseek(fp, 0, SEEK_END) != 0)
{
fclose(fp);
return false;
}
long bufSize = ftell(fp);
if (bufSize < 0)
{
fclose(fp);
return false;
}
if (fseek(fp, 0, SEEK_SET) != 0)
{
fclose(fp);
return false;
}
buf = new char[bufSize];
if (!buf)
{

View File

@ -102,9 +102,22 @@ bool TestCase::load(const std::string& filePath)
FILE* fp = fopen(filePath.c_str(), "rb");
if (!fp)
return false;
fseek(fp, 0, SEEK_END);
int bufSize = ftell(fp);
fseek(fp, 0, SEEK_SET);
if (fseek(fp, 0, SEEK_END) != 0)
{
fclose(fp);
return false;
}
long bufSize = ftell(fp);
if (bufSize < 0)
{
fclose(fp);
return false;
}
if (fseek(fp, 0, SEEK_SET) != 0)
{
fclose(fp);
return false;
}
buf = new char[bufSize];
if (!buf)
{

View File

@ -247,9 +247,22 @@ bool imguiRenderGLInit(const char* fontpath)
// Load font.
FILE* fp = fopen(fontpath, "rb");
if (!fp) return false;
fseek(fp, 0, SEEK_END);
size_t size = ftell(fp);
fseek(fp, 0, SEEK_SET);
if (fseek(fp, 0, SEEK_END) != 0)
{
fclose(fp);
return false;
}
long size = ftell(fp);
if (size < 0)
{
fclose(fp);
return false;
}
if (fseek(fp, 0, SEEK_SET) != 0)
{
fclose(fp);
return false;
}
unsigned char* ttfBuffer = (unsigned char*)malloc(size);
if (!ttfBuffer)
@ -260,7 +273,7 @@ bool imguiRenderGLInit(const char* fontpath)
size_t readLen = fread(ttfBuffer, 1, size, fp);
fclose(fp);
if (readLen != size)
if (readLen != static_cast<size_t>(size))
{
free(ttfBuffer);
return false;