Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(120)

Side by Side Diff: ui/gfx/geometry/safe_integer_conversions.h

Issue 2354783004: Fix overflow/underflow in gfx geometry once and for all (Closed)
Patch Set: Add additional inset test Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 UI_GFX_GEOMETRY_SAFE_INTEGER_CONVERSIONS_H_ 5 #ifndef UI_GFX_GEOMETRY_SAFE_INTEGER_CONVERSIONS_H_
6 #define UI_GFX_GEOMETRY_SAFE_INTEGER_CONVERSIONS_H_ 6 #define UI_GFX_GEOMETRY_SAFE_INTEGER_CONVERSIONS_H_
7 7
8 #include <cmath> 8 #include <cmath>
9 #include <limits> 9 #include <limits>
10 10
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
50 inline bool IsExpressibleAsInt(float value) { 50 inline bool IsExpressibleAsInt(float value) {
51 if (value != value) 51 if (value != value)
52 return false; // no int NaN. 52 return false; // no int NaN.
53 if (value > std::numeric_limits<int>::max()) 53 if (value > std::numeric_limits<int>::max())
54 return false; 54 return false;
55 if (value < std::numeric_limits<int>::min()) 55 if (value < std::numeric_limits<int>::min())
56 return false; 56 return false;
57 return true; 57 return true;
58 } 58 }
59 59
60 // Returns true iff a+b would overflow max int.
61 constexpr bool AddWouldOverflow(int a, int b) {
62 return a > 0 && b > std::numeric_limits<int>::max() - a;
63 }
64
65 // Returns true iff a+b would underflow min int.
66 constexpr bool AddWouldUnderflow(int a, int b) {
67 return a < 0 && b < std::numeric_limits<int>::min() - a;
68 }
69
70 // Returns true iff a-b would overflow max int.
71 constexpr bool SubtractWouldOverflow(int a, int b) {
72 return b < 0 && a > std::numeric_limits<int>::max() + b;
73 }
74
75 // Returns true iff a-b would underflow min int.
76 constexpr bool SubtractWouldUnderflow(int a, int b) {
77 return b > 0 && a < std::numeric_limits<int>::min() + b;
78 }
79
80 // Safely adds a+b without integer overflow/underflow. Exceeding these
81 // bounds will clamp to the max or min int limit.
82 constexpr int SafeAdd(int a, int b) {
83 return AddWouldOverflow(a, b)
84 ? std::numeric_limits<int>::max()
85 : AddWouldUnderflow(a, b) ? std::numeric_limits<int>::min()
86 : a + b;
87 }
88
89 // Safely subtracts a-b without integer overflow/underflow. Exceeding these
90 // bounds will clamp to the max or min int limit.
91 constexpr int SafeSubtract(int a, int b) {
92 return SubtractWouldOverflow(a, b)
93 ? std::numeric_limits<int>::max()
94 : SubtractWouldUnderflow(a, b) ? std::numeric_limits<int>::min()
95 : a - b;
96 }
97
60 } // namespace gfx 98 } // namespace gfx
61 99
62 #endif // UI_GFX_GEOMETRY_SAFE_INTEGER_CONVERSIONS_H_ 100 #endif // UI_GFX_GEOMETRY_SAFE_INTEGER_CONVERSIONS_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698