Skip to content
Permalink
Browse files Browse the repository at this point in the history
gzip: fix compression size calculation (oss-fuzz 27261)
Signed-off-by: davkor <david@adalogics.com>
  • Loading branch information
DavidKorczynski authored and edsiper committed Nov 7, 2020
1 parent d75062d commit cadff53
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/flb_gzip.c
Expand Up @@ -77,8 +77,25 @@ int flb_gzip_compress(void *in_data, size_t in_len,
z_stream strm;
mz_ulong crc;

out_size = in_len + 32;

/*
* GZIP relies on an algorithm with worst-case expansion
* of 5 bytes per 32KB data. This means we need to create a variable
* length output, that depends on the input length.
* See RFC 1951 for details.
*/
int max_input_expansion = ((int)(in_len / 32000) + 1) * 5;

/*
* Max compressed size is equal to sum of:
* 10 byte header
* 8 byte foot
* max input expansion
* size of input
*/
out_size = 10 + 8 + max_input_expansion + in_len;
out_buf = flb_malloc(out_size);

if (!out_buf) {
flb_errno();
flb_error("[gzip] could not allocate outgoing buffer");
Expand Down

0 comments on commit cadff53

Please sign in to comment.