Bug Summary

File:builds/wireshark/wireshark/epan/tvbuff_zstd.c
Warning:line 95, column 21
Potential leak of memory pointed to by 'uncompr'

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name tvbuff_zstd.c -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 2 -fhalf-no-semantic-interposition -fno-delete-null-pointer-checks -mframe-pointer=all -relaxed-aliasing -fmath-errno -ffp-contract=on -fno-rounding-math -ffloat16-excess-precision=fast -fbfloat16-excess-precision=fast -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fdebug-compilation-dir=/builds/wireshark/wireshark/build -fcoverage-compilation-dir=/builds/wireshark/wireshark/build -resource-dir /usr/lib/llvm-22/lib/clang/22 -isystem /usr/include/glib-2.0 -isystem /usr/lib/x86_64-linux-gnu/glib-2.0/include -isystem /builds/wireshark/wireshark/epan -isystem /builds/wireshark/wireshark/build/epan -isystem /usr/include/mit-krb5 -isystem /usr/include/lua5.5 -isystem /usr/include/libxml2 -D CARES_NO_DEPRECATED -D G_DISABLE_DEPRECATED -D G_DISABLE_SINGLE_INCLUDES -D REX_API=extern -D WS_BUILD_DLL -D WS_DEBUG -D WS_DEBUG_UTF_8 -D epan_EXPORTS -I /builds/wireshark/wireshark/build -I /builds/wireshark/wireshark -I /builds/wireshark/wireshark/include -I /builds/wireshark/wireshark/wiretap -D _GLIBCXX_ASSERTIONS -internal-isystem /usr/lib/llvm-22/lib/clang/22/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/16/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -fmacro-prefix-map=/builds/wireshark/wireshark/= -fmacro-prefix-map=/builds/wireshark/wireshark/build/= -fmacro-prefix-map=../= -Wno-format-nonliteral -std=gnu17 -ferror-limit 19 -fvisibility=hidden -fwrapv -fwrapv-pointer -fstrict-flex-arrays=3 -stack-protector 2 -fstack-clash-protection -fcf-protection=full -fgnuc-version=4.2.1 -fskip-odr-check-in-gmf -fexceptions -fcolor-diagnostics -analyzer-output=html -faddrsig -fdwarf2-cfi-asm -o /builds/wireshark/wireshark/sbout/2026-12-04-100315-3660-1 -x c /builds/wireshark/wireshark/epan/tvbuff_zstd.c
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 <[email protected]>
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
30tvbuff_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)
3
Assuming field 'pos' is < field 'size'
4
Loop condition is true. Entering loop body
13
Assuming field 'pos' is < field 'size'
14
Loop condition is true. Entering loop body
23
Assuming field 'pos' is >= field 'size'
52 {
53 rc = ZSTD_decompressStream(zds, &output, &input);
54 if (ZSTD_isError(rc))
5
Assuming the condition is false
6
Taking false branch
15
Assuming the condition is false
16
Taking false branch
55 {
56 goto end;
57 }
58
59 if (output.pos > 0)
7
Assuming field 'pos' is > 0
8
Taking true branch
17
Assuming field 'pos' is > 0
18
Taking true branch
60 {
61 if (output.pos > max_out - uncompr_len)
9
Assuming the condition is false
10
Taking false branch
19
Assuming the condition is false
20
Taking false branch
62 {
63 // More output than the caller allows.
64 goto end;
65 }
66 if (!uncompr
10.1
'uncompr' is null
20.1
'uncompr' is non-null
)
21
Taking false branch
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"
))))
;
11
Taking true branch
12
'?' condition is true
69 uncompr = g_malloc(output.pos);
70 } else {
71 uncompr = g_realloc(uncompr, uncompr_len + output.pos);
22
Memory is allocated
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
24.1
Field 'pos' is >= field 'size'
< input.size)
24
Assuming 'rc' is <= 0
25
Taking false branch
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;
88end:
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)
;
26
Assuming the condition is false
27
'?' condition is false
90 wmem_free(NULL((void*)0), (void *)input.src);
91 ZSTD_freeDStream(zds);
92 if (ok
27.1
'ok' is true
)
28
Taking true branch
93 {
94 tvbuff_t *uncompr_tvb;
95 uncompr_tvb = tvb_new_real_data (uncompr, (unsigned)uncompr_len, (unsigned)uncompr_len);
29
Potential leak of memory pointed to by 'uncompr'
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
109tvbuff_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));
2
Calling 'tvb_uncompress_zstd_max'
112}
113
114tvbuff_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
125tvbuff_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);
1
Calling 'tvb_uncompress_zstd'
128 if (!uncompressed)
129 {
130 return uncompressed;
131 }
132 tvb_add_to_chain(parent, uncompressed);
133 return uncompressed;
134}