[07/11] intel: Validate memory allocations

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

Details

Message ID 1428655383-26087-8-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:42 a.m.
This patch adds check for various malloc/calloc function if they
were able to allocate memory as requested or not. Return
appropriate error if the allocation fails.

v2: Rebased to the latest.

Signed-off-by: Praveen Paneri <praveen.paneri@intel.com>
---
 intel/intel_bufmgr_fake.c | 4 ++++
 intel/intel_bufmgr_gem.c  | 3 +++
 intel/intel_decode.c      | 2 ++
 3 files changed, 9 insertions(+)

Patch hide | download patch | download mbox

diff --git a/intel/intel_bufmgr_fake.c b/intel/intel_bufmgr_fake.c
index 129d344..e2b25eb 100644
--- a/intel/intel_bufmgr_fake.c
+++ b/intel/intel_bufmgr_fake.c
@@ -1278,6 +1278,8 @@  drm_intel_fake_emit_reloc(drm_intel_bo *bo, uint32_t offset,
 	if (bo_fake->relocs == NULL) {
 		bo_fake->relocs =
 		    malloc(sizeof(struct fake_buffer_reloc) * MAX_RELOCS);
+		if (!bo_fake->relocs)
+			return -ENOMEM;
 	}
 
 	r = &bo_fake->relocs[bo_fake->nr_relocs++];
@@ -1597,6 +1599,8 @@  drm_intel_bufmgr_fake_init(int fd, unsigned long low_offset,
 	drm_intel_bufmgr_fake *bufmgr_fake;
 
 	bufmgr_fake = calloc(1, sizeof(*bufmgr_fake));
+	if (!bufmgr_fake)
+		return NULL;
 
 	if (pthread_mutex_init(&bufmgr_fake->lock, NULL) != 0) {
 		free(bufmgr_fake);
diff --git a/intel/intel_bufmgr_gem.c b/intel/intel_bufmgr_gem.c
index 91320ca..6180c6e 100644
--- a/intel/intel_bufmgr_gem.c
+++ b/intel/intel_bufmgr_gem.c
@@ -2081,6 +2081,9 @@  aub_write_bo_data(drm_intel_bo *bo, uint32_t offset, uint32_t size)
 	unsigned int i;
 
 	data = malloc(bo->size);
+	if (!data)
+		return;
+
 	drm_intel_bo_get_subdata(bo, offset, size, data);
 
 	/* Easy mode: write out bo with no relocations */
diff --git a/intel/intel_decode.c b/intel/intel_decode.c
index 5dab9ca..88267fd 100644
--- a/intel/intel_decode.c
+++ b/intel/intel_decode.c
@@ -3914,6 +3914,8 @@  drm_intel_decode(struct drm_intel_decode *ctx)
 	 * checking in statically sized packets.
 	 */
 	temp = malloc(size + 4096);
+	if (!temp)
+		return;
 	memcpy(temp, ctx->base_data, size);
 	memset((char *)temp + size, 0xd0, 4096);
 	ctx->data = temp;