diff options
author | David Staessens <dstaessens@google.com> | 2021-05-12 05:56:13 +0000 |
---|---|---|
committer | Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com> | 2021-05-12 05:56:13 +0000 |
commit | 1a5a53b20f1b8bbc2b6e503e34296edf65a40a36 (patch) | |
tree | 7f1fd3005b83402217d940f43ca58c106479816e | |
parent | da0126036dd0b041e286350475c70bdfe5ca5489 (diff) | |
parent | 9db55159cc96f7bc1b73ee3ee816f6f0c9c03e95 (diff) | |
download | v4l2_codec2-1a5a53b20f1b8bbc2b6e503e34296edf65a40a36.tar.gz |
v4l2_codec2: Adapt overflow check in common.cpp. am: 9db55159cc
Original change: https://googleplex-android-review.googlesource.com/c/platform/external/v4l2_codec2/+/14511929
Change-Id: Ibe876dfa1152be287e23b659edc711419673e60e
-rw-r--r-- | common/Common.cpp | 17 |
1 files changed, 5 insertions, 12 deletions
diff --git a/common/Common.cpp b/common/Common.cpp index a444c3a..79243ec 100644 --- a/common/Common.cpp +++ b/common/Common.cpp @@ -4,18 +4,9 @@ #include <v4l2_codec2/common/Common.h> -namespace android { +#include <base/numerics/safe_math.h> -namespace { -std::optional<int> checkedMul(int a, int b) { - int32_t result = a * b; - // Check whether multiplication caused an overflow. - if ((a != 0) && ((result / a) != b)) { - return std::nullopt; - } - return result; -} -} // namespace +namespace android { bool contains(const Rect& rect1, const Rect& rect2) { return (rect2.left >= rect1.left && rect2.right <= rect1.right && rect2.top >= rect1.top && @@ -28,7 +19,9 @@ std::string toString(const Rect& rect) { } std::optional<int> getArea(const ui::Size& size) { - return checkedMul(size.width, size.height); + base::CheckedNumeric<int> checked_area = size.width; + checked_area *= size.height; + return checked_area.IsValid() ? std::optional<int>(checked_area.ValueOrDie()) : std::nullopt; } bool isEmpty(const ui::Size& size) { |