From 7242de68569b8c927d4f59e430935158e73c75a7 Mon Sep 17 00:00:00 2001 From: kervala Date: Tue, 29 Nov 2016 20:28:19 +0100 Subject: [PATCH] Fixed: GCC warning "variable might be clobbered by longjmp" --- code/nel/src/misc/bitmap_png.cpp | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/code/nel/src/misc/bitmap_png.cpp b/code/nel/src/misc/bitmap_png.cpp index 459437f5c..251fd5495 100644 --- a/code/nel/src/misc/bitmap_png.cpp +++ b/code/nel/src/misc/bitmap_png.cpp @@ -91,7 +91,7 @@ uint8 CBitmap::readPNG( NLMISC::IStream &f ) // free all of the memory associated with the png_ptr and info_ptr png_destroy_read_struct(&png_ptr, &info_ptr, NULL); // if we get here, we had a problem reading the file - nlwarning("failed to setjump"); + nlwarning("Error while reading PNG"); return 0; } @@ -241,6 +241,21 @@ uint8 CBitmap::readPNG( NLMISC::IStream &f ) return imageDepth; } +// small helper to avoid local variables +static bool writePNGSetJmp(png_struct *png_ptr) +{ + if (setjmp(png_jmpbuf(png_ptr))) + { + // free all of the memory associated with the png_ptr + png_destroy_write_struct(&png_ptr, (png_info**)NULL); + // if we get here, we had a problem writing the file + nlwarning("Error while writing PNG"); + return false; + } + + return true; +} + /*-------------------------------------------------------------------*\ writePNG \*-------------------------------------------------------------------*/ @@ -274,12 +289,7 @@ bool CBitmap::writePNG( NLMISC::IStream &f, uint32 d) return false; } - if (setjmp(png_jmpbuf(png_ptr))) - { - png_destroy_write_struct( &png_ptr, (png_info**)NULL ); - nlwarning("couldn't set setjmp"); - return false; - } + if (!writePNGSetJmp(png_ptr)) return false; // set the write function png_set_write_fn(png_ptr, (void*)&f, writePNGData, NULL);