Skip to contentSkip to conversation

staging: bcm2835-camera: free first element in array

PATCH In review Dan Carpenter 2 participants 5 messages Feb 15, 2017 – Feb 18, 2017

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

PATCH 1/1 staging: bcm2835-camera: free first element in array

We should free gdev[0] so the > should be >=.

Fixes 7b3ad5abf027 ("staging: Import the BCM2835 MMAL-based V4L2 camera driver.")
drivers/staging/media/platform/bcm2835/bcm2835-camera.c +1 -1
index ca15a698e018..9bcd8e546a14 100644
@@ -1998,7 +1998,7 @@ static int __init bm2835_mmal_init(void)
 free_dev:
 	kfree(dev);
 
-	for ( ; camera > 0; camera--) {
+	for ( ; camera >= 0; camera--) {
 		bcm2835_cleanup_instance(gdev[camera]);
 		gdev[camera] = NULL;
 	}
#1 lore raw

Am 15.02.2017 13:25, schrieb Dan Carpenter:

Quoted 10 lines

We should free gdev[0] so the > should be >=.

Fixes 7b3ad5abf027 ("staging: Import the BCM2835 MMAL-based V4L2 camera driver.")
drivers/staging/media/platform/bcm2835/bcm2835-camera.c +0 -0
index ca15a698e018..9bcd8e546a14 100644

drivers/staging/media/platform/bcm2835/bcm2835-camera.c static int __init bm2835_mmal_init(void) · line 2004

Quoted 8 lines
drivers/staging/media/platform/bcm2835/bcm2835-camera.c +1 -1
@@ -1998,7 +1998,7 @@ static int __init bm2835_mmal_init(void)
 free_dev:
 	kfree(dev);
 
-	for ( ; camera > 0; camera--) {
+	for ( ; camera >= 0; camera--) {
 		bcm2835_cleanup_instance(gdev[camera]);
 		gdev[camera] = NULL;
 	}

since we already know that programmers are bad in counting backwards ...

is is possible to change that into std. loop like:

 for(i=0, i< camera; i++ {
	bcm2835_cleanup_instance(gdev[i]);
	gdev[i] = NULL;

}

this is way a much more common pattern.

just my 2 cents,
re,
wh

#2 lore raw
Quoted 24 lines · walter harms

Am 15.02.2017 13:25, schrieb Dan Carpenter:

Quoted 10 lines

We should free gdev[0] so the > should be >=.

Fixes 7b3ad5abf027 ("staging: Import the BCM2835 MMAL-based V4L2 camera driver.")
drivers/staging/media/platform/bcm2835/bcm2835-camera.c +0 -0
index ca15a698e018..9bcd8e546a14 100644

drivers/staging/media/platform/bcm2835/bcm2835-camera.c static int __init bm2835_mmal_init(void) · line 2004

Quoted 8 lines
drivers/staging/media/platform/bcm2835/bcm2835-camera.c +1 -1
@@ -1998,7 +1998,7 @@ static int __init bm2835_mmal_init(void)
 free_dev:
 	kfree(dev);
 
-	for ( ; camera > 0; camera--) {
+	for ( ; camera >= 0; camera--) {
 		bcm2835_cleanup_instance(gdev[camera]);
 		gdev[camera] = NULL;
 	}

since we already know that programmers are bad in counting backwards ...

Quoted 8 lines

is is possible to change that into std. loop like:

 for(i=0, i< camera; i++ {
	bcm2835_cleanup_instance(gdev[i]);
	gdev[i] = NULL;

}

this is way a much more common pattern.

Hm... My patch is buggy. It frees the wong thing on the first iteration through the loop. I'll resend.

regards,
dan carpenter

#3 lore raw

PATCH v2 1/1 staging: bcm2835-camera: fix error handling in init

In reply to walter harms

The unwinding here isn't right. We don't free gdev[0] and instead free 1 step past what was allocated. Also we can't allocate "dev" then we should unwind instead of returning directly.

Fixes 7b3ad5abf027 ("staging: Import the BCM2835 MMAL-based V4L2 camera driver.")

---
v2: Change the style to make Walter Harms happy. Fix some additional
bugs I missed in the first patch.

drivers/staging/media/platform/bcm2835/bcm2835-camera.c +9 -5
index ca15a698e018..c4dad30dd133 100644
@@ -1901,6 +1901,7 @@ static int __init bm2835_mmal_init(void)
 	unsigned int num_cameras;
 	struct vchiq_mmal_instance *instance;
 	unsigned int resolutions[MAX_BCM2835_CAMERAS][2];
+	int i;
 
 	ret = vchiq_mmal_init(&instance);
 	if (ret < 0)
@@ -1914,8 +1915,10 @@ static int __init bm2835_mmal_init(void)
 
 	for (camera = 0; camera < num_cameras; camera++) {
 		dev = kzalloc(sizeof(struct bm2835_mmal_dev), GFP_KERNEL);
-		if (!dev)
-			return -ENOMEM;
+		if (!dev) {
+			ret = -ENOMEM;
+			goto cleanup_gdev;
+		}
 
 		dev->camera_num = camera;
 		dev->max_width = resolutions[camera][0];
@@ -1998,9 +2001,10 @@ static int __init bm2835_mmal_init(void)
 free_dev:
 	kfree(dev);
 
-	for ( ; camera > 0; camera--) {
-		bcm2835_cleanup_instance(gdev[camera]);
-		gdev[camera] = NULL;
+cleanup_gdev:
+	for (i = 0; i < camera; i++) {
+		bcm2835_cleanup_instance(gdev[i]);
+		gdev[i] = NULL;
 	}
 	pr_info("%s: error %d while loading driver\n",
 		BM2835_MMAL_MODULE_NAME, ret);
Acked-by on v2 1/1
#4 lore raw

Re: [patch v2] staging: bcm2835-camera: fix error handling in init

looks more readable now :)

Am 18.02.2017 00:20, schrieb Dan Carpenter:

Quoted 54 lines

The unwinding here isn't right. We don't free gdev[0] and instead free 1 step past what was allocated. Also we can't allocate "dev" then we should unwind instead of returning directly.

Fixes 7b3ad5abf027 ("staging: Import the BCM2835 MMAL-based V4L2 camera driver.")

---
v2: Change the style to make Walter Harms happy. Fix some additional
bugs I missed in the first patch.

drivers/staging/media/platform/bcm2835/bcm2835-camera.c +9 -6
index ca15a698e018..c4dad30dd133 100644
@@ -1901,6 +1901,7 @@ static int __init bm2835_mmal_init(void)
 	unsigned int num_cameras;
 	struct vchiq_mmal_instance *instance;
 	unsigned int resolutions[MAX_BCM2835_CAMERAS][2];
+	int i;
 
 	ret = vchiq_mmal_init(&instance);
 	if (ret < 0)
@@ -1914,8 +1915,10 @@ static int __init bm2835_mmal_init(void)
 
 	for (camera = 0; camera < num_cameras; camera++) {
 		dev = kzalloc(sizeof(struct bm2835_mmal_dev), GFP_KERNEL);
-		if (!dev)
-			return -ENOMEM;
+		if (!dev) {
+			ret = -ENOMEM;
+			goto cleanup_gdev;
+		}
 
 		dev->camera_num = camera;
 		dev->max_width = resolutions[camera][0];
@@ -1998,9 +2001,10 @@ static int __init bm2835_mmal_init(void)
 free_dev:
 	kfree(dev);
 
-	for ( ; camera > 0; camera--) {
-		bcm2835_cleanup_instance(gdev[camera]);
-		gdev[camera] = NULL;
+cleanup_gdev:
+	for (i = 0; i < camera; i++) {
+		bcm2835_cleanup_instance(gdev[i]);
+		gdev[i] = NULL;
 	}
 	pr_info("%s: error %d while loading driver\n",
 		BM2835_MMAL_MODULE_NAME, ret);
--

To unsubscribe from this list: send the line "unsubscribe kernel-janitors" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html