spice/0007-server-mjpeg_encoder-Fix-memory-leak-for-the-inital-.patch

69 lines
2.3 KiB
Diff

From 1ba5d956168b0d0b9a9f16a843bd1b13cf897e78 Mon Sep 17 00:00:00 2001
From: Yonit Halperin <yhalperi@redhat.com>
Date: Thu, 10 May 2012 12:26:01 +0300
Subject: [PATCH 7/8] server/mjpeg_encoder: Fix memory leak for the inital
output buffer given for each frame
---
server/mjpeg_encoder.c | 11 +++--------
server/mjpeg_encoder.h | 5 +++++
2 files changed, 8 insertions(+), 8 deletions(-)
diff --git a/server/mjpeg_encoder.c b/server/mjpeg_encoder.c
index 6b68549..74062f5 100644
--- a/server/mjpeg_encoder.c
+++ b/server/mjpeg_encoder.c
@@ -105,7 +105,6 @@ typedef struct {
unsigned char ** outbuffer; /* target buffer */
size_t * outsize;
- unsigned char * newbuffer; /* newly allocated buffer */
uint8_t * buffer; /* start of buffer */
size_t bufsize;
} mem_destination_mgr;
@@ -129,9 +128,7 @@ static boolean empty_mem_output_buffer(j_compress_ptr cinfo)
memcpy(nextbuffer, dest->buffer, dest->bufsize);
- free(dest->newbuffer);
-
- dest->newbuffer = nextbuffer;
+ free(dest->buffer);
dest->pub.next_output_byte = nextbuffer + dest->bufsize;
dest->pub.free_in_buffer = dest->bufsize;
@@ -184,12 +181,10 @@ spice_jpeg_mem_dest(j_compress_ptr cinfo,
dest->pub.term_destination = term_mem_destination;
dest->outbuffer = outbuffer;
dest->outsize = outsize;
- dest->newbuffer = NULL;
-
if (*outbuffer == NULL || *outsize == 0) {
/* Allocate initial buffer */
- dest->newbuffer = *outbuffer = malloc(OUTPUT_BUF_SIZE);
- if (dest->newbuffer == NULL)
+ *outbuffer = malloc(OUTPUT_BUF_SIZE);
+ if (*outbuffer == NULL)
ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 10);
*outsize = OUTPUT_BUF_SIZE;
}
diff --git a/server/mjpeg_encoder.h b/server/mjpeg_encoder.h
index c43827f..62ef207 100644
--- a/server/mjpeg_encoder.h
+++ b/server/mjpeg_encoder.h
@@ -27,6 +27,11 @@ MJpegEncoder *mjpeg_encoder_new(int width, int height);
void mjpeg_encoder_destroy(MJpegEncoder *encoder);
uint8_t mjpeg_encoder_get_bytes_per_pixel(MJpegEncoder *encoder);
+
+/*
+ * *dest must be either NULL or allocated by malloc, since it might be freed
+ * during the encoding, if its size is too small.
+ */
int mjpeg_encoder_start_frame(MJpegEncoder *encoder, SpiceBitmapFmt format,
uint8_t **dest, size_t *dest_len);
int mjpeg_encoder_encode_scanline(MJpegEncoder *encoder, uint8_t *src_pixels,
--
1.7.10.1