[10/11] xf86drmHash: Check memory allocation in HashHash()

Submitted by Praveen Paneri on April 10, 2015, 8:43 a.m.

Details

Message ID 1428655383-26087-11-git-send-email-praveen.paneri@intel.com
State New
Headers show

Not browsing as part of any series.

Commit Message

Praveen Paneri April 10, 2015, 8:43 a.m.
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@intel.com>
---
 xf86drmHash.c | 9 +++++++++
 1 file changed, 9 insertions(+)

Patch hide | download patch | download mbox

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;