mm_log((1,"i_readgif_low(GifFile %p, colour_table %p, colours %p)\n", GifFile, colour_table, colours));
+ /* it's possible that the caller has called us with *colour_table being
+ non-NULL, but we check that to see if we need to free an allocated
+ colour table on error.
+ */
+ if (colour_table)
+ *colour_table = NULL;
+
BackGround = GifFile->SBackGroundColor;
ColorMap = (GifFile->Image.ColorMap ? GifFile->Image.ColorMap : GifFile->SColorMap);
if (DGifGetRecordType(GifFile, &RecordType) == GIF_ERROR) {
gif_push_error();
i_push_error(0, "Unable to get record type");
- if (colour_table) free(colour_table); /* FIXME: Isn't this an error? */
+ if (colour_table && *colour_table) {
+ free(*colour_table);
+ *colour_table = NULL;
+ }
i_img_destroy(im);
DGifCloseFile(GifFile);
return NULL;
if (DGifGetImageDesc(GifFile) == GIF_ERROR) {
gif_push_error();
i_push_error(0, "Unable to get image descriptor");
- if (colour_table) free(colour_table); /* FIXME: Isn't this an error? */
+ if (colour_table && *colour_table) {
+ free(*colour_table);
+ *colour_table = NULL;
+ }
i_img_destroy(im);
DGifCloseFile(GifFile);
return NULL;
/* No colormap and we are about to read in the image - abandon for now */
mm_log((1, "Going in with no colormap\n"));
i_push_error(0, "Image does not have a local or a global color map");
- if (colour_table) free(colour_table); /* FIXME: Isn't this an error? */
+ /* we can't have allocated a colour table here */
i_img_destroy(im);
DGifCloseFile(GifFile);
return NULL;
if (GifFile->Image.Left + GifFile->Image.Width > GifFile->SWidth ||
GifFile->Image.Top + GifFile->Image.Height > GifFile->SHeight) {
i_push_errorf(0, "Image %d is not confined to screen dimension, aborted.\n",ImageNum);
- if (colour_table) free(colour_table); /* FIXME: Yet again */
+ if (colour_table && *colour_table) {
+ free(*colour_table);
+ *colour_table = NULL;
+ }
i_img_destroy(im);
DGifCloseFile(GifFile);
return(0);
if (DGifGetLine(GifFile, &GifRow[Col], Width) == GIF_ERROR) {
gif_push_error();
i_push_error(0, "Reading GIF line");
- if (colour_table)
- free(colour_table);
+ if (colour_table && *colour_table) {
+ free(*colour_table);
+ *colour_table = NULL;
+ }
i_img_destroy(im);
DGifCloseFile(GifFile);
return NULL;
if (DGifGetLine(GifFile, &GifRow[Col], Width) == GIF_ERROR) {
gif_push_error();
i_push_error(0, "Reading GIF line");
- if (colour_table)
- free(colour_table);
+ if (colour_table && *colour_table) {
+ free(*colour_table);
+ *colour_table = NULL;
+ }
i_img_destroy(im);
DGifCloseFile(GifFile);
return NULL;
if (DGifGetExtension(GifFile, &ExtCode, &Extension) == GIF_ERROR) {
gif_push_error();
i_push_error(0, "Reading extension record");
- if (colour_table)
- free(colour_table);
+ if (colour_table && *colour_table) {
+ free(*colour_table);
+ *colour_table = NULL;
+ }
i_img_destroy(im);
DGifCloseFile(GifFile);
return NULL;
if (DGifGetExtensionNext(GifFile, &Extension) == GIF_ERROR) {
gif_push_error();
i_push_error(0, "reading next block of extension");
- if (colour_table)
- free(colour_table);
+ if (colour_table && *colour_table) {
+ free(*colour_table);
+ *colour_table = NULL;
+ }
i_img_destroy(im);
DGifCloseFile(GifFile);
return NULL;
if (DGifCloseFile(GifFile) == GIF_ERROR) {
gif_push_error();
i_push_error(0, "Closing GIF file object");
- if (colour_table)
- free(colour_table);
+ if (colour_table && *colour_table) {
+ free(*colour_table);
+ *colour_table = NULL;
+ }
i_img_destroy(im);
return NULL;
}
$|=1;
-print "1..19\n";
+print "1..22\n";
use Imager qw(:all);
init_log("testout/t105gif.log",1);
else {
print "ok 19 # ",Imager::_error_as_msg(),"\n";
}
-}
+ # try to read a truncated gif (no image descriptors)
+ read_failure('testimg/trimgdesc.gif', 20);
+ # file truncated just after the image descriptor tag
+ read_failure('testimg/trmiddesc.gif', 21);
+ # image has no colour map
+ read_failure('testimg/nocmap.gif', 22);
+ # image has a local colour map
+ open FH, "< testimg/loccmap.gif"
+ or die "Cannot open testimg/loccmap.gif: $!";
+ binmode FH;
+ if (i_readgif(fileno(FH))) {
+ print "ok 23\n";
+ }
+ else {
+ print "not ok 23 # failed to read image with only a local colour map";
+ }
+}
sub test_readgif_cb {
my ($size) = @_;
close FH;
return $img;
}
+
+# tests for reading bad gif files
+sub read_failure {
+ my ($filename, $testnum) = @_;
+
+ open FH, "< $filename"
+ or die "Cannot open $filename: $!";
+ binmode FH;
+ my ($result, $map) = i_readgif(fileno(FH));
+ if ($result) {
+ print "not ok $testnum # this is an invalid file, we succeeded\n";
+ }
+ else {
+ print "ok $testnum # ",Imager::_error_as_msg(),"\n";
+ }
+ close FH;
+}
+