From eec7b03faddad6ae6acf11f9ca737035722fcb65 Mon Sep 17 00:00:00 2001 From: Stephan Bergmann Date: Fri, 16 Oct 2015 16:57:38 +0200 Subject: -fsanitize=shift-exponent Through test code added with d30f5bc3e65463f28c3087acad6f88e12d60e53b "lok::Document::paintTile: fix non-rectangular tiles wrt. transparency," CppunitTest_desktop_lib started to exhibit > basebmp/inc/metafunctions.hxx:210:26: runtime error: shift exponent 32 is too large for 32-bit type 'unsigned int' > unsigned int basebmp::shiftRight(unsigned int, int) basebmp/inc/metafunctions.hxx:210:26 > basebmp::RGBMaskGetter::operator()(unsigned int) const basebmp/inc/rgbmaskpixelformats.hxx:123:25 > basebmp::Color basebmp::UnaryFunctionAccessorAdapter, basebmp::RGBMaskGetter, basebmp::RGBMaskSetter >::operator() >(basebmp::PixelIterator const&) const basebmp/inc/accessoradapters.hxx:115:16 > basebmp::(anonymous namespace)::BitmapRenderer, basebmp::StandardAccessor, basebmp::AccessorSelector, basebmp::RGBMaskSetter >, basebmp::StdMasks>::getPixel_i(basegfx::B2IPoint const&) basebmp/source/bitmapdevice.cxx:472:20 > basebmp::BitmapDevice::getPixel(basegfx::B2IPoint const&) basebmp/source/bitmapdevice.cxx:1289:16 > basebmp::Color basebmp::GenericColorImageAccessor::operator() > >(vigra::IteratorAdaptor > const&) const basebmp/inc/genericcolorimageaccessor.hxx:54:18 > void vigra::copyLine >, basebmp::GenericColorImageAccessor, unsigned int*, basebmp::UnaryFunctionAccessorAdapter, basebmp::RGBMaskGetter, basebmp::RGBMaskSetter > >(vigra::IteratorAdaptor >, vigra::IteratorAdaptor >, basebmp::GenericColorImageAccessor, unsigned int*, basebmp::UnaryFunctionAccessorAdapter, basebmp::RGBMaskGetter, basebmp::RGBMaskSetter >) workdir/UnpackedTarball/vigra/include/vigra/copyimage.hxx:65:18 > void vigra::copyImage, basebmp::UnaryFunctionAccessorAdapter, basebmp::RGBMaskGetter, basebmp::RGBMaskSetter > >(vigra::Diff2D, vigra::Diff2D, basebmp::GenericColorImageAccessor, basebmp::PixelIterator, basebmp::UnaryFunctionAccessorAdapter, basebmp::RGBMaskGetter, basebmp::RGBMaskSetter >) workdir/UnpackedTarball/vigra/include/vigra/copyimage.hxx:158:9 > void basebmp::scaleImage, basebmp::UnaryFunctionAccessorAdapter, basebmp::RGBMaskGetter, basebmp::RGBMaskSetter > >(vigra::Diff2D, vigra::Diff2D, basebmp::GenericColorImageAccessor, basebmp::PixelIterator, basebmp::PixelIterator, basebmp::UnaryFunctionAccessorAdapter, basebmp::RGBMaskGetter, basebmp::RGBMaskSetter >, bool) basebmp/inc/scaleimage.hxx:133:9 > void basebmp::scaleImage, basebmp::UnaryFunctionAccessorAdapter, basebmp::RGBMaskGetter, basebmp::RGBMaskSetter > >(vigra::triple const&, vigra::triple, basebmp::PixelIterator, basebmp::UnaryFunctionAccessorAdapter, basebmp::RGBMaskGetter, basebmp::RGBMaskSetter > > const&, bool) basebmp/inc/scaleimage.hxx:180:5 > void basebmp::(anonymous namespace)::BitmapRenderer, basebmp::StandardAccessor, basebmp::AccessorSelector, basebmp::RGBMaskSetter >, basebmp::StdMasks>::implDrawBitmapGeneric, basebmp::UnaryFunctionAccessorAdapter, basebmp::RGBMaskGetter, basebmp::RGBMaskSetter > >(std::shared_ptr const&, basegfx::B2IBox const&, basegfx::B2IBox const&, basebmp::PixelIterator const&, basebmp::UnaryFunctionAccessorAdapter, basebmp::RGBMaskGetter, basebmp::RGBMaskSetter > const&) basebmp/source/bitmapdevice.cxx:739:13 > basebmp::(anonymous namespace)::BitmapRenderer, basebmp::StandardAccessor, basebmp::AccessorSelector, basebmp::RGBMaskSetter >, basebmp::StdMasks>::drawBitmap_i(std::shared_ptr const&, basegfx::B2IBox const&, basegfx::B2IBox const&, basebmp::DrawMode) basebmp/source/bitmapdevice.cxx:827:21 > basebmp::BitmapDevice::drawBitmap(std::shared_ptr const&, basegfx::B2IBox const&, basegfx::B2IBox const&, basebmp::DrawMode) basebmp/source/bitmapdevice.cxx:1562:9 > basebmp::BitmapDevice::drawBitmap(std::shared_ptr const&, basegfx::B2IBox const&, basegfx::B2IBox const&, basebmp::DrawMode, std::shared_ptr const&) basebmp/source/bitmapdevice.cxx:1574:9 > SvpSalGraphics::copyBits(SalTwoRect const&, SalGraphics*) vcl/headless/svpgdi.cxx:687:9 > SalGraphics::CopyBits(SalTwoRect const&, SalGraphics*, OutputDevice const*, OutputDevice const*) vcl/source/gdi/salgdilayout.cxx:589:9 > OutputDevice::GetBitmap(Point const&, Size const&) const vcl/source/outdev/bitmap.cxx:443:29 > OutputDevice::GetBitmapEx(Point const&, Size const&) const vcl/source/outdev/bitmap.cxx:487:25 > OutputDevice::DrawOutDev(Point const&, Size const&, Point const&, Size const&, OutputDevice const&) vcl/source/outdev/outdev.cxx:587:47 > sdr::overlay::OverlayManagerBuffered::ImpSaveBackground(vcl::Region const&, OutputDevice*) svx/source/sdr/overlay/overlaymanagerbuffered.cxx:198:17 > sdr::overlay::OverlayManagerBuffered::completeRedraw(vcl::Region const&, OutputDevice*) const svx/source/sdr/overlay/overlaymanagerbuffered.cxx:418:17 > (anonymous namespace)::RenderContextGuard::~RenderContextGuard() sw/source/core/view/viewsh.cxx:1714:21 > SwViewShell::Paint(OutputDevice&, Rectangle const&) sw/source/core/view/viewsh.cxx:1856:1 > SwCrsrShell::Paint(OutputDevice&, Rectangle const&) sw/source/core/crsr/crsrsh.cxx:1278:5 > SwViewShell::PaintTile(VirtualDevice&, int, int, int, int, long, long) sw/source/core/view/viewsh.cxx:1901:5 > SwXTextDocument::paintTile(VirtualDevice&, int, int, int, int, long, long) sw/source/uibase/uno/unotxdoc.cxx:3139:5 > non-virtual thunk to SwXTextDocument::paintTile(VirtualDevice&, int, int, int, int, long, long) sw/source/uibase/uno/unotxdoc.cxx:3132:23 > doc_paintTile desktop/source/lib/init.cxx:784:5 > DesktopLOKTest::testPaintTile() desktop/qa/desktop_lib/test_desktop_lib.cxx:299:5 Change-Id: Iadda5d7bb3f7f15706d16b813e11e6d145818f6c --- basebmp/inc/metafunctions.hxx | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'basebmp') diff --git a/basebmp/inc/metafunctions.hxx b/basebmp/inc/metafunctions.hxx index 16ee2615f4d7..4e6efb44413f 100644 --- a/basebmp/inc/metafunctions.hxx +++ b/basebmp/inc/metafunctions.hxx @@ -25,6 +25,8 @@ #include #include +#include +#include namespace basebmp { @@ -207,7 +209,15 @@ template< typename T > inline T shiftLeft( T v, int shift ) /// Shift right for positive shift value, and left otherwise template< typename T > inline T shiftRight( T v, int shift ) { - return shift > 0 ? v >> shift : v << (-shift); + // Avoid undefined behavior and only shift by less than the length in bits + // of the promoted left operand: + static_assert( + std::is_unsigned::value, + "must be unsigned for promotedBits and the below ': 0' to be correct"); + auto const promotedBits = std::numeric_limits::digits; + return shift >= 0 + ? shift < promotedBits ? v >> shift : 0 + : -shift < promotedBits ? v << (-shift) : 0; } } // namespace basebmp -- cgit