Description: <short summary of the patch>
 TODO: Put a short summary on the line above and replace this paragraph
 with a longer explanation of this change. Complete the meta-information
 with other relevant fields (see below for details). To make it easier, the
 information below has been extracted from the changelog. Adjust it or drop
 it.
 .
 grafx2 (2.6-2) unstable; urgency=medium
 .
   * Patch src/fileformats to fix png saving.
Author: Gürkan Myczko <gurkan@phys.ethz.ch>

---
The information above should follow the Patch Tagging Guidelines, please
checkout http://dep.debian.net/deps/dep3/ to learn about the format. Here
are templates for supplementary fields that you might want to add:

Origin: <vendor|upstream|other>, <url of original patch>
Bug: <url in upstream bugtracker>
Bug-Debian: https://bugs.debian.org/<bugnumber>
Bug-Ubuntu: https://launchpad.net/bugs/<bugnumber>
Forwarded: <no|not-needed|url proving that it has been forwarded>
Reviewed-By: <name and email of someone who approved the patch>
Last-Update: 2019-01-21

--- grafx2-2.6.orig/src/fileformats.c
+++ grafx2-2.6/src/fileformats.c
@@ -6798,6 +6798,7 @@ void Save_PNG_Sub(T_IO_Context * context
   byte cycle_data[16*6]; // Storage for color-cycling data, referenced by crng_chunk
   struct PNG_memory_buffer memory_buffer;
 
+  memset(&memory_buffer, 0, sizeof(memory_buffer));
   /* initialisation */
   if ((png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL))
       && (info_ptr = png_create_info_struct(png_ptr)))
@@ -6806,14 +6807,10 @@ void Save_PNG_Sub(T_IO_Context * context
     {
       if (file != NULL)
         png_init_io(png_ptr, file);
-      else
-      {
-        // to write to memory, use png_set_write_fn() instead of calling png_init_io()
-        memset(&memory_buffer, 0, sizeof(memory_buffer));
+      else // to write to memory, use png_set_write_fn() instead of calling png_init_io()
         png_set_write_fn(png_ptr, &memory_buffer, PNG_memory_write, PNG_memory_flush);
-      }
-      
-      /* en-tete */
+
+      /* read PNG header */
       if (!setjmp(png_jmpbuf(png_ptr)))
       {
         png_set_IHDR(png_ptr, info_ptr, context->Width, context->Height,
@@ -6822,8 +6819,7 @@ void Save_PNG_Sub(T_IO_Context * context
 
         png_set_PLTE(png_ptr, info_ptr, (png_colorp)context->Palette, 256);
         {
-          // Commentaires texte PNG
-          // Cette partie est optionnelle
+          // text chunks in PNG (optional)
           png_text text_ptr[2] = {
 #ifdef PNG_iTXt_SUPPORTED
             {-1, "Software", "Grafx2", 6, 0, NULL, NULL},
@@ -6960,11 +6956,14 @@ void Save_PNG_Sub(T_IO_Context * context
 
   if (Row_pointers)
     free(Row_pointers);
-  if (memory_buffer.buffer)
+  if (File_error == 0 && buffer != NULL)
   {
     *buffer = memory_buffer.buffer;
-    *buffer_size = memory_buffer.offset;
+    if (buffer_size != NULL)
+      *buffer_size = memory_buffer.offset;
   }
+  else
+    free(memory_buffer.buffer);
 }
 
 
