OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef BASE_NUMERICS_SAFE_MATH_H_ | 5 #ifndef BASE_NUMERICS_SAFE_MATH_H_ |
6 #define BASE_NUMERICS_SAFE_MATH_H_ | 6 #define BASE_NUMERICS_SAFE_MATH_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 | 9 |
10 #include "base/numerics/safe_math_impl.h" | 10 #include "base/numerics/safe_math_impl.h" |
(...skipping 20 matching lines...) Expand all Loading... |
31 // | 31 // |
32 // Bitwise operations are explicitly not supported, because correct | 32 // Bitwise operations are explicitly not supported, because correct |
33 // handling of some cases (e.g. sign manipulation) is ambiguous. Comparison | 33 // handling of some cases (e.g. sign manipulation) is ambiguous. Comparison |
34 // operations are explicitly not supported because they could result in a crash | 34 // operations are explicitly not supported because they could result in a crash |
35 // on a CHECK condition. You should use patterns like the following for these | 35 // on a CHECK condition. You should use patterns like the following for these |
36 // operations: | 36 // operations: |
37 // Bitwise operation: | 37 // Bitwise operation: |
38 // CheckedNumeric<int> checked_int = untrusted_input_value; | 38 // CheckedNumeric<int> checked_int = untrusted_input_value; |
39 // int x = checked_int.ValueOrDefault(0) | kFlagValues; | 39 // int x = checked_int.ValueOrDefault(0) | kFlagValues; |
40 // Comparison: | 40 // Comparison: |
41 // CheckedNumeric<size_t> checked_size; | 41 // CheckedNumeric<size_t> checked_size = untrusted_input_value; |
42 // CheckedNumeric<int> checked_size = untrusted_input_value; | 42 // checked_size += HEADER LENGTH; |
43 // checked_size = checked_size + HEADER LENGTH; | |
44 // if (checked_size.IsValid() && checked_size.ValueOrDie() < buffer_size) | 43 // if (checked_size.IsValid() && checked_size.ValueOrDie() < buffer_size) |
45 // Do stuff... | 44 // Do stuff... |
46 template <typename T> | 45 template <typename T> |
47 class CheckedNumeric { | 46 class CheckedNumeric { |
48 public: | 47 public: |
49 typedef T type; | 48 typedef T type; |
50 | 49 |
51 CheckedNumeric() {} | 50 CheckedNumeric() {} |
52 | 51 |
53 // Copy constructor. | 52 // Copy constructor. |
(...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
291 | 290 |
292 #undef BASE_NUMERIC_ARITHMETIC_OPERATORS | 291 #undef BASE_NUMERIC_ARITHMETIC_OPERATORS |
293 | 292 |
294 } // namespace internal | 293 } // namespace internal |
295 | 294 |
296 using internal::CheckedNumeric; | 295 using internal::CheckedNumeric; |
297 | 296 |
298 } // namespace base | 297 } // namespace base |
299 | 298 |
300 #endif // BASE_NUMERICS_SAFE_MATH_H_ | 299 #endif // BASE_NUMERICS_SAFE_MATH_H_ |
OLD | NEW |