Skip to content

Commit

Permalink
https://github.com/ImageMagick/ImageMagick/pull/2081
Browse files Browse the repository at this point in the history
  • Loading branch information
Cristy committed Jun 10, 2020
1 parent 886b203 commit d49a1a9
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 14 deletions.
7 changes: 7 additions & 0 deletions ChangeLog
@@ -1,3 +1,10 @@
2020-06-09 7.0.10-19 Cristy <quetzlzacatenango@image...>
* Release ImageMagick version 7.0.10-19, GIT revision 17...

2020-06-09 7.0.10-19 Cristy <quetzlzacatenango@image...>
* improved checking for write failures (reference
https://github.com/ImageMagick/ImageMagick/pull/2081).

2020-06-08 7.0.10-18 Cristy <quetzlzacatenango@image...>
* Release ImageMagick version 7.0.10-18, GIT revision 17333:d071c2032:20200608

Expand Down
103 changes: 89 additions & 14 deletions MagickCore/blob.c
Expand Up @@ -93,7 +93,6 @@
#include <io.h>
#define _O_BINARY O_BINARY
#endif

/*
Typedef declarations.
*/
Expand Down Expand Up @@ -128,7 +127,8 @@ struct _BlobInfo
eof;

int
error;
error,
error_number;

MagickOffsetType
offset;
Expand Down Expand Up @@ -588,6 +588,14 @@ MagickExport BlobInfo *CloneBlobInfo(const BlobInfo *blob_info)
% o image: the image.
%
*/

static inline void ThrowBlobException(BlobInfo *blob_info)
{
if ((blob_info->status == MagickFalse) && (errno != 0))
blob_info->error_number=errno;
blob_info->status=MagickTrue;
}

MagickExport MagickBooleanType CloseBlob(Image *image)
{
BlobInfo
Expand Down Expand Up @@ -616,21 +624,35 @@ MagickExport MagickBooleanType CloseBlob(Image *image)
case PipeStream:
{
if (blob_info->synchronize != MagickFalse)
status=fsync(fileno(blob_info->file_info.file));
status=ferror(blob_info->file_info.file);
{
status=fflush(blob_info->file_info.file);
if (status != 0)
ThrowBlobException(blob_info);
status=fsync(fileno(blob_info->file_info.file));
if (status != 0)
ThrowBlobException(blob_info);
}
if ((status != 0) && (ferror(blob_info->file_info.file) != 0))
ThrowBlobException(blob_info);
break;
}
case ZipStream:
{
#if defined(MAGICKCORE_ZLIB_DELEGATE)
status=Z_OK;
(void) gzerror(blob_info->file_info.gzfile,&status);
if (status != Z_OK)
ThrowBlobException(blob_info);
#endif
break;
}
case BZipStream:
{
#if defined(MAGICKCORE_BZLIB_DELEGATE)
status=BZ_OK;
(void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
if (status != BZ_OK)
ThrowBlobException(blob_info);
#endif
break;
}
Expand All @@ -641,15 +663,22 @@ MagickExport MagickBooleanType CloseBlob(Image *image)
if (blob_info->file_info.file != (FILE *) NULL)
{
if (blob_info->synchronize != MagickFalse)
status=fsync(fileno(blob_info->file_info.file));
status=ferror(blob_info->file_info.file);
{
status=fflush(blob_info->file_info.file);
if (status != 0)
ThrowBlobException(blob_info);
status=fsync(fileno(blob_info->file_info.file));
if (status != 0)
ThrowBlobException(blob_info);
}
if ((status != 0) && (ferror(blob_info->file_info.file) != 0))
ThrowBlobException(blob_info);
}
break;
}
case CustomStream:
break;
}
blob_info->status=status < 0 ? MagickTrue : MagickFalse;
blob_info->size=GetBlobSize(image);
image->extent=blob_info->size;
blob_info->eof=MagickFalse;
Expand All @@ -668,20 +697,28 @@ MagickExport MagickBooleanType CloseBlob(Image *image)
case FileStream:
{
if (fileno(blob_info->file_info.file) != -1)
status=fclose(blob_info->file_info.file);
{
status=fclose(blob_info->file_info.file);
if (status != 0)
ThrowBlobException(blob_info);
}
break;
}
case PipeStream:
{
#if defined(MAGICKCORE_HAVE_PCLOSE)
status=pclose(blob_info->file_info.file);
if (status != 0)
ThrowBlobException(blob_info);
#endif
break;
}
case ZipStream:
{
#if defined(MAGICKCORE_ZLIB_DELEGATE)
status=gzclose(blob_info->file_info.gzfile);
if (status != Z_OK)
ThrowBlobException(blob_info);
#endif
break;
}
Expand All @@ -697,14 +734,17 @@ MagickExport MagickBooleanType CloseBlob(Image *image)
case BlobStream:
{
if (blob_info->file_info.file != (FILE *) NULL)
status=fclose(blob_info->file_info.file);
{
status=fclose(blob_info->file_info.file);
if (status != 0)
ThrowBlobException(blob_info);
}
break;
}
case CustomStream:
break;
}
(void) DetachBlob(blob_info);
blob_info->status=status < 0 ? MagickTrue : MagickFalse;
return(blob_info->status);
}

Expand Down Expand Up @@ -1677,6 +1717,8 @@ MagickExport MagickBooleanType GetBlobError(const Image *image)
assert(image->signature == MagickCoreSignature);
if (image->debug != MagickFalse)
(void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
if ((image->blob->status != MagickFalse) && (image->blob->error_number != 0))
errno=image->blob->error_number;
return(image->blob->status);
}

Expand Down Expand Up @@ -3487,8 +3529,9 @@ MagickExport MagickBooleanType OpenBlob(const ImageInfo *image_info,
blob_info->type=FileStream;
(void) SetStreamBuffering(image_info,image);
}
}
}
blob_info->status=MagickFalse;
blob_info->error_number=MagickFalse;
if (blob_info->type != UndefinedStream)
blob_info->size=GetBlobSize(image);
else
Expand Down Expand Up @@ -3688,7 +3731,8 @@ MagickExport ssize_t ReadBlob(Image *image,const size_t length,void *data)
*magick_restrict blob_info;

int
c;
c,
status;

register unsigned char
*q;
Expand All @@ -3705,6 +3749,7 @@ MagickExport ssize_t ReadBlob(Image *image,const size_t length,void *data)
assert(data != (void *) NULL);
blob_info=image->blob;
count=0;
status=0;
q=(unsigned char *) data;
switch (blob_info->type)
{
Expand Down Expand Up @@ -3756,6 +3801,9 @@ MagickExport ssize_t ReadBlob(Image *image,const size_t length,void *data)
case 0:
break;
}
if ((count != (ssize_t) length) &&
(ferror(blob_info->file_info.file) != 0))
ThrowBlobException(blob_info);
break;
}
case ZipStream:
Expand Down Expand Up @@ -3817,6 +3865,12 @@ MagickExport ssize_t ReadBlob(Image *image,const size_t length,void *data)
case 0:
break;
}
status=Z_OK;
(void) gzerror(blob_info->file_info.gzfile,&status);
if ((count != (ssize_t) length) && (status != Z_OK))
ThrowBlobException(blob_info);
if (blob_info->eof == MagickFalse)
blob_info->eof=gzeof(blob_info->file_info.gzfile);
#endif
break;
}
Expand All @@ -3838,6 +3892,10 @@ MagickExport ssize_t ReadBlob(Image *image,const size_t length,void *data)
}
}
count=i;
status=BZ_OK;
(void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
if ((count != (ssize_t) length) && (status != BZ_OK))
ThrowBlobException(blob_info);
#endif
break;
}
Expand Down Expand Up @@ -5539,7 +5597,8 @@ MagickExport ssize_t WriteBlob(Image *image,const size_t length,
*magick_restrict blob_info;

int
c;
c,
status;

register const unsigned char
*p;
Expand All @@ -5559,6 +5618,7 @@ MagickExport ssize_t WriteBlob(Image *image,const size_t length,
assert(data != (const void *) NULL);
blob_info=image->blob;
count=0;
status=0;
p=(const unsigned char *) data;
q=(unsigned char *) data;
switch (blob_info->type)
Expand Down Expand Up @@ -5608,6 +5668,9 @@ MagickExport ssize_t WriteBlob(Image *image,const size_t length,
case 0:
break;
}
if ((count != (ssize_t) length) &&
(ferror(blob_info->file_info.file) != 0))
ThrowBlobException(blob_info);
break;
}
case ZipStream:
Expand Down Expand Up @@ -5665,6 +5728,10 @@ MagickExport ssize_t WriteBlob(Image *image,const size_t length,
case 0:
break;
}
status=Z_OK;
(void) gzerror(blob_info->file_info.gzfile,&status);
if ((count != (ssize_t) length) && (status != Z_OK))
ThrowBlobException(blob_info);
#endif
break;
}
Expand All @@ -5686,6 +5753,10 @@ MagickExport ssize_t WriteBlob(Image *image,const size_t length,
}
}
count=i;
status=BZ_OK;
(void) BZ2_bzerror(blob_info->file_info.bzfile,&status);
if ((count != (ssize_t) length) && (status != BZ_OK))
ThrowBlobException(blob_info);
#endif
break;
}
Expand Down Expand Up @@ -5781,7 +5852,11 @@ MagickExport ssize_t WriteBlobByte(Image *image,const unsigned char value)

c=putc((int) value,blob_info->file_info.file);
if (c == EOF)
break;
{
if (ferror(blob_info->file_info.file) != 0)
ThrowBlobException(blob_info);
break;
}
count++;
break;
}
Expand Down
4 changes: 4 additions & 0 deletions MagickCore/constitute.c
Expand Up @@ -844,6 +844,8 @@ MagickExport Image *ReadImage(const ImageInfo *image_info,
image=next;
}
read_info=DestroyImageInfo(read_info);
if (GetBlobError(image) != MagickFalse)
ThrowImageException(FileOpenError,"UnableToReadFile");
return(GetFirstImageInList(image));
}

Expand Down Expand Up @@ -1277,6 +1279,8 @@ MagickExport MagickBooleanType WriteImage(const ImageInfo *image_info,
(write_info->verbose != MagickFalse))
(void) IdentifyImage(image,stdout,MagickFalse,exception);
write_info=DestroyImageInfo(write_info);
if (GetBlobError(image) != MagickFalse)
ThrowBinaryException(FileOpenError,"UnableToWriteFile",image->filename);
return(status);
}

Expand Down

0 comments on commit d49a1a9

Please sign in to comment.