1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
|
/**********************************************************************
*
* Name: cpl_safemaths.hpp
* Project: CPL - Common Portability Library
* Purpose: Arithmetic overflow checking
* Author: Even Rouault <even.rouault at spatialys.com>
*
**********************************************************************
* Copyright (c) 2017, Even Rouault <even.rouault at spatialys.com>
*
* SPDX-License-Identifier: MIT
****************************************************************************/
#ifndef CPL_SAFEMATHS_INCLUDED
#define CPL_SAFEMATHS_INCLUDED
#include <exception>
#include <limits>
#ifndef __has_builtin
#define __has_builtin(x) 0
#endif
#if (__GNUC__ >= 5 && !defined(__INTEL_COMPILER)) || \
__has_builtin(__builtin_sadd_overflow)
#define BUILTIN_OVERFLOW_CHECK_AVAILABLE
#elif defined(_MSC_VER)
#include "safeint.h"
#elif defined(GDAL_COMPILATION)
#include "cpl_port.h"
#elif !defined(DISABLE_GINT64) && !defined(CPL_HAS_GINT64)
#if defined(WIN32) && defined(_MSC_VER)
typedef _int64 GInt64;
typedef unsigned _int64 GUInt64;
#else
typedef long long GInt64;
typedef unsigned long long GUInt64;
#endif
#define CPL_HAS_GINT64
#endif
template <typename T> struct CPLSafeInt
{
T val;
inline explicit CPLSafeInt(T valIn) : val(valIn)
{
}
inline T v() const
{
return val;
}
};
class CPLSafeIntOverflow : public std::exception
{
public:
inline CPLSafeIntOverflow()
{
}
};
class CPLSafeIntOverflowDivisionByZero : public CPLSafeIntOverflow
{
public:
inline CPLSafeIntOverflowDivisionByZero()
{
}
};
/** Convenience functions to build a CPLSafeInt */
inline CPLSafeInt<int> CPLSM(int x)
{
return CPLSafeInt<int>(x);
}
inline CPLSafeInt<unsigned> CPLSM(unsigned x)
{
return CPLSafeInt<unsigned>(x);
}
inline CPLSafeInt<unsigned> CPLSM_TO_UNSIGNED(int x)
{
if (x < 0)
throw CPLSafeIntOverflow();
return CPLSafeInt<unsigned>(static_cast<unsigned>(x));
}
// Unimplemented for now
inline CPLSafeInt<unsigned> CPLSM_TO_UNSIGNED(unsigned x);
inline CPLSafeInt<int> CPLSM(long x);
inline CPLSafeInt<unsigned> CPLSM_TO_UNSIGNED(long x);
inline CPLSafeInt<int> CPLSM(unsigned long x);
inline CPLSafeInt<unsigned> CPLSM_TO_UNSIGNED(unsigned long x);
#if defined(GDAL_COMPILATION)
inline CPLSafeInt<GInt64> CPLSM(GInt64 x)
{
return CPLSafeInt<GInt64>(x);
}
inline CPLSafeInt<unsigned> CPLSM_TO_UNSIGNED(GInt64 x);
inline CPLSafeInt<GUInt64> CPLSM(GUInt64 x)
{
return CPLSafeInt<GUInt64>(x);
}
inline CPLSafeInt<unsigned> CPLSM_TO_UNSIGNED(GUInt64 x);
#endif
#if !defined(BUILTIN_OVERFLOW_CHECK_AVAILABLE) && defined(_MSC_VER)
class CPLMSVCSafeIntException : public msl::utilities::SafeIntException
{
public:
static void SafeIntOnOverflow()
{
throw CPLSafeIntOverflow();
}
static void SafeIntOnDivZero()
{
throw CPLSafeIntOverflowDivisionByZero();
}
};
#endif
template <class T>
inline CPLSafeInt<T> SafeAddSigned(const CPLSafeInt<T> &A,
const CPLSafeInt<T> &B)
{
const auto a = A.v();
const auto b = B.v();
if (a > 0 && b > 0 && a > std::numeric_limits<T>::max() - b)
throw CPLSafeIntOverflow();
if (a < 0 && b < 0 && a < std::numeric_limits<T>::min() - b)
throw CPLSafeIntOverflow();
return CPLSM(a + b);
}
inline CPLSafeInt<int> operator+(const CPLSafeInt<int> &A,
const CPLSafeInt<int> &B)
{
#ifdef BUILTIN_OVERFLOW_CHECK_AVAILABLE
int res;
if (__builtin_sadd_overflow(A.v(), B.v(), &res))
throw CPLSafeIntOverflow();
return CPLSM(res);
#elif defined(_MSC_VER)
msl::utilities::SafeInt<int, CPLMSVCSafeIntException> A2(A.v());
msl::utilities::SafeInt<int, CPLMSVCSafeIntException> B2(B.v());
return CPLSM(static_cast<int>(A2 + B2));
#elif defined(CPL_HAS_GINT64)
const int a = A.v();
const int b = B.v();
const GInt64 res = static_cast<GInt64>(a) + b;
if (res < std::numeric_limits<int>::min() ||
res > std::numeric_limits<int>::max())
{
throw CPLSafeIntOverflow();
}
return CPLSM(static_cast<int>(res));
#else
return SafeAddSigned(A, B);
#endif
}
#if defined(GDAL_COMPILATION)
inline CPLSafeInt<GInt64> operator+(const CPLSafeInt<GInt64> &A,
const CPLSafeInt<GInt64> &B)
{
#ifdef BUILTIN_OVERFLOW_CHECK_AVAILABLE
GInt64 res;
if (__builtin_saddll_overflow(A.v(), B.v(), &res))
throw CPLSafeIntOverflow();
return CPLSM(res);
#elif defined(_MSC_VER)
msl::utilities::SafeInt<GInt64, CPLMSVCSafeIntException> A2(A.v());
msl::utilities::SafeInt<GInt64, CPLMSVCSafeIntException> B2(B.v());
return CPLSM(static_cast<GInt64>(A2 + B2));
#else
return SafeAddSigned(A, B);
#endif
}
#endif // GDAL_COMPILATION
inline CPLSafeInt<unsigned> operator+(const CPLSafeInt<unsigned> &A,
const CPLSafeInt<unsigned> &B)
{
#ifdef BUILTIN_OVERFLOW_CHECK_AVAILABLE
unsigned res;
if (__builtin_uadd_overflow(A.v(), B.v(), &res))
throw CPLSafeIntOverflow();
return CPLSM(res);
#elif defined(_MSC_VER)
msl::utilities::SafeInt<unsigned, CPLMSVCSafeIntException> A2(A.v());
msl::utilities::SafeInt<unsigned, CPLMSVCSafeIntException> B2(B.v());
return CPLSM(static_cast<unsigned>(A2 + B2));
#else
const unsigned a = A.v();
const unsigned b = B.v();
if (a > std::numeric_limits<unsigned>::max() - b)
throw CPLSafeIntOverflow();
return CPLSM(a + b);
#endif
}
#if defined(GDAL_COMPILATION)
inline CPLSafeInt<GUInt64> operator+(const CPLSafeInt<GUInt64> &A,
const CPLSafeInt<GUInt64> &B)
{
#ifdef BUILTIN_OVERFLOW_CHECK_AVAILABLE
GUInt64 res;
if (__builtin_uaddll_overflow(A.v(), B.v(), &res))
throw CPLSafeIntOverflow();
return CPLSM(res);
#elif defined(_MSC_VER)
msl::utilities::SafeInt<GUInt64, CPLMSVCSafeIntException> A2(A.v());
msl::utilities::SafeInt<GUInt64, CPLMSVCSafeIntException> B2(B.v());
return CPLSM(static_cast<GUInt64>(A2 + B2));
#else
const GUInt64 a = A.v();
const GUInt64 b = B.v();
if (a > std::numeric_limits<GUInt64>::max() - b)
throw CPLSafeIntOverflow();
return CPLSM(a + b);
#endif
}
#endif // GDAL_COMPILATION
template <class T>
inline CPLSafeInt<T> SafeSubSigned(const CPLSafeInt<T> &A,
const CPLSafeInt<T> &B)
{
const auto a = A.v();
const auto b = B.v();
/* caution we must catch a == 0 && b = INT_MIN */
if (a >= 0 && b < 0 && a > std::numeric_limits<T>::max() + b)
throw CPLSafeIntOverflow();
if (a < 0 && b > 0 && a < std::numeric_limits<T>::min() + b)
throw CPLSafeIntOverflow();
return CPLSM(a - b);
}
inline CPLSafeInt<int> operator-(const CPLSafeInt<int> &A,
const CPLSafeInt<int> &B)
{
#ifdef BUILTIN_OVERFLOW_CHECK_AVAILABLE
int res;
if (__builtin_ssub_overflow(A.v(), B.v(), &res))
throw CPLSafeIntOverflow();
return CPLSM(res);
#elif defined(_MSC_VER)
msl::utilities::SafeInt<int, CPLMSVCSafeIntException> A2(A.v());
msl::utilities::SafeInt<int, CPLMSVCSafeIntException> B2(B.v());
return CPLSM(static_cast<int>(A2 - B2));
#elif defined(CPL_HAS_GINT64)
const int a = A.v();
const int b = B.v();
const GInt64 res = static_cast<GInt64>(a) - b;
if (res < std::numeric_limits<int>::min() ||
res > std::numeric_limits<int>::max())
{
throw CPLSafeIntOverflow();
}
return CPLSM(static_cast<int>(res));
#else
return SafeSubSigned(A, B);
#endif
}
#if defined(GDAL_COMPILATION)
inline CPLSafeInt<GInt64> operator-(const CPLSafeInt<GInt64> &A,
const CPLSafeInt<GInt64> &B)
{
#ifdef BUILTIN_OVERFLOW_CHECK_AVAILABLE
GInt64 res;
if (__builtin_ssubll_overflow(A.v(), B.v(), &res))
throw CPLSafeIntOverflow();
return CPLSM(res);
#elif defined(_MSC_VER)
msl::utilities::SafeInt<GInt64, CPLMSVCSafeIntException> A2(A.v());
msl::utilities::SafeInt<GInt64, CPLMSVCSafeIntException> B2(B.v());
return CPLSM(static_cast<GInt64>(A2 - B2));
#else
return SafeSubSigned(A, B);
#endif
}
#endif // GDAL_COMPILATION
inline CPLSafeInt<unsigned> operator-(const CPLSafeInt<unsigned> &A,
const CPLSafeInt<unsigned> &B)
{
#ifdef BUILTIN_OVERFLOW_CHECK_AVAILABLE
unsigned res;
if (__builtin_usub_overflow(A.v(), B.v(), &res))
throw CPLSafeIntOverflow();
return CPLSM(res);
#elif defined(_MSC_VER)
msl::utilities::SafeInt<unsigned, CPLMSVCSafeIntException> A2(A.v());
msl::utilities::SafeInt<unsigned, CPLMSVCSafeIntException> B2(B.v());
return CPLSM(static_cast<unsigned>(A2 - B2));
#else
const unsigned a = A.v();
const unsigned b = B.v();
if (a < b)
throw CPLSafeIntOverflow();
return CPLSM(a - b);
#endif
}
template <class T>
inline CPLSafeInt<T> SafeMulSigned(const CPLSafeInt<T> &A,
const CPLSafeInt<T> &B)
{
const auto a = A.v();
const auto b = B.v();
if (a > 0 && b > 0 && a > std::numeric_limits<T>::max() / b)
throw CPLSafeIntOverflow();
if (a > 0 && b < 0 && b < std::numeric_limits<T>::min() / a)
throw CPLSafeIntOverflow();
if (a < 0 && b > 0 && a < std::numeric_limits<T>::min() / b)
throw CPLSafeIntOverflow();
else if (a == std::numeric_limits<T>::min())
{
if (b != 0 && b != 1)
throw CPLSafeIntOverflow();
}
else if (b == std::numeric_limits<T>::min())
{
if (a != 0 && a != 1)
throw CPLSafeIntOverflow();
}
else if (a < 0 && b < 0 && -a > std::numeric_limits<T>::max() / (-b))
throw CPLSafeIntOverflow();
return CPLSM(a * b);
}
inline CPLSafeInt<int> operator*(const CPLSafeInt<int> &A,
const CPLSafeInt<int> &B)
{
#ifdef BUILTIN_OVERFLOW_CHECK_AVAILABLE
int res;
if (__builtin_smul_overflow(A.v(), B.v(), &res))
throw CPLSafeIntOverflow();
return CPLSM(res);
#elif defined(_MSC_VER)
msl::utilities::SafeInt<int, CPLMSVCSafeIntException> A2(A.v());
msl::utilities::SafeInt<int, CPLMSVCSafeIntException> B2(B.v());
return CPLSM(static_cast<int>(A2 * B2));
#elif defined(CPL_HAS_GINT64)
const int a = A.v();
const int b = B.v();
const GInt64 res = static_cast<GInt64>(a) * b;
if (res < std::numeric_limits<int>::min() ||
res > std::numeric_limits<int>::max())
{
throw CPLSafeIntOverflow();
}
return CPLSM(static_cast<int>(res));
#else
return SafeMulSigned(A, B);
#endif
}
#if defined(GDAL_COMPILATION)
inline CPLSafeInt<GInt64> operator*(const CPLSafeInt<GInt64> &A,
const CPLSafeInt<GInt64> &B)
{
#if defined(BUILTIN_OVERFLOW_CHECK_AVAILABLE) && defined(__x86_64__)
GInt64 res;
if (__builtin_smulll_overflow(A.v(), B.v(), &res))
throw CPLSafeIntOverflow();
return CPLSM(res);
#elif defined(_MSC_VER)
msl::utilities::SafeInt<GInt64, CPLMSVCSafeIntException> A2(A.v());
msl::utilities::SafeInt<GInt64, CPLMSVCSafeIntException> B2(B.v());
return CPLSM(static_cast<GInt64>(A2 * B2));
#else
return SafeMulSigned(A, B);
#endif
}
#endif // GDAL_COMPILATION
inline CPLSafeInt<unsigned> operator*(const CPLSafeInt<unsigned> &A,
const CPLSafeInt<unsigned> &B)
{
#ifdef BUILTIN_OVERFLOW_CHECK_AVAILABLE
unsigned res;
if (__builtin_umul_overflow(A.v(), B.v(), &res))
throw CPLSafeIntOverflow();
return CPLSM(res);
#elif defined(_MSC_VER)
msl::utilities::SafeInt<unsigned, CPLMSVCSafeIntException> A2(A.v());
msl::utilities::SafeInt<unsigned, CPLMSVCSafeIntException> B2(B.v());
return CPLSM(static_cast<unsigned>(A2 * B2));
#elif defined(CPL_HAS_GINT64)
const unsigned a = A.v();
const unsigned b = B.v();
const GUInt64 res = static_cast<GUInt64>(a) * b;
if (res > std::numeric_limits<unsigned>::max())
{
throw CPLSafeIntOverflow();
}
return CPLSM(static_cast<unsigned>(res));
#else
const unsigned a = A.v();
const unsigned b = B.v();
if (b > 0 && a > std::numeric_limits<unsigned>::max() / b)
throw CPLSafeIntOverflow();
return CPLSM(a * b);
#endif
}
#if defined(GDAL_COMPILATION)
inline CPLSafeInt<GUInt64> operator*(const CPLSafeInt<GUInt64> &A,
const CPLSafeInt<GUInt64> &B)
{
#ifdef BUILTIN_OVERFLOW_CHECK_AVAILABLE
GUInt64 res;
if (__builtin_umulll_overflow(A.v(), B.v(), &res))
throw CPLSafeIntOverflow();
return CPLSM(res);
#elif defined(_MSC_VER)
msl::utilities::SafeInt<GUInt64, CPLMSVCSafeIntException> A2(A.v());
msl::utilities::SafeInt<GUInt64, CPLMSVCSafeIntException> B2(B.v());
return CPLSM(static_cast<GUInt64>(A2 * B2));
#else
const GUInt64 a = A.v();
const GUInt64 b = B.v();
if (b > 0 && a > std::numeric_limits<GUInt64>::max() / b)
throw CPLSafeIntOverflow();
return CPLSM(a * b);
#endif
}
#endif // GDAL_COMPILATION
template <class T>
inline CPLSafeInt<T> SafeDivSigned(const CPLSafeInt<T> &A,
const CPLSafeInt<T> &B)
{
const auto a = A.v();
const auto b = B.v();
if (b == 0)
throw CPLSafeIntOverflowDivisionByZero();
if (a == std::numeric_limits<T>::min() && b == -1)
throw CPLSafeIntOverflow();
return CPLSM(a / b);
}
inline CPLSafeInt<int> operator/(const CPLSafeInt<int> &A,
const CPLSafeInt<int> &B)
{
return SafeDivSigned(A, B);
}
#if defined(GDAL_COMPILATION)
inline CPLSafeInt<GInt64> operator/(const CPLSafeInt<GInt64> &A,
const CPLSafeInt<GInt64> &B)
{
return SafeDivSigned(A, B);
}
#endif // GDAL_COMPILATION
inline CPLSafeInt<unsigned> operator/(const CPLSafeInt<unsigned> &A,
const CPLSafeInt<unsigned> &B)
{
const unsigned a = A.v();
const unsigned b = B.v();
if (b == 0)
throw CPLSafeIntOverflowDivisionByZero();
return CPLSM(a / b);
}
#endif // CPL_SAFEMATHS_INCLUDED
|