From 474f1f4268d24ba7782ab57ae47c87113ec2a8f5 Mon Sep 17 00:00:00 2001 From: Kalle Olavi Niemitalo Date: Sun, 17 Feb 2008 18:55:41 +0200 Subject: [PATCH] encoding: Set *new_len even if data was truncated. Previously, bzip2_decode_buffer and deflate_decode_buffer left *new_len unchanged if the compressed input data ended unexpectedly. This behaviour was also inherited by decode_encoded_buffer, whose only caller render_encoded_document preinitializes the variable and so did not crash. With this change, the functions now store in *new_len the number of bytes that were successfully decoded, even if more bytes were expected. An error should perhaps be reported to the user, but I don't think the previous version did that either, as it returned a non-NULL pointer. --- src/encoding/bzip2.c | 11 ++++++----- src/encoding/deflate.c | 11 ++++++----- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/encoding/bzip2.c b/src/encoding/bzip2.c index 87434b15..8e7139a6 100644 --- a/src/encoding/bzip2.c +++ b/src/encoding/bzip2.c @@ -138,6 +138,8 @@ bzip2_decode_buffer(unsigned char *data, int len, int *new_len) unsigned char *buffer = NULL; int error; + *new_len = 0; /* default, left there if an error occurs */ + memset(&stream, 0, sizeof(bz_stream)); stream.next_in = data; stream.avail_in = len; @@ -168,7 +170,6 @@ bzip2_decode_buffer(unsigned char *data, int len, int *new_len) error = BZ2_bzDecompress(&stream); if (error == BZ_STREAM_END) { - *new_len = stream.total_out_lo32; error = BZ_OK; break; } @@ -181,13 +182,13 @@ bzip2_decode_buffer(unsigned char *data, int len, int *new_len) BZ2_bzDecompressEnd(&stream); - if (error != BZ_OK) { + if (error == BZ_OK) { + *new_len = stream.total_out_lo32; + return buffer; + } else { if (buffer) mem_free(buffer); - *new_len = 0; return NULL; } - - return buffer; } static void diff --git a/src/encoding/deflate.c b/src/encoding/deflate.c index c211942f..95b33c23 100644 --- a/src/encoding/deflate.c +++ b/src/encoding/deflate.c @@ -123,6 +123,8 @@ deflate_decode_buffer(unsigned char *data, int len, int *new_len) unsigned char *buffer = NULL; int error; + *new_len = 0; /* default, left there if an error occurs */ + if (!len) return NULL; memset(&stream, 0, sizeof(z_stream)); stream.next_in = data; @@ -147,7 +149,6 @@ deflate_decode_buffer(unsigned char *data, int len, int *new_len) error = inflate(&stream, Z_SYNC_FLUSH); if (error == Z_STREAM_END) { - *new_len = stream.total_out; error = Z_OK; break; } @@ -155,13 +156,13 @@ deflate_decode_buffer(unsigned char *data, int len, int *new_len) inflateEnd(&stream); - if (error != Z_OK) { + if (error == Z_OK) { + *new_len = stream.total_out; + return buffer; + } else { if (buffer) mem_free(buffer); - *new_len = 0; return NULL; } - - return buffer; } static void