]> git.ipfire.org Git - thirdparty/hostap.git/commitdiff
Add sanity checks for fseek and ftell return values
authorJouni Malinen <jouni.malinen@atheros.com>
Thu, 14 Apr 2011 17:22:21 +0000 (20:22 +0300)
committerJouni Malinen <j@w1.fi>
Thu, 14 Apr 2011 17:22:21 +0000 (20:22 +0300)
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.

src/utils/os_unix.c

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