| File: | builds/wireshark/wireshark/epan/tvbuff_zstd.c |
| Warning: | line 95, column 21 Potential leak of memory pointed to by 'uncompr' |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
| 1 | /* tvbuff_zstd.c | |||
| 2 | * Copyright 2022, Kevin Albertson <kevin.eric.albertson [AT] gmail.com> | |||
| 3 | * | |||
| 4 | * Wireshark - Network traffic analyzer | |||
| 5 | * By Gerald Combs <gerald@wireshark.org> | |||
| 6 | * Copyright 1998 Gerald Combs | |||
| 7 | * | |||
| 8 | * SPDX-License-Identifier: GPL-2.0-or-later | |||
| 9 | */ | |||
| 10 | ||||
| 11 | /* | |||
| 12 | * Decompress ZSTD: http://facebook.github.io/zstd/ | |||
| 13 | */ | |||
| 14 | ||||
| 15 | #include "config.h" | |||
| 16 | ||||
| 17 | #include <limits.h> | |||
| 18 | ||||
| 19 | #ifdef HAVE_ZSTD1 | |||
| 20 | #include <zstd.h> | |||
| 21 | #endif | |||
| 22 | ||||
| 23 | #include "proto.h" // DISSECTOR_ASSERT_HINT | |||
| 24 | #include "tvbuff.h" | |||
| 25 | ||||
| 26 | #include "tvbuff-int.h" // tvb_add_to_chain | |||
| 27 | ||||
| 28 | #define MAX_LOOP_ITERATIONS100 100 | |||
| 29 | ||||
| 30 | tvbuff_t *tvb_uncompress_zstd_max(tvbuff_t *tvb, const unsigned offset, unsigned comprlen, unsigned max_out) | |||
| 31 | { | |||
| 32 | #ifndef HAVE_ZSTD1 | |||
| 33 | // Cast to void to silence unused warnings. | |||
| 34 | (void)tvb; | |||
| 35 | (void)offset; | |||
| 36 | (void)comprlen; | |||
| 37 | (void)max_out; | |||
| 38 | return NULL((void*)0); | |||
| 39 | #else | |||
| 40 | ZSTD_inBuffer input = {tvb_memdup(NULL((void*)0), tvb, offset, comprlen), comprlen, 0}; | |||
| 41 | ZSTD_DStream *zds = ZSTD_createDStream(); | |||
| 42 | size_t rc = 0; | |||
| 43 | uint8_t *uncompr = NULL((void*)0); | |||
| 44 | size_t uncompr_len = 0; | |||
| 45 | bool_Bool ok = false0; | |||
| 46 | int count = 0; | |||
| 47 | ||||
| 48 | // ZSTD does not consume the last byte of the frame until it has flushed all of the decompressed data of the frame. | |||
| 49 | // Therefore, loop while there is more input. | |||
| 50 | ZSTD_outBuffer output = {g_malloc(ZSTD_DStreamOutSize()), ZSTD_DStreamOutSize(), 0}; | |||
| 51 | while (input.pos < input.size && count < MAX_LOOP_ITERATIONS100) | |||
| 52 | { | |||
| 53 | rc = ZSTD_decompressStream(zds, &output, &input); | |||
| 54 | if (ZSTD_isError(rc)) | |||
| 55 | { | |||
| 56 | goto end; | |||
| 57 | } | |||
| 58 | ||||
| 59 | if (output.pos > 0) | |||
| 60 | { | |||
| 61 | if (output.pos > max_out - uncompr_len) | |||
| 62 | { | |||
| 63 | // More output than the caller allows. | |||
| 64 | goto end; | |||
| 65 | } | |||
| 66 | if (!uncompr
| |||
| 67 | { | |||
| 68 | DISSECTOR_ASSERT (uncompr_len == 0)((void) ((uncompr_len == 0) ? (void)0 : (proto_report_dissector_bug ("%s:%u: failed assertion \"%s\"", "epan/tvbuff_zstd.c", 68, "uncompr_len == 0" )))); | |||
| 69 | uncompr = g_malloc(output.pos); | |||
| 70 | } else { | |||
| 71 | uncompr = g_realloc(uncompr, uncompr_len + output.pos); | |||
| 72 | } | |||
| 73 | memcpy (uncompr + uncompr_len, output.dst, output.pos); | |||
| 74 | uncompr_len += output.pos; | |||
| 75 | // Reset the output buffer. | |||
| 76 | output.pos = 0; | |||
| 77 | } | |||
| 78 | count++; | |||
| 79 | } | |||
| 80 | if (rc > 0 || input.pos
| |||
| 81 | { | |||
| 82 | // There is extra data that was not decompressed, or the loop | |||
| 83 | // limit was hit. | |||
| 84 | goto end; | |||
| 85 | } | |||
| 86 | ||||
| 87 | ok = true1; | |||
| 88 | end: | |||
| 89 | g_free((void *)output.dst)(__builtin_object_size (((void *)output.dst), 0) != ((size_t) - 1)) ? g_free_sized ((void *)output.dst, __builtin_object_size (((void *)output.dst), 0)) : (g_free) ((void *)output.dst); | |||
| 90 | wmem_free(NULL((void*)0), (void *)input.src); | |||
| 91 | ZSTD_freeDStream(zds); | |||
| 92 | if (ok
| |||
| 93 | { | |||
| 94 | tvbuff_t *uncompr_tvb; | |||
| 95 | uncompr_tvb = tvb_new_real_data (uncompr, (unsigned)uncompr_len, (unsigned)uncompr_len); | |||
| ||||
| 96 | tvb_set_free_cb (uncompr_tvb, g_free); | |||
| 97 | return uncompr_tvb; | |||
| 98 | } | |||
| 99 | ||||
| 100 | if (uncompr) | |||
| 101 | { | |||
| 102 | g_free (uncompr)(__builtin_object_size ((uncompr), 0) != ((size_t) - 1)) ? g_free_sized (uncompr, __builtin_object_size ((uncompr), 0)) : (g_free) ( uncompr); | |||
| 103 | } | |||
| 104 | ||||
| 105 | return NULL((void*)0); | |||
| 106 | #endif /* HAVE_ZSTD */ | |||
| 107 | } | |||
| 108 | ||||
| 109 | tvbuff_t *tvb_uncompress_zstd(tvbuff_t *tvb, const unsigned offset, unsigned comprlen) | |||
| 110 | { | |||
| 111 | return tvb_uncompress_zstd_max(tvb, offset, comprlen, UINT_MAX(2147483647 *2U +1U)); | |||
| 112 | } | |||
| 113 | ||||
| 114 | tvbuff_t *tvb_child_uncompress_zstd_max(tvbuff_t *parent, tvbuff_t *tvb, const unsigned offset, unsigned comprlen, unsigned max_out) | |||
| 115 | { | |||
| 116 | tvbuff_t *uncompressed = tvb_uncompress_zstd_max(tvb, offset, comprlen, max_out); | |||
| 117 | if (!uncompressed) | |||
| 118 | { | |||
| 119 | return uncompressed; | |||
| 120 | } | |||
| 121 | tvb_add_to_chain(parent, uncompressed); | |||
| 122 | return uncompressed; | |||
| 123 | } | |||
| 124 | ||||
| 125 | tvbuff_t *tvb_child_uncompress_zstd(tvbuff_t *parent, tvbuff_t *tvb, const unsigned offset, unsigned comprlen) | |||
| 126 | { | |||
| 127 | tvbuff_t *uncompressed = tvb_uncompress_zstd(tvb, offset, comprlen); | |||
| ||||
| 128 | if (!uncompressed) | |||
| 129 | { | |||
| 130 | return uncompressed; | |||
| 131 | } | |||
| 132 | tvb_add_to_chain(parent, uncompressed); | |||
| 133 | return uncompressed; | |||
| 134 | } |