vcl/source/filter/png/PngImageReader.cxx |   31 +++++++++----------------------
 1 file changed, 9 insertions(+), 22 deletions(-)

New commits:
commit 272a639bd8ea7c85d6f12dc2e217e79280a1a196
Author:     Luboš Luňák <l.lu...@collabora.com>
AuthorDate: Thu Apr 15 16:33:11 2021 +0200
Commit:     Luboš Luňák <l.lu...@collabora.com>
CommitDate: Thu Apr 15 21:14:25 2021 +0200

    automatic png_struct cleanup
    
    Change-Id: I77f3c5070b4481e4c349bac51ddc55218cd0654b
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/114164
    Tested-by: Jenkins
    Reviewed-by: Luboš Luňák <l.lu...@collabora.com>

diff --git a/vcl/source/filter/png/PngImageReader.cxx 
b/vcl/source/filter/png/PngImageReader.cxx
index 7c32188c22ba..b3a2265c23bf 100644
--- a/vcl/source/filter/png/PngImageReader.cxx
+++ b/vcl/source/filter/png/PngImageReader.cxx
@@ -58,6 +58,13 @@ bool isPng(SvStream& rStream)
     return png_sig_cmp(aHeader, 0, PNG_SIGNATURE_SIZE) == 0;
 }
 
+struct PngDestructor
+{
+    ~PngDestructor() { png_destroy_read_struct(&pPng, &pInfo, nullptr); }
+    png_structp pPng;
+    png_infop pInfo;
+};
+
 bool reader(SvStream& rStream, BitmapEx& rBitmapEx, bool bUseBitmap32)
 {
     if (!isPng(rStream))
@@ -74,6 +81,8 @@ bool reader(SvStream& rStream, BitmapEx& rBitmapEx, bool 
bUseBitmap32)
         return false;
     }
 
+    PngDestructor pngDestructor = { pPng, pInfo };
+
     // All variables holding resources need to be declared here in order to be
     // properly cleaned up in case of an error, otherwise libpng's longjmp()
     // jumps over the destructor calls.
@@ -86,7 +95,6 @@ bool reader(SvStream& rStream, BitmapEx& rBitmapEx, bool 
bUseBitmap32)
 
     if (setjmp(png_jmpbuf(pPng)))
     {
-        png_destroy_read_struct(&pPng, &pInfo, nullptr);
         // Set the bitmap if it contains something, even on failure. This 
allows
         // reading images that are only partially broken.
         pWriteAccess.reset();
@@ -126,10 +134,7 @@ bool reader(SvStream& rStream, BitmapEx& rBitmapEx, bool 
bUseBitmap32)
                                            &interlace, nullptr, nullptr);
 
     if (returnValue != 1)
-    {
-        png_destroy_read_struct(&pPng, &pInfo, nullptr);
         return false;
-    }
 
     if (colorType == PNG_COLOR_TYPE_PALETTE)
         png_set_palette_to_rgb(pPng);
@@ -163,16 +168,12 @@ bool reader(SvStream& rStream, BitmapEx& rBitmapEx, bool 
bUseBitmap32)
                                nullptr, nullptr);
 
     if (returnValue != 1)
-    {
-        png_destroy_read_struct(&pPng, &pInfo, nullptr);
         return false;
-    }
 
     if (bitDepth != 8
         || (colorType != PNG_COLOR_TYPE_RGB && colorType != 
PNG_COLOR_TYPE_RGB_ALPHA
             && colorType != PNG_COLOR_TYPE_GRAY))
     {
-        png_destroy_read_struct(&pPng, &pInfo, nullptr);
         return false;
     }
 
@@ -192,10 +193,7 @@ bool reader(SvStream& rStream, BitmapEx& rBitmapEx, bool 
bUseBitmap32)
         aBitmap = Bitmap(Size(width, height), vcl::PixelFormat::N24_BPP);
         pWriteAccess = BitmapScopedWriteAccess(aBitmap);
         if (!pWriteAccess)
-        {
-            png_destroy_read_struct(&pPng, &pInfo, nullptr);
             return false;
-        }
         ScanlineFormat eFormat = pWriteAccess->GetScanlineFormat();
         if (eFormat == ScanlineFormat::N24BitTcBgr)
             png_set_bgr(pPng);
@@ -220,10 +218,7 @@ bool reader(SvStream& rStream, BitmapEx& rBitmapEx, bool 
bUseBitmap32)
             aBitmap = Bitmap(Size(width, height), vcl::PixelFormat::N32_BPP);
             pWriteAccess = BitmapScopedWriteAccess(aBitmap);
             if (!pWriteAccess)
-            {
-                png_destroy_read_struct(&pPng, &pInfo, nullptr);
                 return false;
-            }
             ScanlineFormat eFormat = pWriteAccess->GetScanlineFormat();
             if (eFormat == ScanlineFormat::N32BitTcAbgr || eFormat == 
ScanlineFormat::N32BitTcBgra)
             {
@@ -277,10 +272,7 @@ bool reader(SvStream& rStream, BitmapEx& rBitmapEx, bool 
bUseBitmap32)
             aBitmapAlpha = AlphaMask(Size(width, height), nullptr);
             pWriteAccess = BitmapScopedWriteAccess(aBitmap);
             if (!pWriteAccess)
-            {
-                png_destroy_read_struct(&pPng, &pInfo, nullptr);
                 return false;
-            }
             ScanlineFormat eFormat = pWriteAccess->GetScanlineFormat();
             if (eFormat == ScanlineFormat::N24BitTcBgr)
                 png_set_bgr(pPng);
@@ -322,10 +314,7 @@ bool reader(SvStream& rStream, BitmapEx& rBitmapEx, bool 
bUseBitmap32)
         aBitmap.Erase(COL_WHITE);
         pWriteAccess = BitmapScopedWriteAccess(aBitmap);
         if (!pWriteAccess)
-        {
-            png_destroy_read_struct(&pPng, &pInfo, nullptr);
             return false;
-        }
 
         for (int pass = 0; pass < nNumberOfPasses; pass++)
         {
@@ -341,8 +330,6 @@ bool reader(SvStream& rStream, BitmapEx& rBitmapEx, bool 
bUseBitmap32)
 
     png_read_end(pPng, pInfo);
 
-    png_destroy_read_struct(&pPng, &pInfo, nullptr);
-
     if (!prefSize.IsEmpty())
     {
         rBitmapEx.SetPrefMapMode(MapMode(MapUnit::Map100thMM));
_______________________________________________
Libreoffice-commits mailing list
libreoffice-comm...@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits

Reply via email to