summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNick Kralevich2015-08-22 16:27:03 -0500
committerNick Kralevich2015-08-24 10:52:22 -0500
commit1f286989986a384e34d9de1acf8899439506695a (patch)
treeca15d9ff0bce7642a7b479002867cac5d0063b15 /libutils/JenkinsHash.cpp
parent3b94cef1b75893f98ad3011a09582405bb32baf4 (diff)
downloadplatform-system-core-1f286989986a384e34d9de1acf8899439506695a.tar.gz
platform-system-core-1f286989986a384e34d9de1acf8899439506695a.tar.xz
platform-system-core-1f286989986a384e34d9de1acf8899439506695a.zip
libutils: cleanups for -fsanitize=integer
* Hashing functions rely on integer overflow behavior. Mark those functions as safe. * abort() if a passed in size_t value is greater than UINT32_MAX. This can occur on 64 bit builds where size_t is larger than uint32_t. * Special case the index lookup for an empty sorted vector. Without the special case, size() == 0, and size()-1 underflows. Change-Id: I343a14b589fc8f0d221c1998ae5d6f0b9e2781f8
Diffstat (limited to 'libutils/JenkinsHash.cpp')
-rw-r--r--libutils/JenkinsHash.cpp10
1 files changed, 10 insertions, 0 deletions
diff --git a/libutils/JenkinsHash.cpp b/libutils/JenkinsHash.cpp
index 52c9bb7df..ff5d252a4 100644
--- a/libutils/JenkinsHash.cpp
+++ b/libutils/JenkinsHash.cpp
@@ -19,10 +19,14 @@
19 * should still be quite good. 19 * should still be quite good.
20 **/ 20 **/
21 21
22#include <stdlib.h>
22#include <utils/JenkinsHash.h> 23#include <utils/JenkinsHash.h>
23 24
24namespace android { 25namespace android {
25 26
27#ifdef __clang__
28__attribute__((no_sanitize("integer")))
29#endif
26hash_t JenkinsHashWhiten(uint32_t hash) { 30hash_t JenkinsHashWhiten(uint32_t hash) {
27 hash += (hash << 3); 31 hash += (hash << 3);
28 hash ^= (hash >> 11); 32 hash ^= (hash >> 11);
@@ -31,6 +35,9 @@ hash_t JenkinsHashWhiten(uint32_t hash) {
31} 35}
32 36
33uint32_t JenkinsHashMixBytes(uint32_t hash, const uint8_t* bytes, size_t size) { 37uint32_t JenkinsHashMixBytes(uint32_t hash, const uint8_t* bytes, size_t size) {
38 if (size > UINT32_MAX) {
39 abort();
40 }
34 hash = JenkinsHashMix(hash, (uint32_t)size); 41 hash = JenkinsHashMix(hash, (uint32_t)size);
35 size_t i; 42 size_t i;
36 for (i = 0; i < (size & -4); i += 4) { 43 for (i = 0; i < (size & -4); i += 4) {
@@ -47,6 +54,9 @@ uint32_t JenkinsHashMixBytes(uint32_t hash, const uint8_t* bytes, size_t size) {
47} 54}
48 55
49uint32_t JenkinsHashMixShorts(uint32_t hash, const uint16_t* shorts, size_t size) { 56uint32_t JenkinsHashMixShorts(uint32_t hash, const uint16_t* shorts, size_t size) {
57 if (size > UINT32_MAX) {
58 abort();
59 }
50 hash = JenkinsHashMix(hash, (uint32_t)size); 60 hash = JenkinsHashMix(hash, (uint32_t)size);
51 size_t i; 61 size_t i;
52 for (i = 0; i < (size & -2); i += 2) { 62 for (i = 0; i < (size & -2); i += 2) {