loadsave: check for errors when using temp file
authorJeremy Maitin-Shepard <jbms@google.com>
Thu, 27 Jul 2017 20:44:36 +0000 (13:44 -0700)
committerJeremy Maitin-Shepard <jbms@google.com>
Mon, 31 Jul 2017 16:02:42 +0000 (09:02 -0700)
Previously, the return value of fwrite and fclose were not properly
checked, leading to possible silent truncation of the data if writing
failed, e.g. due to lack of disk space.

Fixes issue #9251.

modules/imgcodecs/src/loadsave.cpp

index 2614019..3b23662 100644 (file)
@@ -651,8 +651,15 @@ imdecode_( const Mat& buf, int flags, int hdrtype, Mat* mat=0 )
         if( !f )
             return 0;
         size_t bufSize = buf.cols*buf.rows*buf.elemSize();
-        fwrite( buf.ptr(), 1, bufSize, f );
-        fclose(f);
+        if( fwrite( buf.ptr(), 1, bufSize, f ) != bufSize )
+        {
+            fclose( f );
+            CV_Error( CV_StsError, "failed to write image data to temporary file" );
+        }
+        if( fclose(f) != 0 )
+        {
+            CV_Error( CV_StsError, "failed to write image data to temporary file" );
+        }
         decoder->setSource(filename);
     }