Offset sanity checks

This commit is contained in:
surkeh 2018-04-19 05:58:35 -07:00
parent 2977274f93
commit 889a3e44a6
6 changed files with 15 additions and 7 deletions

View File

@ -45,6 +45,7 @@ void write(const char *filename,
fclose(out); fclose(out);
if (written == 0) { if (written == 0) {
perror("write error"); perror("write error");
exit(4);
} }
} }
} }
@ -61,6 +62,7 @@ void append(const char *filename, const char *t, unsigned size) {
fclose(out); fclose(out);
if (written == 0) { if (written == 0) {
perror("write error"); perror("write error");
exit(4);
} }
} }
} }

View File

@ -57,8 +57,8 @@ void writemesh(const struct MeshHeader *mh,
} }
void extractmeshes(const char *filedata, void extractmeshes(const char *filedata,
unsigned int filesize, const char *filename,
const char *filename) { unsigned int filesize) {
if (filedata == NULL || filesize <= 0) { if (filedata == NULL || filesize <= 0) {
return; return;
} }
@ -83,7 +83,7 @@ int main(int argc, char ** argv) {
char *f = argv[1]; char *f = argv[1];
unsigned int bufsize = 0; unsigned int bufsize = 0;
char *buffer = loadfile(f, &bufsize); char *buffer = loadfile(f, &bufsize);
extractmeshes(buffer, bufsize, f); extractmeshes(buffer, f, bufsize);
free(buffer); free(buffer);
return 0; return 0;
} }

View File

@ -105,7 +105,7 @@ static bool getmeshheader(struct MeshHeader**, unsigned int i, const char * cons
static bool getmeshbatch(struct Batch*, unsigned int offset, const char * const); static bool getmeshbatch(struct Batch*, unsigned int offset, const char * const);
static bool getmesh(struct Mesh*, unsigned int i, const char* filename); static bool getmesh(struct Mesh*, unsigned int i, const char* filename, unsigned int filesize);
#endif #endif

View File

@ -100,7 +100,8 @@ static bool getmeshbatch(struct Batch *b,
// assume client has allocated memory for mesh // assume client has allocated memory for mesh
static bool getmesh(struct Mesh *m, static bool getmesh(struct Mesh *m,
unsigned int i, unsigned int i,
const char * const filedata) { const char * const filedata,
unsigned int filesize) {
bool done = false; bool done = false;
if (m == NULL || filedata == NULL || m -> b == NULL) { if (m == NULL || filedata == NULL || m -> b == NULL) {
return done; return done;
@ -112,6 +113,9 @@ static bool getmesh(struct Mesh *m,
struct Batch b; struct Batch b;
for (j = 0; j < mh -> numBatch; j++) { for (j = 0; j < mh -> numBatch; j++) {
unsigned int offset = mh->offsetBatches + j * sizeof(struct BatchData); unsigned int offset = mh->offsetBatches + j * sizeof(struct BatchData);
if (offset > filesize) {
return done;
}
getmeshbatch(&b, offset, filedata); getmeshbatch(&b, offset, filedata);
// printmeshbatch(&b); // printmeshbatch(&b);
m -> b[j] = b; m -> b[j] = b;

View File

@ -8,8 +8,10 @@ static bool getpldh(struct PldHeader *ph, const char *filedata) {
if (ph != NULL && filedata != NULL) { if (ph != NULL && filedata != NULL) {
ph -> numOffset = (int32_t)filedata[0]; ph -> numOffset = (int32_t)filedata[0];
ph -> offsets = (uint32_t*)(filedata + sizeof(int32_t)); ph -> offsets = (uint32_t*)(filedata + sizeof(int32_t));
if ( ph->numOffset > 0 ) {
good = true; good = true;
} }
}
return good; return good;
} }

View File

@ -37,7 +37,7 @@ static bool gettexdescriptor(struct TextureBatchDescriptor **descriptor,
bool done = false; bool done = false;
unsigned int offset = sizeof(struct TexturePack); unsigned int offset = sizeof(struct TexturePack);
offset += sizeof(struct TextureBatchDescriptor) * i; offset += sizeof(struct TextureBatchDescriptor) * i;
if (filedata != NULL) { if (filedata != NULL && offset <= filesize) {
*descriptor = (struct TextureBatchDescriptor*)(filedata + offset); *descriptor = (struct TextureBatchDescriptor*)(filedata + offset);
done = true; done = true;
} }