From bd1f2cc996531d84b3c1179092f9dc95ff11d91c Mon Sep 17 00:00:00 2001 From: John Thacker Date: Fri, 2 Dec 2022 23:28:42 -0500 Subject: [PATCH] epan: Use realloc when extending the uncompressed buffer tvb_uncompress initially allocates an output buffer of twice the input size. It is typical to have a compression ratio of 2:1 or 5:1, but in the extreme case (lots of all identical bytes), 1030:1 is possible. When extending the output buffer, instead of always malloc'ing a new buffer and memcpy'ing the old buffer into it, call realloc, which at least some (most?) of the time will extend the current buffer in place instead. This should reduce the time to unzip from always O(N^2) (where N is the compression ratio) to something average case more like O(N) or O(N log N), depending on how often it actually copies the data. It only really affects pathological cases. Related to #13779. --- epan/tvbuff_zlib.c | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/epan/tvbuff_zlib.c b/epan/tvbuff_zlib.c index 2264d5dc1d..5a48ab72d9 100644 --- a/epan/tvbuff_zlib.c +++ b/epan/tvbuff_zlib.c @@ -114,13 +114,8 @@ tvb_uncompress(tvbuff_t *tvb, const int offset, int comprlen) g_memdup2(strmbuf, bytes_pass) : g_strdup("")); } else { - guint8 *new_data = (guint8 *)g_malloc0(bytes_out + bytes_pass); - - memcpy(new_data, uncompr, bytes_out); - memcpy(new_data + bytes_out, strmbuf, bytes_pass); - - g_free(uncompr); - uncompr = new_data; + uncompr = (guint8 *)g_realloc(uncompr, bytes_out + bytes_pass); + memcpy(uncompr + bytes_out, strmbuf, bytes_pass); } bytes_out += bytes_pass;