| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright (C) 2011 Apple Inc. All rights reserved. | |
| 3 * | |
| 4 * Redistribution and use in source and binary forms, with or without | |
| 5 * modification, are permitted provided that the following conditions | |
| 6 * are met: | |
| 7 * 1. Redistributions of source code must retain the above copyright | |
| 8 * notice, this list of conditions and the following disclaimer. | |
| 9 * 2. Redistributions in binary form must reproduce the above copyright | |
| 10 * notice, this list of conditions and the following disclaimer in the | |
| 11 * documentation and/or other materials provided with the distribution. | |
| 12 * | |
| 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY | |
| 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
| 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR | |
| 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
| 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
| 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
| 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | |
| 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
| 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 24 */ | |
| 25 | |
| 26 #ifndef CheckedArithmetic_h | |
| 27 #define CheckedArithmetic_h | |
| 28 | |
| 29 #include "wtf/Assertions.h" | |
| 30 #include "wtf/TypeTraits.h" | |
| 31 | |
| 32 #include <limits> | |
| 33 #include <stdint.h> | |
| 34 | |
| 35 /* Checked<T> | |
| 36 * | |
| 37 * This class provides a mechanism to perform overflow-safe integer arithmetic | |
| 38 * without having to manually ensure that you have all the required bounds check
s | |
| 39 * directly in your code. | |
| 40 * | |
| 41 * There are two modes of operation: | |
| 42 * - The default is Checked<T, CrashOnOverflow>, and crashes at the point | |
| 43 * and overflow has occurred. | |
| 44 * - The alternative is Checked<T, RecordOverflow>, which uses an additional | |
| 45 * byte of storage to track whether an overflow has occurred, subsequent | |
| 46 * unchecked operations will crash if an overflow has occured | |
| 47 * | |
| 48 * It is possible to provide a custom overflow handler, in which case you need | |
| 49 * to support these functions: | |
| 50 * - void overflowed(); | |
| 51 * This function is called when an operation has produced an overflow. | |
| 52 * - bool hasOverflowed(); | |
| 53 * This function must return true if overflowed() has been called on an | |
| 54 * instance and false if it has not. | |
| 55 * - void clearOverflow(); | |
| 56 * Used to reset overflow tracking when a value is being overwritten with | |
| 57 * a new value. | |
| 58 * | |
| 59 * Checked<T> works for all integer types, with the following caveats: | |
| 60 * - Mixing signedness of operands is only supported for types narrower than | |
| 61 * 64bits. | |
| 62 * - It does have a performance impact, so tight loops may want to be careful | |
| 63 * when using it. | |
| 64 * | |
| 65 */ | |
| 66 | |
| 67 namespace WTF { | |
| 68 | |
| 69 enum class CheckedState { | |
| 70 DidOverflow, | |
| 71 DidNotOverflow | |
| 72 }; | |
| 73 | |
| 74 class CrashOnOverflow { | |
| 75 protected: | |
| 76 NO_RETURN_DUE_TO_CRASH void overflowed() | |
| 77 { | |
| 78 CRASH(); | |
| 79 } | |
| 80 | |
| 81 void clearOverflow() { } | |
| 82 | |
| 83 public: | |
| 84 bool hasOverflowed() const { return false; } | |
| 85 }; | |
| 86 | |
| 87 class RecordOverflow { | |
| 88 protected: | |
| 89 RecordOverflow() | |
| 90 : m_overflowed(false) | |
| 91 { | |
| 92 } | |
| 93 | |
| 94 void overflowed() | |
| 95 { | |
| 96 m_overflowed = true; | |
| 97 } | |
| 98 | |
| 99 void clearOverflow() | |
| 100 { | |
| 101 m_overflowed = false; | |
| 102 } | |
| 103 | |
| 104 public: | |
| 105 bool hasOverflowed() const { return m_overflowed; } | |
| 106 | |
| 107 private: | |
| 108 unsigned char m_overflowed; | |
| 109 }; | |
| 110 | |
| 111 template <typename T, class OverflowHandler = CrashOnOverflow> class Checked; | |
| 112 template <typename T> struct RemoveChecked; | |
| 113 template <typename T> struct RemoveChecked<Checked<T>>; | |
| 114 | |
| 115 template <typename Target, typename Source, bool targetSigned = std::numeric_lim
its<Target>::is_signed, bool sourceSigned = std::numeric_limits<Source>::is_sign
ed> struct BoundsChecker; | |
| 116 template <typename Target, typename Source> struct BoundsChecker<Target, Source,
false, false> { | |
| 117 static bool inBounds(Source value) | |
| 118 { | |
| 119 // Same signedness so implicit type conversion will always increase prec
ision | |
| 120 // to widest type | |
| 121 return value <= std::numeric_limits<Target>::max(); | |
| 122 } | |
| 123 }; | |
| 124 | |
| 125 template <typename Target, typename Source> struct BoundsChecker<Target, Source,
true, true> { | |
| 126 static bool inBounds(Source value) | |
| 127 { | |
| 128 // Same signedness so implicit type conversion will always increase prec
ision | |
| 129 // to widest type | |
| 130 return std::numeric_limits<Target>::min() <= value && value <= std::nume
ric_limits<Target>::max(); | |
| 131 } | |
| 132 }; | |
| 133 | |
| 134 template <typename Target, typename Source> struct BoundsChecker<Target, Source,
false, true> { | |
| 135 static bool inBounds(Source value) | |
| 136 { | |
| 137 // Target is unsigned so any value less than zero is clearly unsafe | |
| 138 if (value < 0) | |
| 139 return false; | |
| 140 // If our (unsigned) Target is the same or greater width we can | |
| 141 // convert value to type Target without losing precision | |
| 142 if (sizeof(Target) >= sizeof(Source)) | |
| 143 return static_cast<Target>(value) <= std::numeric_limits<Target>::ma
x(); | |
| 144 // The signed Source type has greater precision than the target so | |
| 145 // max(Target) -> Source will widen. | |
| 146 return value <= static_cast<Source>(std::numeric_limits<Target>::max()); | |
| 147 } | |
| 148 }; | |
| 149 | |
| 150 template <typename Target, typename Source> struct BoundsChecker<Target, Source,
true, false> { | |
| 151 static bool inBounds(Source value) | |
| 152 { | |
| 153 // Signed target with an unsigned source | |
| 154 if (sizeof(Target) <= sizeof(Source)) | |
| 155 return value <= static_cast<Source>(std::numeric_limits<Target>::max
()); | |
| 156 // Target is Wider than Source so we're guaranteed to fit any value in | |
| 157 // unsigned Source | |
| 158 return true; | |
| 159 } | |
| 160 }; | |
| 161 | |
| 162 template <typename Target, typename Source, bool CanElide = std::is_same<Target,
Source>::value || (sizeof(Target) > sizeof(Source)) > struct BoundsCheckElider; | |
| 163 template <typename Target, typename Source> struct BoundsCheckElider<Target, Sou
rce, true> { | |
| 164 static bool inBounds(Source) { return true; } | |
| 165 }; | |
| 166 template <typename Target, typename Source> struct BoundsCheckElider<Target, Sou
rce, false> : public BoundsChecker<Target, Source> { | |
| 167 }; | |
| 168 | |
| 169 template <typename Target, typename Source> static inline bool isInBounds(Source
value) | |
| 170 { | |
| 171 return BoundsCheckElider<Target, Source>::inBounds(value); | |
| 172 } | |
| 173 | |
| 174 template <typename T> struct RemoveChecked { | |
| 175 typedef T CleanType; | |
| 176 static const CleanType DefaultValue = 0; | |
| 177 }; | |
| 178 | |
| 179 template <typename T> struct RemoveChecked<Checked<T, CrashOnOverflow>> { | |
| 180 typedef typename RemoveChecked<T>::CleanType CleanType; | |
| 181 static const CleanType DefaultValue = 0; | |
| 182 }; | |
| 183 | |
| 184 template <typename T> struct RemoveChecked<Checked<T, RecordOverflow>> { | |
| 185 typedef typename RemoveChecked<T>::CleanType CleanType; | |
| 186 static const CleanType DefaultValue = 0; | |
| 187 }; | |
| 188 | |
| 189 // The ResultBase and SignednessSelector are used to workaround typeof not being | |
| 190 // available in MSVC | |
| 191 template <typename U, typename V, bool uIsBigger = (sizeof(U) > sizeof(V)), bool
sameSize = (sizeof(U) == sizeof(V))> struct ResultBase; | |
| 192 template <typename U, typename V> struct ResultBase<U, V, true, false> { | |
| 193 typedef U ResultType; | |
| 194 }; | |
| 195 | |
| 196 template <typename U, typename V> struct ResultBase<U, V, false, false> { | |
| 197 typedef V ResultType; | |
| 198 }; | |
| 199 | |
| 200 template <typename U> struct ResultBase<U, U, false, true> { | |
| 201 typedef U ResultType; | |
| 202 }; | |
| 203 | |
| 204 template <typename U, typename V, bool uIsSigned = std::numeric_limits<U>::is_si
gned, bool vIsSigned = std::numeric_limits<V>::is_signed> struct SignednessSelec
tor; | |
| 205 template <typename U, typename V> struct SignednessSelector<U, V, true, true> { | |
| 206 typedef U ResultType; | |
| 207 }; | |
| 208 | |
| 209 template <typename U, typename V> struct SignednessSelector<U, V, false, false>
{ | |
| 210 typedef U ResultType; | |
| 211 }; | |
| 212 | |
| 213 template <typename U, typename V> struct SignednessSelector<U, V, true, false> { | |
| 214 typedef V ResultType; | |
| 215 }; | |
| 216 | |
| 217 template <typename U, typename V> struct SignednessSelector<U, V, false, true> { | |
| 218 typedef U ResultType; | |
| 219 }; | |
| 220 | |
| 221 template <typename U, typename V> struct ResultBase<U, V, false, true> { | |
| 222 typedef typename SignednessSelector<U, V>::ResultType ResultType; | |
| 223 }; | |
| 224 | |
| 225 template <typename U, typename V> struct Result : ResultBase<typename RemoveChec
ked<U>::CleanType, typename RemoveChecked<V>::CleanType> { | |
| 226 }; | |
| 227 | |
| 228 template <typename LHS, typename RHS, typename ResultType = typename Result<LHS,
RHS>::ResultType, | |
| 229 bool lhsSigned = std::numeric_limits<LHS>::is_signed, bool rhsSigned = std::
numeric_limits<RHS>::is_signed> struct ArithmeticOperations; | |
| 230 | |
| 231 template <typename LHS, typename RHS, typename ResultType> struct ArithmeticOper
ations<LHS, RHS, ResultType, true, true> { | |
| 232 // LHS and RHS are signed types | |
| 233 | |
| 234 // Helper function | |
| 235 static inline bool signsMatch(LHS lhs, RHS rhs) | |
| 236 { | |
| 237 return (lhs ^ rhs) >= 0; | |
| 238 } | |
| 239 | |
| 240 static inline bool add(LHS lhs, RHS rhs, ResultType& result) WARN_UNUSED_RET
URN | |
| 241 { | |
| 242 if (signsMatch(lhs, rhs)) { | |
| 243 if (lhs >= 0) { | |
| 244 if ((std::numeric_limits<ResultType>::max() - rhs) < lhs) | |
| 245 return false; | |
| 246 } else { | |
| 247 ResultType temp = lhs - std::numeric_limits<ResultType>::min(); | |
| 248 if (rhs < -temp) | |
| 249 return false; | |
| 250 } | |
| 251 } // if the signs do not match this operation can't overflow | |
| 252 result = static_cast<ResultType>(lhs + rhs); | |
| 253 return true; | |
| 254 } | |
| 255 | |
| 256 static inline bool sub(LHS lhs, RHS rhs, ResultType& result) WARN_UNUSED_RET
URN | |
| 257 { | |
| 258 if (!signsMatch(lhs, rhs)) { | |
| 259 if (lhs >= 0) { | |
| 260 if (lhs > std::numeric_limits<ResultType>::max() + rhs) | |
| 261 return false; | |
| 262 } else { | |
| 263 if (rhs > std::numeric_limits<ResultType>::max() + lhs) | |
| 264 return false; | |
| 265 } | |
| 266 } // if the signs match this operation can't overflow | |
| 267 result = static_cast<ResultType>(lhs - rhs); | |
| 268 return true; | |
| 269 } | |
| 270 | |
| 271 static inline bool multiply(LHS lhs, RHS rhs, ResultType& result) WARN_UNUSE
D_RETURN | |
| 272 { | |
| 273 if (signsMatch(lhs, rhs)) { | |
| 274 if (lhs >= 0) { | |
| 275 if (lhs && (std::numeric_limits<ResultType>::max() / lhs) < rhs) | |
| 276 return false; | |
| 277 } else { | |
| 278 if (static_cast<ResultType>(lhs) == std::numeric_limits<ResultTy
pe>::min() || static_cast<ResultType>(rhs) == std::numeric_limits<ResultType>::m
in()) | |
| 279 return false; | |
| 280 if ((std::numeric_limits<ResultType>::max() / -lhs) < -rhs) | |
| 281 return false; | |
| 282 } | |
| 283 } else { | |
| 284 if (lhs < 0) { | |
| 285 if (rhs && lhs < (std::numeric_limits<ResultType>::min() / rhs)) | |
| 286 return false; | |
| 287 } else { | |
| 288 if (lhs && rhs < (std::numeric_limits<ResultType>::min() / lhs)) | |
| 289 return false; | |
| 290 } | |
| 291 } | |
| 292 result = static_cast<ResultType>(lhs * rhs); | |
| 293 return true; | |
| 294 } | |
| 295 | |
| 296 static inline bool equals(LHS lhs, RHS rhs) { return lhs == rhs; } | |
| 297 | |
| 298 }; | |
| 299 | |
| 300 template <typename LHS, typename RHS, typename ResultType> struct ArithmeticOper
ations<LHS, RHS, ResultType, false, false> { | |
| 301 // LHS and RHS are unsigned types so bounds checks are nice and easy | |
| 302 static inline bool add(LHS lhs, RHS rhs, ResultType& result) WARN_UNUSED_RET
URN | |
| 303 { | |
| 304 ResultType temp = lhs + rhs; | |
| 305 if (temp < lhs) | |
| 306 return false; | |
| 307 result = temp; | |
| 308 return true; | |
| 309 } | |
| 310 | |
| 311 static inline bool sub(LHS lhs, RHS rhs, ResultType& result) WARN_UNUSED_RET
URN | |
| 312 { | |
| 313 ResultType temp = lhs - rhs; | |
| 314 if (temp > lhs) | |
| 315 return false; | |
| 316 result = temp; | |
| 317 return true; | |
| 318 } | |
| 319 | |
| 320 static inline bool multiply(LHS lhs, RHS rhs, ResultType& result) WARN_UNUSE
D_RETURN | |
| 321 { | |
| 322 if (!lhs || !rhs) { | |
| 323 result = 0; | |
| 324 return true; | |
| 325 } | |
| 326 if (std::numeric_limits<ResultType>::max() / lhs < rhs) | |
| 327 return false; | |
| 328 result = lhs * rhs; | |
| 329 return true; | |
| 330 } | |
| 331 | |
| 332 static inline bool equals(LHS lhs, RHS rhs) { return lhs == rhs; } | |
| 333 | |
| 334 }; | |
| 335 | |
| 336 template <typename ResultType> struct ArithmeticOperations<int, unsigned, Result
Type, true, false> { | |
| 337 static inline bool add(int64_t lhs, int64_t rhs, ResultType& result) | |
| 338 { | |
| 339 int64_t temp = lhs + rhs; | |
| 340 if (temp < std::numeric_limits<ResultType>::min()) | |
| 341 return false; | |
| 342 if (temp > std::numeric_limits<ResultType>::max()) | |
| 343 return false; | |
| 344 result = static_cast<ResultType>(temp); | |
| 345 return true; | |
| 346 } | |
| 347 | |
| 348 static inline bool sub(int64_t lhs, int64_t rhs, ResultType& result) | |
| 349 { | |
| 350 int64_t temp = lhs - rhs; | |
| 351 if (temp < std::numeric_limits<ResultType>::min()) | |
| 352 return false; | |
| 353 if (temp > std::numeric_limits<ResultType>::max()) | |
| 354 return false; | |
| 355 result = static_cast<ResultType>(temp); | |
| 356 return true; | |
| 357 } | |
| 358 | |
| 359 static inline bool multiply(int64_t lhs, int64_t rhs, ResultType& result) | |
| 360 { | |
| 361 int64_t temp = lhs * rhs; | |
| 362 if (temp < std::numeric_limits<ResultType>::min()) | |
| 363 return false; | |
| 364 if (temp > std::numeric_limits<ResultType>::max()) | |
| 365 return false; | |
| 366 result = static_cast<ResultType>(temp); | |
| 367 return true; | |
| 368 } | |
| 369 | |
| 370 static inline bool equals(int lhs, unsigned rhs) | |
| 371 { | |
| 372 return static_cast<int64_t>(lhs) == static_cast<int64_t>(rhs); | |
| 373 } | |
| 374 }; | |
| 375 | |
| 376 template <typename ResultType> struct ArithmeticOperations<unsigned, int, Result
Type, false, true> { | |
| 377 static inline bool add(int64_t lhs, int64_t rhs, ResultType& result) | |
| 378 { | |
| 379 return ArithmeticOperations<int, unsigned, ResultType>::add(rhs, lhs, re
sult); | |
| 380 } | |
| 381 | |
| 382 static inline bool sub(int64_t lhs, int64_t rhs, ResultType& result) | |
| 383 { | |
| 384 return ArithmeticOperations<int, unsigned, ResultType>::sub(lhs, rhs, re
sult); | |
| 385 } | |
| 386 | |
| 387 static inline bool multiply(int64_t lhs, int64_t rhs, ResultType& result) | |
| 388 { | |
| 389 return ArithmeticOperations<int, unsigned, ResultType>::multiply(rhs, lh
s, result); | |
| 390 } | |
| 391 | |
| 392 static inline bool equals(unsigned lhs, int rhs) | |
| 393 { | |
| 394 return ArithmeticOperations<int, unsigned, ResultType>::equals(rhs, lhs)
; | |
| 395 } | |
| 396 }; | |
| 397 | |
| 398 template <typename U, typename V, typename R> static inline bool safeAdd(U lhs,
V rhs, R& result) | |
| 399 { | |
| 400 return ArithmeticOperations<U, V, R>::add(lhs, rhs, result); | |
| 401 } | |
| 402 | |
| 403 template <typename U, typename V, typename R> static inline bool safeSub(U lhs,
V rhs, R& result) | |
| 404 { | |
| 405 return ArithmeticOperations<U, V, R>::sub(lhs, rhs, result); | |
| 406 } | |
| 407 | |
| 408 template <typename U, typename V, typename R> static inline bool safeMultiply(U
lhs, V rhs, R& result) | |
| 409 { | |
| 410 return ArithmeticOperations<U, V, R>::multiply(lhs, rhs, result); | |
| 411 } | |
| 412 | |
| 413 template <typename U, typename V> static inline bool safeEquals(U lhs, V rhs) | |
| 414 { | |
| 415 return ArithmeticOperations<U, V>::equals(lhs, rhs); | |
| 416 } | |
| 417 | |
| 418 enum ResultOverflowedTag { ResultOverflowed }; | |
| 419 | |
| 420 template <typename T, class OverflowHandler> class Checked : public OverflowHand
ler { | |
| 421 public: | |
| 422 template <typename _T, class _OverflowHandler> friend class Checked; | |
| 423 Checked() | |
| 424 : m_value(0) | |
| 425 { | |
| 426 } | |
| 427 | |
| 428 Checked(ResultOverflowedTag) | |
| 429 : m_value(0) | |
| 430 { | |
| 431 this->overflowed(); | |
| 432 } | |
| 433 | |
| 434 template <typename U> Checked(U value) | |
| 435 { | |
| 436 if (!isInBounds<T>(value)) | |
| 437 this->overflowed(); | |
| 438 m_value = static_cast<T>(value); | |
| 439 } | |
| 440 | |
| 441 template <typename V> Checked(const Checked<T, V>& rhs) | |
| 442 : m_value(rhs.m_value) | |
| 443 { | |
| 444 if (rhs.hasOverflowed()) | |
| 445 this->overflowed(); | |
| 446 } | |
| 447 | |
| 448 template <typename U> Checked(const Checked<U, OverflowHandler>& rhs) | |
| 449 : OverflowHandler(rhs) | |
| 450 { | |
| 451 if (!isInBounds<T>(rhs.m_value)) | |
| 452 this->overflowed(); | |
| 453 m_value = static_cast<T>(rhs.m_value); | |
| 454 } | |
| 455 | |
| 456 template <typename U, typename V> Checked(const Checked<U, V>& rhs) | |
| 457 { | |
| 458 if (rhs.hasOverflowed()) | |
| 459 this->overflowed(); | |
| 460 if (!isInBounds<T>(rhs.m_value)) | |
| 461 this->overflowed(); | |
| 462 m_value = static_cast<T>(rhs.m_value); | |
| 463 } | |
| 464 | |
| 465 const Checked& operator=(Checked rhs) | |
| 466 { | |
| 467 this->clearOverflow(); | |
| 468 if (rhs.hasOverflowed()) | |
| 469 this->overflowed(); | |
| 470 m_value = static_cast<T>(rhs.m_value); | |
| 471 return *this; | |
| 472 } | |
| 473 | |
| 474 template <typename U> const Checked& operator=(U value) | |
| 475 { | |
| 476 return *this = Checked(value); | |
| 477 } | |
| 478 | |
| 479 template <typename U, typename V> const Checked& operator=(const Checked<U,
V>& rhs) | |
| 480 { | |
| 481 return *this = Checked(rhs); | |
| 482 } | |
| 483 | |
| 484 // prefix | |
| 485 const Checked& operator++() | |
| 486 { | |
| 487 if (m_value == std::numeric_limits<T>::max()) | |
| 488 this->overflowed(); | |
| 489 m_value++; | |
| 490 return *this; | |
| 491 } | |
| 492 | |
| 493 const Checked& operator--() | |
| 494 { | |
| 495 if (m_value == std::numeric_limits<T>::min()) | |
| 496 this->overflowed(); | |
| 497 m_value--; | |
| 498 return *this; | |
| 499 } | |
| 500 | |
| 501 // postfix operators | |
| 502 const Checked operator++(int) | |
| 503 { | |
| 504 if (m_value == std::numeric_limits<T>::max()) | |
| 505 this->overflowed(); | |
| 506 return Checked(m_value++); | |
| 507 } | |
| 508 | |
| 509 const Checked operator--(int) | |
| 510 { | |
| 511 if (m_value == std::numeric_limits<T>::min()) | |
| 512 this->overflowed(); | |
| 513 return Checked(m_value--); | |
| 514 } | |
| 515 | |
| 516 // Boolean operators | |
| 517 bool operator!() const | |
| 518 { | |
| 519 if (this->hasOverflowed()) | |
| 520 CRASH(); | |
| 521 return !m_value; | |
| 522 } | |
| 523 | |
| 524 typedef void* (Checked::*UnspecifiedBoolType); | |
| 525 operator UnspecifiedBoolType*() const | |
| 526 { | |
| 527 if (this->hasOverflowed()) | |
| 528 CRASH(); | |
| 529 return (m_value) ? reinterpret_cast<UnspecifiedBoolType*>(1) : 0; | |
| 530 } | |
| 531 | |
| 532 // Value accessors. unsafeGet() will crash if there's been an overflow. | |
| 533 T unsafeGet() const | |
| 534 { | |
| 535 if (this->hasOverflowed()) | |
| 536 CRASH(); | |
| 537 return m_value; | |
| 538 } | |
| 539 | |
| 540 inline CheckedState safeGet(T& value) const WARN_UNUSED_RETURN | |
| 541 { | |
| 542 value = m_value; | |
| 543 if (this->hasOverflowed()) | |
| 544 return CheckedState::DidOverflow; | |
| 545 return CheckedState::DidNotOverflow; | |
| 546 } | |
| 547 | |
| 548 // Mutating assignment | |
| 549 template <typename U> const Checked operator+=(U rhs) | |
| 550 { | |
| 551 if (!safeAdd(m_value, rhs, m_value)) | |
| 552 this->overflowed(); | |
| 553 return *this; | |
| 554 } | |
| 555 | |
| 556 template <typename U> const Checked operator-=(U rhs) | |
| 557 { | |
| 558 if (!safeSub(m_value, rhs, m_value)) | |
| 559 this->overflowed(); | |
| 560 return *this; | |
| 561 } | |
| 562 | |
| 563 template <typename U> const Checked operator*=(U rhs) | |
| 564 { | |
| 565 if (!safeMultiply(m_value, rhs, m_value)) | |
| 566 this->overflowed(); | |
| 567 return *this; | |
| 568 } | |
| 569 | |
| 570 const Checked operator*=(double rhs) | |
| 571 { | |
| 572 double result = rhs * m_value; | |
| 573 // Handle +/- infinity and NaN | |
| 574 if (!(std::numeric_limits<T>::min() <= result && std::numeric_limits<T>:
:max() >= result)) | |
| 575 this->overflowed(); | |
| 576 m_value = (T)result; | |
| 577 return *this; | |
| 578 } | |
| 579 | |
| 580 const Checked operator*=(float rhs) | |
| 581 { | |
| 582 return *this *= (double)rhs; | |
| 583 } | |
| 584 | |
| 585 template <typename U, typename V> const Checked operator+=(Checked<U, V> rhs
) | |
| 586 { | |
| 587 if (rhs.hasOverflowed()) | |
| 588 this->overflowed(); | |
| 589 return *this += rhs.m_value; | |
| 590 } | |
| 591 | |
| 592 template <typename U, typename V> const Checked operator-=(Checked<U, V> rhs
) | |
| 593 { | |
| 594 if (rhs.hasOverflowed()) | |
| 595 this->overflowed(); | |
| 596 return *this -= rhs.m_value; | |
| 597 } | |
| 598 | |
| 599 template <typename U, typename V> const Checked operator*=(Checked<U, V> rhs
) | |
| 600 { | |
| 601 if (rhs.hasOverflowed()) | |
| 602 this->overflowed(); | |
| 603 return *this *= rhs.m_value; | |
| 604 } | |
| 605 | |
| 606 // Equality comparisons | |
| 607 template <typename V> bool operator==(Checked<T, V> rhs) | |
| 608 { | |
| 609 return unsafeGet() == rhs.unsafeGet(); | |
| 610 } | |
| 611 | |
| 612 template <typename U> bool operator==(U rhs) | |
| 613 { | |
| 614 if (this->hasOverflowed()) | |
| 615 this->overflowed(); | |
| 616 return safeEquals(m_value, rhs); | |
| 617 } | |
| 618 | |
| 619 template <typename U, typename V> const Checked operator==(Checked<U, V> rhs
) | |
| 620 { | |
| 621 return unsafeGet() == Checked(rhs.unsafeGet()); | |
| 622 } | |
| 623 | |
| 624 template <typename U> bool operator!=(U rhs) | |
| 625 { | |
| 626 return !(*this == rhs); | |
| 627 } | |
| 628 | |
| 629 private: | |
| 630 // Disallow implicit conversion of floating point to integer types | |
| 631 Checked(float); | |
| 632 Checked(double); | |
| 633 void operator=(float); | |
| 634 void operator=(double); | |
| 635 void operator+=(float); | |
| 636 void operator+=(double); | |
| 637 void operator-=(float); | |
| 638 void operator-=(double); | |
| 639 T m_value; | |
| 640 }; | |
| 641 | |
| 642 template <typename U, typename V, typename OverflowHandler> static inline Checke
d<typename Result<U, V>::ResultType, OverflowHandler> operator+(Checked<U, Overf
lowHandler> lhs, Checked<V, OverflowHandler> rhs) | |
| 643 { | |
| 644 U x = 0; | |
| 645 V y = 0; | |
| 646 bool overflowed = lhs.safeGet(x) == CheckedState::DidOverflow || rhs.safeGet
(y) == CheckedState::DidOverflow; | |
| 647 typename Result<U, V>::ResultType result = 0; | |
| 648 overflowed |= !safeAdd(x, y, result); | |
| 649 if (overflowed) | |
| 650 return ResultOverflowed; | |
| 651 return result; | |
| 652 } | |
| 653 | |
| 654 template <typename U, typename V, typename OverflowHandler> static inline Checke
d<typename Result<U, V>::ResultType, OverflowHandler> operator-(Checked<U, Overf
lowHandler> lhs, Checked<V, OverflowHandler> rhs) | |
| 655 { | |
| 656 U x = 0; | |
| 657 V y = 0; | |
| 658 bool overflowed = lhs.safeGet(x) == CheckedState::DidOverflow || rhs.safeGet
(y) == CheckedState::DidOverflow; | |
| 659 typename Result<U, V>::ResultType result = 0; | |
| 660 overflowed |= !safeSub(x, y, result); | |
| 661 if (overflowed) | |
| 662 return ResultOverflowed; | |
| 663 return result; | |
| 664 } | |
| 665 | |
| 666 template <typename U, typename V, typename OverflowHandler> static inline Checke
d<typename Result<U, V>::ResultType, OverflowHandler> operator*(Checked<U, Overf
lowHandler> lhs, Checked<V, OverflowHandler> rhs) | |
| 667 { | |
| 668 U x = 0; | |
| 669 V y = 0; | |
| 670 bool overflowed = lhs.safeGet(x) == CheckedState::DidOverflow || rhs.safeGet
(y) == CheckedState::DidOverflow; | |
| 671 typename Result<U, V>::ResultType result = 0; | |
| 672 overflowed |= !safeMultiply(x, y, result); | |
| 673 if (overflowed) | |
| 674 return ResultOverflowed; | |
| 675 return result; | |
| 676 } | |
| 677 | |
| 678 template <typename U, typename V, typename OverflowHandler> static inline Checke
d<typename Result<U, V>::ResultType, OverflowHandler> operator+(Checked<U, Overf
lowHandler> lhs, V rhs) | |
| 679 { | |
| 680 return lhs + Checked<V, OverflowHandler>(rhs); | |
| 681 } | |
| 682 | |
| 683 template <typename U, typename V, typename OverflowHandler> static inline Checke
d<typename Result<U, V>::ResultType, OverflowHandler> operator-(Checked<U, Overf
lowHandler> lhs, V rhs) | |
| 684 { | |
| 685 return lhs - Checked<V, OverflowHandler>(rhs); | |
| 686 } | |
| 687 | |
| 688 template <typename U, typename V, typename OverflowHandler> static inline Checke
d<typename Result<U, V>::ResultType, OverflowHandler> operator*(Checked<U, Overf
lowHandler> lhs, V rhs) | |
| 689 { | |
| 690 return lhs * Checked<V, OverflowHandler>(rhs); | |
| 691 } | |
| 692 | |
| 693 template <typename U, typename V, typename OverflowHandler> static inline Checke
d<typename Result<U, V>::ResultType, OverflowHandler> operator+(U lhs, Checked<V
, OverflowHandler> rhs) | |
| 694 { | |
| 695 return Checked<U, OverflowHandler>(lhs) + rhs; | |
| 696 } | |
| 697 | |
| 698 template <typename U, typename V, typename OverflowHandler> static inline Checke
d<typename Result<U, V>::ResultType, OverflowHandler> operator-(U lhs, Checked<V
, OverflowHandler> rhs) | |
| 699 { | |
| 700 return Checked<U, OverflowHandler>(lhs) - rhs; | |
| 701 } | |
| 702 | |
| 703 template <typename U, typename V, typename OverflowHandler> static inline Checke
d<typename Result<U, V>::ResultType, OverflowHandler> operator*(U lhs, Checked<V
, OverflowHandler> rhs) | |
| 704 { | |
| 705 return Checked<U, OverflowHandler>(lhs) * rhs; | |
| 706 } | |
| 707 | |
| 708 } // namespace WTF | |
| 709 | |
| 710 using WTF::Checked; | |
| 711 using WTF::CheckedState; | |
| 712 using WTF::RecordOverflow; | |
| 713 | |
| 714 #endif | |
| OLD | NEW |