[weston,v2] screenshooter: fix various memory handling

Submitted by Marek Chalupa on Dec. 10, 2014, 10:50 a.m.

Details

Message ID 1418208646-28858-1-git-send-email-mchqwerty@gmail.com
State Accepted
Commit ab9285bf8a7a866daa7b4efc9f27181ea4fdc719
Headers show

Not browsing as part of any series.

Commit Message

Marek Chalupa Dec. 10, 2014, 10:50 a.m.
There were unchecked malloc and no free for this memory.
Also simplify error handling in one function.

v2. remove check if memory is NULL, according to man pages,
    free(NULL) is a no-op

Signed-off-by: Marek Chalupa <mchqwerty@gmail.com>
---
 src/screenshooter.c | 25 +++++++++++++++++--------
 1 file changed, 17 insertions(+), 8 deletions(-)

Patch hide | download patch | download mbox

diff --git a/src/screenshooter.c b/src/screenshooter.c
index 6246cda..c98e2f9 100644
--- a/src/screenshooter.c
+++ b/src/screenshooter.c
@@ -464,8 +464,9 @@  weston_recorder_free(struct weston_recorder *recorder)
 {
 	if (recorder == NULL)
 		return;
-	free(recorder->rect);
+
 	free(recorder->tmpbuf);
+	free(recorder->rect);
 	free(recorder->frame);
 	free(recorder);
 }
@@ -495,12 +496,16 @@  weston_recorder_create(struct weston_output *output, const char *filename)
 
 	if ((recorder->frame == NULL) || (recorder->rect == NULL)) {
 		weston_log("%s: out of memory\n", __func__);
-		weston_recorder_free(recorder);
-		return;
+		goto err_recorder;
 	}
 
-	if (!do_yflip)
+	if (!do_yflip) {
 		recorder->tmpbuf = malloc(size);
+		if (recorder->tmpbuf == NULL) {
+			weston_log("%s: out of memory\n", __func__);
+			goto err_recorder;
+		}
+	}
 
 	header.magic = WCAP_HEADER_MAGIC;
 
@@ -514,8 +519,7 @@  weston_recorder_create(struct weston_output *output, const char *filename)
 		break;
 	default:
 		weston_log("unknown recorder format\n");
-		weston_recorder_free(recorder);
-		return;
+		goto err_recorder;
 	}
 
 	recorder->fd = open(filename,
@@ -523,8 +527,7 @@  weston_recorder_create(struct weston_output *output, const char *filename)
 
 	if (recorder->fd < 0) {
 		weston_log("problem opening output file %s: %m\n", filename);
-		weston_recorder_free(recorder);
-		return;
+		goto err_recorder;
 	}
 
 	header.width = output->current_mode->width;
@@ -535,6 +538,12 @@  weston_recorder_create(struct weston_output *output, const char *filename)
 	wl_signal_add(&output->frame_signal, &recorder->frame_listener);
 	output->disable_planes++;
 	weston_output_damage(output);
+
+	return;
+
+err_recorder:
+	weston_recorder_free(recorder);
+	return;
 }
 
 static void

Comments

On Wed, 10 Dec 2014 11:50:46 +0100
Marek Chalupa <mchqwerty@gmail.com> wrote:

> There were unchecked malloc and no free for this memory.
> Also simplify error handling in one function.
> 
> v2. remove check if memory is NULL, according to man pages,
>     free(NULL) is a no-op
> 
> Signed-off-by: Marek Chalupa <mchqwerty@gmail.com>
> ---
>  src/screenshooter.c | 25 +++++++++++++++++--------
>  1 file changed, 17 insertions(+), 8 deletions(-)

Pushed, thanks,
pq