From patchwork Thu Apr 2 08:32:25 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Praveen Paneri X-Patchwork-Id: 6145191 Return-Path: X-Original-To: patchwork-intel-gfx@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 92155BF4A6 for ; Thu, 2 Apr 2015 08:28:45 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id B2F2920328 for ; Thu, 2 Apr 2015 08:28:44 +0000 (UTC) Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) by mail.kernel.org (Postfix) with ESMTP id C19AE202C8 for ; Thu, 2 Apr 2015 08:28:43 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 2DCC36E99E; Thu, 2 Apr 2015 01:28:43 -0700 (PDT) X-Original-To: intel-gfx@lists.freedesktop.org Delivered-To: intel-gfx@lists.freedesktop.org Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by gabe.freedesktop.org (Postfix) with ESMTP id B77916E99E for ; Thu, 2 Apr 2015 01:28:41 -0700 (PDT) Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by orsmga103.jf.intel.com with ESMTP; 02 Apr 2015 01:28:43 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.11,509,1422950400"; d="scan'208";a="689364170" Received: from intel-desktop.iind.intel.com ([10.223.82.37]) by fmsmga001.fm.intel.com with ESMTP; 02 Apr 2015 01:28:39 -0700 From: Praveen Paneri To: intel-gfx@lists.freedesktop.org Date: Thu, 2 Apr 2015 14:02:25 +0530 Message-Id: <1427963547-23614-11-git-send-email-praveen.paneri@intel.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1427963547-23614-1-git-send-email-praveen.paneri@intel.com> References: <1427963547-23614-1-git-send-email-praveen.paneri@intel.com> Cc: Praveen Paneri Subject: [Intel-gfx] [PATCH 10/12] xf86drmHash: Check memory allocation in HashHash() X-BeenThere: intel-gfx@lists.freedesktop.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Intel graphics driver community testing & development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: intel-gfx-bounces@lists.freedesktop.org Sender: "Intel-gfx" X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_MED, T_RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP HASH_RANDOM_INIT() 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. Signed-off-by: Praveen Paneri --- xf86drmHash.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/xf86drmHash.c b/xf86drmHash.c index 82cbc2a..56e9af3 100644 --- a/xf86drmHash.c +++ b/xf86drmHash.c @@ -70,6 +70,7 @@ #include #include +#include #define HASH_MAIN 0 @@ -79,6 +80,7 @@ #define HASH_MAGIC 0xdeadbeef #define HASH_DEBUG 0 +#define HASH_INVALID 0xffffffff /* A value that is out of bound */ #define HASH_SIZE 512 /* Good for about 100 entries */ /* If you change this value, you probably have to change the HashHash hashing @@ -137,6 +139,8 @@ static unsigned long HashHash(unsigned long key) if (!init) { HASH_RANDOM_DECL; HASH_RANDOM_INIT(37); + if (!state) + return HASH_INVALID; for (i = 0; i < 256; i++) scatter[i] = HASH_RANDOM; HASH_RANDOM_DESTROY; ++init; @@ -203,6 +207,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) { @@ -244,6 +251,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 = HASH_ALLOC(sizeof(*bucket)); if (!bucket) return -1; /* Error */ @@ -267,6 +275,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;