Add sanity checks for fseek and ftell return values

In theory, these calls could fail, but it is not really likely to
happen in practice in the use case here. Anyway, check that they do
not return an error before accepting the length of the file.
This commit is contained in:
Jouni Malinen 2011-04-14 20:22:21 +03:00 committed by Jouni Malinen
parent 4a26df7107
commit fe655a8402

View file

@ -315,14 +315,21 @@ char * os_readfile(const char *name, size_t *len)
{ {
FILE *f; FILE *f;
char *buf; char *buf;
long pos;
f = fopen(name, "rb"); f = fopen(name, "rb");
if (f == NULL) if (f == NULL)
return NULL; return NULL;
fseek(f, 0, SEEK_END); if (fseek(f, 0, SEEK_END) < 0 || (pos = ftell(f)) < 0) {
*len = ftell(f); fclose(f);
fseek(f, 0, SEEK_SET); return NULL;
}
*len = pos;
if (fseek(f, 0, SEEK_SET) < 0) {
fclose(f);
return NULL;
}
buf = os_malloc(*len); buf = os_malloc(*len);
if (buf == NULL) { if (buf == NULL) {