summaryrefslogtreecommitdiff
path: root/include/tools/helpers.hxx
diff options
context:
space:
mode:
authorCaolán McNamara <caolanm@redhat.com>2017-10-31 08:54:42 +0000
committerCaolán McNamara <caolanm@redhat.com>2017-11-01 00:25:55 +0100
commit013618308c2d24702de18c12922931b130b6fade (patch)
treef70af86abdc8eea17471c1c1767986ce68c21834 /include/tools/helpers.hxx
parentaab1018339fba9e6c618a17dfb2a01464555c4dc (diff)
ofz#3934 Integer-overflow
Change-Id: I2c58cca6f01d7c50244dfec6acdfaa988cdbaa07 Reviewed-on: https://gerrit.libreoffice.org/44102 Tested-by: Jenkins <ci@libreoffice.org> Reviewed-by: Caolán McNamara <caolanm@redhat.com> Tested-by: Caolán McNamara <caolanm@redhat.com>
Diffstat (limited to 'include/tools/helpers.hxx')
-rw-r--r--include/tools/helpers.hxx18
1 files changed, 17 insertions, 1 deletions
diff --git a/include/tools/helpers.hxx b/include/tools/helpers.hxx
index 30064cf93fc6..8325f095bad8 100644
--- a/include/tools/helpers.hxx
+++ b/include/tools/helpers.hxx
@@ -11,7 +11,7 @@
#include <sal/config.h>
#include <sal/types.h>
-
+#include <o3tl/safeint.hxx>
#include <cassert>
#include <type_traits>
@@ -55,6 +55,22 @@ inline long FRound( double fVal )
return fVal > 0.0 ? static_cast<long>( fVal + 0.5 ) : -static_cast<long>( -fVal + 0.5 );
}
+// return (n >= 0)? (n*72+63)/127: (n*72-63)/127;
+inline sal_Int64 sanitiseMm100ToTwip(sal_Int64 n)
+{
+ if (n >= 0)
+ {
+ if (o3tl::checked_multiply<sal_Int64>(n, 72, n) || o3tl::checked_add<sal_Int64>(n, 63, n))
+ n = SAL_MAX_INT64;
+ }
+ else
+ {
+ if (o3tl::checked_multiply<sal_Int64>(n, 72, n) || o3tl::checked_sub<sal_Int64>(n, 63, n))
+ n = SAL_MIN_INT64;
+ }
+ return n / 127;
+}
+
#endif
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */