[Libreoffice-commits] core.git: filter/source include/o3tl

Stephan Bergmann (via logerrit) logerrit at kemper.freedesktop.org
Thu Jan 9 19:34:36 UTC 2020


 filter/source/graphicfilter/icgm/class1.cxx |    6 +++++-
 filter/source/graphicfilter/icgm/class4.cxx |    9 ++++++---
 include/o3tl/safeint.hxx                    |    8 ++++++++
 3 files changed, 19 insertions(+), 4 deletions(-)

New commits:
commit 6417668b3e12d9659ac5dc4a2f60aa8ad3bca675
Author:     Stephan Bergmann <sbergman at redhat.com>
AuthorDate: Thu Jan 9 19:43:23 2020 +0100
Commit:     Stephan Bergmann <sbergman at redhat.com>
CommitDate: Thu Jan 9 20:34:01 2020 +0100

    Introduce o3tl::make_unsigned to cast from signed to unsigned type
    
    ...without having to spell out a specific type to cast to (and also making it
    more obvious what the intend of such a cast is)
    
    Change-Id: Id9c68b856a4ee52e5a40d15dc9d83e95d1c231cd
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/86502
    Tested-by: Jenkins
    Reviewed-by: Stephan Bergmann <sbergman at redhat.com>

diff --git a/filter/source/graphicfilter/icgm/class1.cxx b/filter/source/graphicfilter/icgm/class1.cxx
index 2661931d7df2..30e28dd820cc 100644
--- a/filter/source/graphicfilter/icgm/class1.cxx
+++ b/filter/source/graphicfilter/icgm/class1.cxx
@@ -17,6 +17,10 @@
  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
  */
 
+#include <sal/config.h>
+
+#include <o3tl/safeint.hxx>
+
 #include "bundles.hxx"
 #include "cgm.hxx"
 #include "elements.hxx"
@@ -169,7 +173,7 @@ void CGM::ImplDoClass1()
             {
                 sal_uInt32 nSize = ImplGetUI(1);
 
-                if (static_cast<sal_uInt32>(mpEndValidSource - (mpSource + mnParaSize)) < nSize)
+                if (o3tl::make_unsigned(mpEndValidSource - (mpSource + mnParaSize)) < nSize)
                     throw css::uno::Exception("attempt to read past end of input", nullptr);
 
                 pElement->aFontList.InsertName( mpSource + mnParaSize, nSize );
diff --git a/filter/source/graphicfilter/icgm/class4.cxx b/filter/source/graphicfilter/icgm/class4.cxx
index 4760215ebe18..2e3f3c439cdb 100644
--- a/filter/source/graphicfilter/icgm/class4.cxx
+++ b/filter/source/graphicfilter/icgm/class4.cxx
@@ -23,6 +23,9 @@
 #include "chart.hxx"
 #include "elements.hxx"
 #include "outact.hxx"
+
+#include <o3tl/safeint.hxx>
+
 #include <math.h>
 #include <memory>
 
@@ -186,7 +189,7 @@ void CGM::ImplDoClass4()
                 sal_uInt32 nType = ImplGetUI16();
                 sal_uInt32 nSize = ImplGetUI( 1 );
 
-                if (static_cast<sal_uInt32>(mpEndValidSource - (mpSource + mnParaSize)) < nSize)
+                if (o3tl::make_unsigned(mpEndValidSource - (mpSource + mnParaSize)) < nSize)
                     throw css::uno::Exception("attempt to read past end of input", nullptr);
 
                 OUString aStr(reinterpret_cast<char*>(mpSource) + mnParaSize, nSize, RTL_TEXTENCODING_ASCII_US);
@@ -223,7 +226,7 @@ void CGM::ImplDoClass4()
                 sal_uInt32 nType = ImplGetUI16();
                 sal_uInt32 nSize = ImplGetUI(1);
 
-                if (static_cast<sal_uInt32>(mpEndValidSource - (mpSource + mnParaSize)) < nSize)
+                if (o3tl::make_unsigned(mpEndValidSource - (mpSource + mnParaSize)) < nSize)
                     throw css::uno::Exception("attempt to read past end of input", nullptr);
 
                 OUString aStr(reinterpret_cast<char*>(mpSource) + mnParaSize, nSize, RTL_TEXTENCODING_ASCII_US);
@@ -240,7 +243,7 @@ void CGM::ImplDoClass4()
                 (void)ImplGetUI16(); // nType
                 sal_uInt32 nSize = ImplGetUI( 1 );
 
-                if (static_cast<sal_uInt32>(mpEndValidSource - (mpSource + mnParaSize)) <= nSize)
+                if (o3tl::make_unsigned(mpEndValidSource - (mpSource + mnParaSize)) <= nSize)
                     throw css::uno::Exception("attempt to read past end of input", nullptr);
 
                 mpSource[ mnParaSize + nSize ] = 0;
diff --git a/include/o3tl/safeint.hxx b/include/o3tl/safeint.hxx
index ae28ca4b6570..6d8d1304fdf3 100644
--- a/include/o3tl/safeint.hxx
+++ b/include/o3tl/safeint.hxx
@@ -12,6 +12,7 @@
 
 #include <sal/config.h>
 
+#include <cassert>
 #include <limits>
 #include <type_traits>
 
@@ -226,6 +227,13 @@ template<typename T> inline typename std::enable_if<std::is_unsigned<T>::value,
 
 #endif
 
+template<typename T> constexpr std::enable_if_t<std::is_signed_v<T>, std::make_unsigned_t<T>>
+make_unsigned(T value)
+{
+    assert(value >= 0);
+    return value;
+}
+
 }
 
 #endif


More information about the Libreoffice-commits mailing list