Skip to content

Commit

Permalink
avoid re-entrancy into giflib using the mutex API
Browse files Browse the repository at this point in the history
  • Loading branch information
tonycoz committed Aug 17, 2012
1 parent b53b3d7 commit 233a54d
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 5 deletions.
2 changes: 1 addition & 1 deletion GIF/GIF.pm
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use Imager;
use vars qw($VERSION @ISA);

BEGIN {
$VERSION = "0.84";
$VERSION = "0.85";

require XSLoader;
XSLoader::load('Imager::File::GIF', $VERSION);
Expand Down
1 change: 1 addition & 0 deletions GIF/GIF.xs
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,4 @@ i_readgif_multi_wiol(ig)
BOOT:
PERL_INITIALIZE_IMAGER_CALLBACKS;
PERL_INITIALIZE_IMAGER_PERL_CALLBACKS;
i_init_gif();
44 changes: 40 additions & 4 deletions GIF/imgif.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ static int
InterlacedJumps[] = { 8, 8, 4, 2 }; /* be read - offsets and jumps... */


static i_mutex_t mutex;

void
i_init_gif(void) {
mutex = i_mutex_create();
}

static
void
Expand Down Expand Up @@ -846,17 +852,25 @@ static int io_glue_read_cb(GifFileType *gft, GifByteType *buf, int length);
i_img **
i_readgif_multi_wiol(io_glue *ig, int *count) {
GifFileType *GifFile;

i_img **result;

i_mutex_lock(mutex);

i_clear_error();

if ((GifFile = DGifOpen((void *)ig, io_glue_read_cb )) == NULL) {
gif_push_error();
i_push_error(0, "Cannot create giflib callback object");
mm_log((1,"i_readgif_multi_wiol: Unable to open callback datasource.\n"));
i_mutex_unlock(mutex);
return NULL;
}

return i_readgif_multi_low(GifFile, count, -1);
result = i_readgif_multi_low(GifFile, count, -1);

i_mutex_unlock(mutex);

return result;
}

static int
Expand All @@ -869,17 +883,25 @@ io_glue_read_cb(GifFileType *gft, GifByteType *buf, int length) {
i_img *
i_readgif_wiol(io_glue *ig, int **color_table, int *colors) {
GifFileType *GifFile;
i_img *result;

i_mutex_lock(mutex);

i_clear_error();

if ((GifFile = DGifOpen((void *)ig, io_glue_read_cb )) == NULL) {
gif_push_error();
i_push_error(0, "Cannot create giflib callback object");
mm_log((1,"i_readgif_wiol: Unable to open callback datasource.\n"));
i_mutex_unlock(mutex);
return NULL;
}

return i_readgif_low(GifFile, color_table, colors);
result = i_readgif_low(GifFile, color_table, colors);

i_mutex_unlock(mutex);

return result;
}

/*
Expand Down Expand Up @@ -924,21 +946,28 @@ Returns NULL if the page isn't found.
i_img *
i_readgif_single_wiol(io_glue *ig, int page) {
GifFileType *GifFile;
i_img *result;

i_clear_error();
if (page < 0) {
i_push_error(0, "page must be non-negative");
return NULL;
}

i_mutex_lock(mutex);

if ((GifFile = DGifOpen((void *)ig, io_glue_read_cb )) == NULL) {
gif_push_error();
i_push_error(0, "Cannot create giflib callback object");
mm_log((1,"i_readgif_wiol: Unable to open callback datasource.\n"));
return NULL;
}

return i_readgif_single_low(GifFile, page);
result = i_readgif_single_low(GifFile, page);

i_mutex_unlock(mutex);

return result;
}

/*
Expand Down Expand Up @@ -1797,6 +1826,8 @@ i_writegif_wiol(io_glue *ig, i_quantize *quant, i_img **imgs,
GifFileType *GifFile;
int result;

i_mutex_lock(mutex);

i_clear_error();

gif_set_version(quant, imgs, count);
Expand All @@ -1805,11 +1836,14 @@ i_writegif_wiol(io_glue *ig, i_quantize *quant, i_img **imgs,
gif_push_error();
i_push_error(0, "Cannot create giflib callback object");
mm_log((1,"i_writegif_wiol: Unable to open callback datasource.\n"));
i_mutex_unlock(mutex);
return 0;
}

result = i_writegif_low(quant, GifFile, imgs, count);

i_mutex_unlock(mutex);

if (i_io_close(ig))
return 0;

Expand Down Expand Up @@ -1945,6 +1979,8 @@ EGifSetGifVersion(). See L<gif_set_version> for an explanation.
Arnar M. Hrafnkelsson, addi@umich.edu
Tony Cook <tonyc@cpan.org>
=head1 SEE ALSO
perl(1), Imager(3)
Expand Down
1 change: 1 addition & 0 deletions GIF/imgif.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "imext.h"

void i_init_gif(void);
double i_giflib_version(void);
i_img *i_readgif_wiol(io_glue *ig, int **colour_table, int *colours);
i_img *i_readgif_single_wiol(io_glue *ig, int page);
Expand Down

0 comments on commit 233a54d

Please sign in to comment.