Skip to content

Commit 2be955a

Browse files
committed
loadsave: check for errors when using temp file
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 opencv#9251.
1 parent 06407b4 commit 2be955a

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

modules/imgcodecs/src/loadsave.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -651,8 +651,15 @@ imdecode_( const Mat& buf, int flags, int hdrtype, Mat* mat=0 )
651651
if( !f )
652652
return 0;
653653
size_t bufSize = buf.cols*buf.rows*buf.elemSize();
654-
fwrite( buf.ptr(), 1, bufSize, f );
655-
fclose(f);
654+
if( fwrite( buf.ptr(), 1, bufSize, f ) != bufSize )
655+
{
656+
fclose( f );
657+
CV_Error( CV_StsError, "failed to write image data to temporary file" );
658+
}
659+
if( fclose(f) != 0 )
660+
{
661+
CV_Error( CV_StsError, "failed to write image data to temporary file" );
662+
}
656663
decoder->setSource(filename);
657664
}
658665

0 commit comments

Comments
 (0)