gif2webp: fix crash on open failure with libgif5

output the current gif_error and use it instead of the NULL context to
retrieve an error message.

Change-Id: Ib761ebd36b3ddd6288375de46e671022d2381083
This commit is contained in:
James Zern 2013-03-17 18:06:39 -07:00
parent 9acb17dea4
commit 59423a2486

View File

@ -132,23 +132,24 @@ static int GetColorFromIndex(const ColorMapObject* const color_map, GifWord idx,
} }
} }
static void DisplayGifError(const GifFileType* const gif) { static void DisplayGifError(const GifFileType* const gif, int gif_error) {
// GIFLIB_MAJOR is only defined in libgif >= 4.2.0. // GIFLIB_MAJOR is only defined in libgif >= 4.2.0.
// libgif 4.2.0 has retired PrintGifError() and added GifErrorString(). // libgif 4.2.0 has retired PrintGifError() and added GifErrorString().
#if defined(GIFLIB_MAJOR) && defined(GIFLIB_MINOR) && \ #if defined(GIFLIB_MAJOR) && defined(GIFLIB_MINOR) && \
((GIFLIB_MAJOR == 4 && GIFLIB_MINOR >= 2) || GIFLIB_MAJOR > 4) ((GIFLIB_MAJOR == 4 && GIFLIB_MINOR >= 2) || GIFLIB_MAJOR > 4)
#if GIFLIB_MAJOR >= 5 #if GIFLIB_MAJOR >= 5
// Static string actually, hence the const char* cast. // Static string actually, hence the const char* cast.
const char* error_str = (const char*)GifErrorString(gif->Error); const char* error_str = (const char*)GifErrorString(
(gif == NULL) ? gif_error : gif->Error);
#else #else
const char* error_str = (const char*)GifErrorString(); const char* error_str = (const char*)GifErrorString();
(void)gif; (void)gif;
#endif #endif
if (error_str == NULL) error_str = "Unknown error"; if (error_str == NULL) error_str = "Unknown error";
fprintf(stderr, "GIFLib Error: %s\n", error_str); fprintf(stderr, "GIFLib Error %d: %s\n", gif_error, error_str);
#else #else
(void)gif; (void)gif;
fprintf(stderr, "GIFLib Error: "); fprintf(stderr, "GIFLib Error %d: ", gif_error);
PrintGifError(); PrintGifError();
fprintf(stderr, "\n"); fprintf(stderr, "\n");
#endif #endif
@ -186,7 +187,7 @@ static void Help(void) {
int main(int argc, const char *argv[]) { int main(int argc, const char *argv[]) {
int verbose = 0; int verbose = 0;
int gif_error = -1; int gif_error = GIF_ERROR;
WebPMuxError err = WEBP_MUX_OK; WebPMuxError err = WEBP_MUX_OK;
int ok = 0; int ok = 0;
const char *in_file = NULL, *out_file = NULL; const char *in_file = NULL, *out_file = NULL;
@ -458,7 +459,7 @@ int main(int argc, const char *argv[]) {
// All OK. // All OK.
ok = 1; ok = 1;
gif_error = 0; gif_error = GIF_OK;
End: End:
WebPDataClear(&webp_data); WebPDataClear(&webp_data);
@ -466,8 +467,8 @@ int main(int argc, const char *argv[]) {
WebPPictureFree(&picture); WebPPictureFree(&picture);
if (out != NULL && out_file != NULL) fclose(out); if (out != NULL && out_file != NULL) fclose(out);
if (gif_error != 0) { if (gif_error != GIF_OK) {
DisplayGifError(gif); DisplayGifError(gif, gif_error);
} }
if (gif != NULL) { if (gif != NULL) {
DGifCloseFile(gif); DGifCloseFile(gif);