Skip to contentSkip to conversation

staging: bcm2835-camera: fix coding style issues

PATCH New Aishwarya Pant 1 participant 4 messages Mar 9, 2017

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v2 cover staging: bcm2835-camera: fix coding style issues

This patchset fixes the following issues:

[1] Added null check after memory allocation to avoid a possible null pointer dereference
[2] Replaced instances of kmalloc and memset with a kzalloc
[3] Replaced null error return values with kernel preferred error return values i.e. return ERR_PTR(-ENOMEM) instead of NULL

Changes in v2:
-- Fix null dereference issue (ouch!)
-- Rebase from origin

Aishwarya Pant (3):
staging: bcm2835-camera: add check to avoid null pointer dereference
staging: bcm2835-camera: replace kmalloc with kzalloc
staging: bcm2835-camera: use kernel preferred style for handling
errors

 .../vc04_services/bcm2835-camera/mmal-vchiq.c      | 24 +++++++++++++---------
 1 file changed, 14 insertions(+), 10 deletions(-)
Signature · 1 line
2.7.4
#1 lore raw

PATCH v2 1/3 staging: bcm2835-camera: add check to avoid null pointer dereference

This patch adds checks after memory allocation to avoid possible null pointer dereferences.

---
Changes in v2:
-- Fix null dereference in memset

 drivers/staging/vc04_services/bcm2835-camera/mmal-vchiq.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
drivers/staging/vc04_services/bcm2835-camera/mmal-vchiq.c +8 -2
index 6126919..4eead48 100644
@@ -261,8 +261,11 @@ get_msg_context(struct vchiq_mmal_instance *instance)
 
 	/* todo: should this be allocated from a pool to avoid kmalloc */
 	msg_context = kmalloc(sizeof(*msg_context), GFP_KERNEL);
-	memset(msg_context, 0, sizeof(*msg_context));
 
+	if (!msg_context)
+		return NULL;
+
+	memset(msg_context, 0, sizeof(*msg_context));
 	msg_context->instance = instance;
 	msg_context->handle =
 		mmal_context_map_create_handle(&instance->context_map,
@@ -2006,8 +2009,11 @@ int vchiq_mmal_init(struct vchiq_mmal_instance **out_instance)
 	}
 
 	instance = kmalloc(sizeof(*instance), GFP_KERNEL);
-	memset(instance, 0, sizeof(*instance));
 
+	if (!instance)
+		return -ENOMEM;
+
+	memset(instance, 0, sizeof(*instance));
 	mutex_init(&instance->vchiq_mutex);
 	mutex_init(&instance->bulk_mutex);
 
Signature · 1 line
2.7.4
#2 lore raw

PATCH v2 2/3 staging: bcm2835-camera: replace kmalloc with kzalloc

In reply to Aishwarya Pant

This patch replaces kmalloc and memset with kzalloc

---
 drivers/staging/vc04_services/bcm2835-camera/mmal-vchiq.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
drivers/staging/vc04_services/bcm2835-camera/mmal-vchiq.c +3 -5
index 4eead48..f795137 100644
@@ -259,13 +259,12 @@ get_msg_context(struct vchiq_mmal_instance *instance)
 {
 	struct mmal_msg_context *msg_context;
 
-	/* todo: should this be allocated from a pool to avoid kmalloc */
-	msg_context = kmalloc(sizeof(*msg_context), GFP_KERNEL);
+	/* todo: should this be allocated from a pool to avoid kzalloc */
+	msg_context = kzalloc(sizeof(*msg_context), GFP_KERNEL);
 
 	if (!msg_context)
 		return NULL;
 
-	memset(msg_context, 0, sizeof(*msg_context));
 	msg_context->instance = instance;
 	msg_context->handle =
 		mmal_context_map_create_handle(&instance->context_map,
@@ -2008,12 +2007,11 @@ int vchiq_mmal_init(struct vchiq_mmal_instance **out_instance)
 		return -EIO;
 	}
 
-	instance = kmalloc(sizeof(*instance), GFP_KERNEL);
+	instance = kzalloc(sizeof(*instance), GFP_KERNEL);
 
 	if (!instance)
 		return -ENOMEM;
 
-	memset(instance, 0, sizeof(*instance));
 	mutex_init(&instance->vchiq_mutex);
 	mutex_init(&instance->bulk_mutex);
 
Signature · 1 line
2.7.4
#3 lore raw

PATCH v2 3/3 staging: bcm2835-camera: use kernel preferred style for handling errors

In reply to Aishwarya Pant

This patch replaces NULL error values with error pointer values.

---
 drivers/staging/vc04_services/bcm2835-camera/mmal-vchiq.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
drivers/staging/vc04_services/bcm2835-camera/mmal-vchiq.c +6 -6
index f795137..d87e2df 100644
@@ -263,7 +263,7 @@ get_msg_context(struct vchiq_mmal_instance *instance)
 	msg_context = kzalloc(sizeof(*msg_context), GFP_KERNEL);
 
 	if (!msg_context)
-		return NULL;
+		return ERR_PTR(-ENOMEM);
 
 	msg_context->instance = instance;
 	msg_context->handle =
@@ -273,7 +273,7 @@ get_msg_context(struct vchiq_mmal_instance *instance)
 
 	if (!msg_context->handle) {
 		kfree(msg_context);
-		return NULL;
+		return ERR_PTR(-ENOMEM);
 	}
 
 	return msg_context;
@@ -508,8 +508,8 @@ buffer_from_host(struct vchiq_mmal_instance *instance,
 
 	/* get context */
 	msg_context = get_msg_context(instance);
-	if (!msg_context) {
-		ret = -ENOMEM;
+	if (IS_ERR(msg_context)) {
+		ret = PTR_ERR(msg_context);
 		goto unlock;
 	}
 
@@ -836,8 +836,8 @@ static int send_synchronous_mmal_msg(struct vchiq_mmal_instance *instance,
 	}
 
 	msg_context = get_msg_context(instance);
-	if (!msg_context)
-		return -ENOMEM;
+	if (IS_ERR(msg_context))
+		return PTR_ERR(msg_context);
 
 	init_completion(&msg_context->u.sync.cmplt);
 
Signature · 1 line
2.7.4