[Intel-gfx] [PATCH 10/11] xf86drmHash: Check memory allocation in HashHash()

Praveen Paneri praveen.paneri at intel.com
Fri Apr 10 01:43:02 PDT 2015


drmRandomCreate() can fail to allocate memory. In such case
return an invalid hash value (0xffffffff) from HashHash()
function.
Caller functions check the hash value and act accordingly.

v2: Rebased to the latest.

Signed-off-by: Praveen Paneri <praveen.paneri at intel.com>
---
 xf86drmHash.c | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/xf86drmHash.c b/xf86drmHash.c
index f287e61..0610838 100644
--- a/xf86drmHash.c
+++ b/xf86drmHash.c
@@ -75,6 +75,8 @@
 #include "xf86drmHash.h"
 
 #define HASH_MAGIC 0xdeadbeef
+#define HASH_INVALID 0xffffffff	/* A value that is out of bound */
+
 
 static unsigned long HashHash(unsigned long key)
 {
@@ -87,6 +89,8 @@ static unsigned long HashHash(unsigned long key)
     if (!init) {
 	void *state;
 	state = drmRandomCreate(37);
+	if (!state)
+		return HASH_INVALID;
 	for (i = 0; i < 256; i++) scatter[i] = drmRandom(state);
 	drmRandomDestroy(state);
 	++init;
@@ -153,6 +157,9 @@ static HashBucketPtr HashFind(HashTablePtr table,
 
     if (h) *h = hash;
 
+    if (hash == HASH_INVALID)
+	return NULL;
+
     for (bucket = table->buckets[hash]; bucket; bucket = bucket->next) {
 	if (bucket->key == key) {
 	    if (prev) {
@@ -194,6 +201,7 @@ int drmHashInsert(void *t, unsigned long key, void *value)
     if (table->magic != HASH_MAGIC) return -1; /* Bad magic */
 
     if (HashFind(table, key, &hash)) return 1; /* Already in table */
+    if (hash == HASH_INVALID) return -1;
 
     bucket               = drmMalloc(sizeof(*bucket));
     if (!bucket) return -1;	/* Error */
@@ -217,6 +225,7 @@ int drmHashDelete(void *t, unsigned long key)
 
     bucket = HashFind(table, key, &hash);
 
+    if (hash == HASH_INVALID) return -1;
     if (!bucket) return 1;	/* Not found */
 
     table->buckets[hash] = bucket->next;
-- 
1.9.1



More information about the Intel-gfx mailing list