Index: frmts/png/pngdataset.cpp
===================================================================
--- frmts/png/pngdataset.cpp	(révision 12105)
+++ frmts/png/pngdataset.cpp	(copie de travail)
@@ -451,7 +451,7 @@
 
     hPNG = png_create_read_struct( PNG_LIBPNG_VER_STRING, this, NULL, NULL );
 
-    png_set_error_fn( hPNG, this, png_gdal_error, png_gdal_warning );
+    png_set_error_fn( hPNG, &sSetJmpContext, png_gdal_error, png_gdal_warning );
     if( setjmp( sSetJmpContext ) != 0 )
         return;
 
@@ -704,7 +704,7 @@
 /* -------------------------------------------------------------------- */
 /*      Setup error handling.                                           */
 /* -------------------------------------------------------------------- */
-    png_set_error_fn( poDS->hPNG, poDS, png_gdal_error, png_gdal_warning );
+    png_set_error_fn( poDS->hPNG, &poDS->sSetJmpContext, png_gdal_error, png_gdal_warning );
 
     if( setjmp( poDS->sSetJmpContext ) != 0 )
         return NULL;
@@ -972,10 +972,18 @@
     png_structp hPNG;
     png_infop   psPNGInfo;
     
+    jmp_buf     sSetJmpContext;
     hPNG = png_create_write_struct( PNG_LIBPNG_VER_STRING, 
-                                    NULL, NULL, NULL );
+                                    &sSetJmpContext, png_gdal_error, png_gdal_warning );
     psPNGInfo = png_create_info_struct( hPNG );
-    
+
+    if( setjmp( sSetJmpContext ) != 0 )
+    {
+        VSIFCloseL( fpImage );
+        png_destroy_write_struct( &hPNG, &psPNGInfo );
+        return NULL;
+    }
+
     png_set_write_fn( hPNG, fpImage, png_vsi_write_data, png_vsi_flush );
 
     png_set_IHDR( hPNG, psPNGInfo, nXSize, nYSize, 
@@ -1240,9 +1248,11 @@
     // libpng is generally not built as C++ and so won't honour unwind
     // semantics.  Ugg. 
 
-    PNGDataset *poDS = (PNGDataset *) png_ptr->error_ptr;
-
-    longjmp( poDS->sSetJmpContext, 1 );
+    jmp_buf* psSetJmpContext = (jmp_buf*) png_ptr->error_ptr;
+    if (psSetJmpContext)
+    {
+        longjmp( *psSetJmpContext, 1 );
+    }
 }
 
 /************************************************************************/

