changeset 17131:e30597858a66

lib-compression: Add assert for LZ4_compress return value. Make certain we detect if compressed data overflows the allocated space.
author Teemu Huovila <teemu.huovila@dovecot.fi>
date Tue, 04 Feb 2014 18:17:35 -0500
parents add8c00fb3cc
children cce087bde475
files src/lib-compression/ostream-lz4.c
diffstat 1 files changed, 9 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/src/lib-compression/ostream-lz4.c	Tue Feb 04 16:23:22 2014 -0500
+++ b/src/lib-compression/ostream-lz4.c	Tue Feb 04 18:17:35 2014 -0500
@@ -18,7 +18,8 @@
 	unsigned int compressbuf_offset;
 
 	/* chunk size, followed by compressed data */
-	unsigned char outbuf[IOSTREAM_LZ4_CHUNK_PREFIX_LEN + LZ4_COMPRESSBOUND(CHUNK_SIZE)];
+	unsigned char outbuf[IOSTREAM_LZ4_CHUNK_PREFIX_LEN +
+	                     LZ4_COMPRESSBOUND(CHUNK_SIZE)];
 	unsigned int outbuf_offset, outbuf_used;
 };
 
@@ -70,11 +71,13 @@
 	i_assert(zstream->outbuf_offset == 0);
 	i_assert(zstream->outbuf_used == 0);
 
-	zstream->outbuf_used = IOSTREAM_LZ4_CHUNK_PREFIX_LEN +
-		LZ4_compress((void *)zstream->compressbuf,
-			     (void *)(zstream->outbuf + IOSTREAM_LZ4_CHUNK_PREFIX_LEN),
-			     zstream->compressbuf_offset);
-	i_assert(zstream->outbuf_used > IOSTREAM_LZ4_CHUNK_PREFIX_LEN);
+	ret = LZ4_compress((void *)zstream->compressbuf,
+			   (void *)(zstream->outbuf +
+			            IOSTREAM_LZ4_CHUNK_PREFIX_LEN),
+			   zstream->compressbuf_offset);
+	i_assert(ret > 0 && (unsigned int)ret <= sizeof(zstream->outbuf) -
+	         IOSTREAM_LZ4_CHUNK_PREFIX_LEN);
+	zstream->outbuf_used = IOSTREAM_LZ4_CHUNK_PREFIX_LEN + ret;
 	chunk_size = zstream->outbuf_used - IOSTREAM_LZ4_CHUNK_PREFIX_LEN;
 	zstream->outbuf[0] = (chunk_size & 0xff000000) >> 24;
 	zstream->outbuf[1] = (chunk_size & 0x00ff0000) >> 16;