diff options
author | Janis Danisevskis <jdanis@google.com> | 2018-06-17 14:06:20 -0700 |
---|---|---|
committer | Janis Danisevskis <jdanis@google.com> | 2019-03-19 09:54:04 -0700 |
commit | 93c7276e3adf8bbd67e62925abf4efdb24ebdb1b (patch) | |
tree | 6f7c04e103ecc0d5fd029422642391aaced7f6fc /keymaster | |
parent | 9c9fea329e87e8535a41b1071b3fb24fdd19a135 (diff) | |
download | android_hardware_interfaces-93c7276e3adf8bbd67e62925abf4efdb24ebdb1b.tar.gz android_hardware_interfaces-93c7276e3adf8bbd67e62925abf4efdb24ebdb1b.tar.bz2 android_hardware_interfaces-93c7276e3adf8bbd67e62925abf4efdb24ebdb1b.zip |
Fix strict weak ordering requirement of less than operation
operator< on hidl_vec<uint8_t> violates strict weak ordering in the case
that one oparand is shorter that the other and the shorter is a prefix
of the longer.
if x and y are incomparable, i.e., neither x < y nor y < x and
y and z are incomparable, i.e., neither y < z nor z < y, then
x and z must be incomparable.
As for the current implementation the first two statements are true but
the third is not given the following example input:
x:="aa", y:="a", z:="ab".
This patch fixes the issue by defining a < b if a is a prefix of b.
As this relation is used in a std::sort algorithm which demands strict
weak ordering this bug leads to undefined behavior.
Change-Id: I4961bb35e2fd4f5fcf561ec0c7c536f81830aab8
Diffstat (limited to 'keymaster')
-rw-r--r-- | keymaster/4.0/support/keymaster_utils.cpp | 4 |
1 files changed, 3 insertions, 1 deletions
diff --git a/keymaster/4.0/support/keymaster_utils.cpp b/keymaster/4.0/support/keymaster_utils.cpp index 729e1c1db..e35fdd36d 100644 --- a/keymaster/4.0/support/keymaster_utils.cpp +++ b/keymaster/4.0/support/keymaster_utils.cpp @@ -21,7 +21,9 @@ namespace android { namespace hardware { inline static bool operator<(const hidl_vec<uint8_t>& a, const hidl_vec<uint8_t>& b) { - return memcmp(a.data(), b.data(), std::min(a.size(), b.size())) == -1; + auto result = memcmp(a.data(), b.data(), std::min(a.size(), b.size())); + if (!result) return a.size() < b.size(); + return result < 0; } template <size_t SIZE> |