[Libreoffice-commits] core.git: vcl/headless vcl/unx

Noel Grandin noel.grandin at collabora.co.uk
Fri Jun 2 11:28:52 UTC 2017


 vcl/headless/svpbmp.cxx        |    5 -
 vcl/unx/generic/gdi/salbmp.cxx |  165 +++++++++++++++++++----------------------
 2 files changed, 81 insertions(+), 89 deletions(-)

New commits:
commit 5c0cee64b9430da404fa52988871fae6a5dd9f41
Author: Noel Grandin <noel.grandin at collabora.co.uk>
Date:   Fri Jun 2 11:04:41 2017 +0200

    no need to assign a nullptr after a bad_alloc
    
    since if the allocation failed, the assignment would never happen
    
    Also use early exit to simplify the code in X11SalBitmap::ImplCreateDIB
    
    Change-Id: I73301bfad6492c2b42c08744c24cfb78237983f0
    Reviewed-on: https://gerrit.libreoffice.org/38346
    Tested-by: Jenkins <ci at libreoffice.org>
    Reviewed-by: Noel Grandin <noel.grandin at collabora.co.uk>

diff --git a/vcl/headless/svpbmp.cxx b/vcl/headless/svpbmp.cxx
index 5be976fc9e74..15c6fc9c1022 100644
--- a/vcl/headless/svpbmp.cxx
+++ b/vcl/headless/svpbmp.cxx
@@ -64,11 +64,8 @@ BitmapBuffer* ImplCreateDIB(
     }
     catch (const std::bad_alloc&)
     {
-        pDIB = nullptr;
-    }
-
-    if(!pDIB)
         return nullptr;
+    }
 
     const sal_uInt16 nColors = ( nBitCount <= 8 ) ? ( 1 << nBitCount ) : 0;
 
diff --git a/vcl/unx/generic/gdi/salbmp.cxx b/vcl/unx/generic/gdi/salbmp.cxx
index 00043d5e783e..8401fa8e3b24 100644
--- a/vcl/unx/generic/gdi/salbmp.cxx
+++ b/vcl/unx/generic/gdi/salbmp.cxx
@@ -135,105 +135,100 @@ BitmapBuffer* X11SalBitmap::ImplCreateDIB(
 
     BitmapBuffer* pDIB = nullptr;
 
-    if( rSize.Width() && rSize.Height() )
+    if( !rSize.Width() || !rSize.Height() )
+        return nullptr;
+
+    try
     {
-        try
-        {
-            pDIB = new BitmapBuffer;
-        }
-        catch (const std::bad_alloc&)
-        {
-            pDIB = nullptr;
-        }
+        pDIB = new BitmapBuffer;
+    }
+    catch (const std::bad_alloc&)
+    {
+        return nullptr;
+    }
 
-        if( pDIB )
-        {
-            const sal_uInt16 nColors = ( nBitCount <= 8 ) ? ( 1 << nBitCount ) : 0;
+    const sal_uInt16 nColors = ( nBitCount <= 8 ) ? ( 1 << nBitCount ) : 0;
 
-            pDIB->mnFormat = ScanlineFormat::NONE;
+    pDIB->mnFormat = ScanlineFormat::NONE;
 
-            switch( nBitCount )
-            {
-                case 1: pDIB->mnFormat |= ScanlineFormat::N1BitMsbPal; break;
-                case 4: pDIB->mnFormat |= ScanlineFormat::N4BitMsnPal; break;
-                case 8: pDIB->mnFormat |= ScanlineFormat::N8BitPal; break;
+    switch( nBitCount )
+    {
+        case 1: pDIB->mnFormat |= ScanlineFormat::N1BitMsbPal; break;
+        case 4: pDIB->mnFormat |= ScanlineFormat::N4BitMsnPal; break;
+        case 8: pDIB->mnFormat |= ScanlineFormat::N8BitPal; break;
 #ifdef OSL_BIGENDIAN
-                case 16:
-                {
-                    pDIB->mnFormat|= ScanlineFormat::N16BitTcMsbMask;
-                    ColorMaskElement aRedMask(0xf800);
-                    aRedMask.CalcMaskShift();
-                    ColorMaskElement aGreenMask(0x07e0);
-                    aGreenMask.CalcMaskShift();
-                    ColorMaskElement aBlueMask(0x001f);
-                    aBlueMask.CalcMaskShift();
-                    pDIB->maColorMask = ColorMask(aRedMask, aGreenMask, aBlueMask);
-                    break;
-                }
+        case 16:
+        {
+            pDIB->mnFormat|= ScanlineFormat::N16BitTcMsbMask;
+            ColorMaskElement aRedMask(0xf800);
+            aRedMask.CalcMaskShift();
+            ColorMaskElement aGreenMask(0x07e0);
+            aGreenMask.CalcMaskShift();
+            ColorMaskElement aBlueMask(0x001f);
+            aBlueMask.CalcMaskShift();
+            pDIB->maColorMask = ColorMask(aRedMask, aGreenMask, aBlueMask);
+            break;
+        }
 #else
-                case 16:
-                {
-                    pDIB->mnFormat|= ScanlineFormat::N16BitTcLsbMask;
-                    ColorMaskElement aRedMask(0xf800);
-                    aRedMask.CalcMaskShift();
-                    ColorMaskElement aGreenMask(0x07e0);
-                    aGreenMask.CalcMaskShift();
-                    ColorMaskElement aBlueMask(0x001f);
-                    aBlueMask.CalcMaskShift();
-                    pDIB->maColorMask = ColorMask(aRedMask, aGreenMask, aBlueMask);
-                    break;
-                }
+        case 16:
+        {
+            pDIB->mnFormat|= ScanlineFormat::N16BitTcLsbMask;
+            ColorMaskElement aRedMask(0xf800);
+            aRedMask.CalcMaskShift();
+            ColorMaskElement aGreenMask(0x07e0);
+            aGreenMask.CalcMaskShift();
+            ColorMaskElement aBlueMask(0x001f);
+            aBlueMask.CalcMaskShift();
+            pDIB->maColorMask = ColorMask(aRedMask, aGreenMask, aBlueMask);
+            break;
+        }
 #endif
-                default:
-                    nBitCount = 24;
-                    SAL_FALLTHROUGH;
-                case 24:
-                    pDIB->mnFormat |= ScanlineFormat::N24BitTcBgr;
-                break;
-            }
+        default:
+            nBitCount = 24;
+            SAL_FALLTHROUGH;
+        case 24:
+            pDIB->mnFormat |= ScanlineFormat::N24BitTcBgr;
+        break;
+    }
 
-            pDIB->mnWidth = rSize.Width();
-            pDIB->mnHeight = rSize.Height();
-            long nScanlineBase;
-            bool bFail = o3tl::checked_multiply<long>(pDIB->mnWidth, nBitCount, nScanlineBase);
-            if (bFail)
-            {
-                SAL_WARN("vcl.gdi", "checked multiply failed");
-                delete pDIB;
-                return nullptr;
-            }
-            pDIB->mnScanlineSize = AlignedWidth4Bytes(nScanlineBase);
-            if (pDIB->mnScanlineSize < nScanlineBase/8)
-            {
-                SAL_WARN("vcl.gdi", "scanline calculation wraparound");
-                delete pDIB;
-                return nullptr;
-            }
-            pDIB->mnBitCount = nBitCount;
+    pDIB->mnWidth = rSize.Width();
+    pDIB->mnHeight = rSize.Height();
+    long nScanlineBase;
+    bool bFail = o3tl::checked_multiply<long>(pDIB->mnWidth, nBitCount, nScanlineBase);
+    if (bFail)
+    {
+        SAL_WARN("vcl.gdi", "checked multiply failed");
+        delete pDIB;
+        return nullptr;
+    }
+    pDIB->mnScanlineSize = AlignedWidth4Bytes(nScanlineBase);
+    if (pDIB->mnScanlineSize < nScanlineBase/8)
+    {
+        SAL_WARN("vcl.gdi", "scanline calculation wraparound");
+        delete pDIB;
+        return nullptr;
+    }
+    pDIB->mnBitCount = nBitCount;
 
-            if( nColors )
-            {
-                pDIB->maPalette = rPal;
-                pDIB->maPalette.SetEntryCount( nColors );
-            }
+    if( nColors )
+    {
+        pDIB->maPalette = rPal;
+        pDIB->maPalette.SetEntryCount( nColors );
+    }
 
-            try
-            {
-                pDIB->mpBits = new sal_uInt8[ pDIB->mnScanlineSize * pDIB->mnHeight ];
+    try
+    {
+        pDIB->mpBits = new sal_uInt8[ pDIB->mnScanlineSize * pDIB->mnHeight ];
 #if defined HAVE_VALGRIND_HEADERS
-                if (RUNNING_ON_VALGRIND)
-                    blankExtraSpace(pDIB);
+        if (RUNNING_ON_VALGRIND)
+            blankExtraSpace(pDIB);
 #endif
-            }
-            catch (const std::bad_alloc&)
-            {
-                delete pDIB;
-                pDIB = nullptr;
-            }
-        }
     }
-    else
+    catch (const std::bad_alloc&)
+    {
+        delete pDIB;
         pDIB = nullptr;
+    }
 
     return pDIB;
 }


More information about the Libreoffice-commits mailing list