Check return value of fread in File::File.
authorCarl Hetherington <cth@carlh.net>
Thu, 24 Jan 2019 22:21:02 +0000 (22:21 +0000)
committerCarl Hetherington <cth@carlh.net>
Thu, 24 Jan 2019 22:21:02 +0000 (22:21 +0000)
src/file.cc

index 6fe4b2992f35ab620812186e2063240ebb4d2b4c..ce7a4c5e224a9365e403092e9771a736515893e8 100644 (file)
@@ -38,6 +38,7 @@
 #include "file.h"
 #include "util.h"
 #include "dcp_assert.h"
+#include "compose.hpp"
 #include <stdio.h>
 
 using namespace dcp;
@@ -51,7 +52,16 @@ File::File (boost::filesystem::path file)
        _data = new uint8_t[_size];
        FILE* f = dcp::fopen_boost (file, "rb");
        DCP_ASSERT (f);
-       fread (_data, 1, _size, f);
+       int const N = fread (_data, 1, _size, f);
+       if (N != _size) {
+               if (ferror(f)) {
+                       fclose (f);
+                       throw FileError (String::compose("fread error %1", errno), file, errno);
+               } else {
+                       fclose (f);
+                       throw FileError ("unexpected short read", file, -1);
+               }
+       }
        fclose (f);
 }