# [PATCH 1/6] Better error handling in compress_all()

1 messages from 2008-01-11 to 2008-01-11. Participants: Marco Costalba.
Thread: https://gitlist.dev/t/11586

## Marco Costalba, 2008-01-11 18:53

Subject: [PATCH 1/6] Better error handling in compress_all()
Message-ID: <e5bfff550801111053v55fd2fc3ice97e6ae10c8f942@mail.gmail.com>
URL: https://gitlist.dev/e/e5bfff550801111053v55fd2fc3ice97e6ae10c8f942%40mail.gmail.com

```
Also let the caller to xmalloc() the buffer
int compress_start()

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
---

This patch belong to the previous compression series,
not the decompression one that I'm publishing right now.

Anyhow next patches depends on this, so that's the reason
why is the first.


 compress.c |   19 +++++++++----------
 1 files changed, 9 insertions(+), 10 deletions(-)

diff --git a/compress.c b/compress.c
index be771a9..a8f46d5 100644
--- a/compress.c
+++ b/compress.c
@@ -12,7 +12,7 @@ int compress_start(z_stream *stream,
                    unsigned char *in, unsigned long in_size,
                    unsigned char *out, unsigned long out_size)
 {
-	stream->next_out = (out ? out : xmalloc(out_size));
+	stream->next_out = out;
 	stream->avail_out = out_size;
 	stream->next_in = in;
 	stream->avail_in = in_size;
@@ -36,19 +36,18 @@ unsigned long compress_free(z_stream
 	return stream->total_out;
 }

-unsigned long compress_all(int level, unsigned char *data,
-                           unsigned long size, unsigned char **out)
+unsigned long compress_all(int level, unsigned char *in,
+                           unsigned long in_size, unsigned char **out)
 {
-	int bound, result;
+	unsigned long out_size;
 	z_stream stream;

-	bound = compress_alloc(&stream, level, size);
-	compress_start(&stream, data, size, NULL, bound);
+	out_size = compress_alloc(&stream, level, in_size);
+	*out = xmalloc(out_size);

-	*out = stream.next_out;
-	result = compress_next(&stream, Z_FINISH);
-
-	if (result != Z_STREAM_END) {
+	if (   compress_start(&stream, in, in_size, *out, out_size) != Z_OK
+	    || compress_next(&stream, Z_FINISH) != Z_STREAM_END)
+	{
 		compress_free(&stream);
 		free(*out);
 		*out = NULL;
-- 
1.5.4.rc2.90.gf158-dirty

```
