Bug Summary

File:builds/wireshark/wireshark/epan/tvbuff_lz77huff.c
Warning:line 412, column 7
Potential leak of memory pointed to by 'p'

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_lz77huff.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_lz77huff.c
1/*
2 * Decompression code for LZ77+Huffman. This encoding is used by
3 * Microsoft in various file formats and protocols including SMB3.
4 *
5 * See MS-XCA.
6 *
7 * Initial code from Samba re-licensed with Samuel's permission.
8 * Copyright (C) Samuel Cabrero 2017
9 *
10 * Glib-ification, extra error-checking and WS integration
11 * Copyright (C) Aurélien Aptel 2019
12 *
13 * Wireshark - Network traffic analyzer
14 * By Gerald Combs <[email protected]>
15 * Copyright 1998 Gerald Combs
16 *
17 * SPDX-License-Identifier: GPL-2.0-or-later
18 */
19
20#include <glib.h>
21#include <stdlib.h> /* qsort */
22#include <epan/exceptions.h>
23#include <epan/tvbuff.h>
24#include <epan/wmem_scopes.h>
25
26#define MAX_INPUT_SIZE(16*1024*1024) (16*1024*1024) /* 16MB */
27#define MAX_OUTPUT_SIZE(16*1024*1024) (16*1024*1024) /* 16MB, as for LZ77 */
28
29#define TREE_SIZE1024 1024
30#define ENCODED_TREE_SIZE256 256
31#define SYMBOL_INFO_SIZE(2*256) (2*ENCODED_TREE_SIZE256)
32
33struct input {
34 tvbuff_t *tvb;
35 int offset;
36 size_t size;
37};
38
39/**
40 * Represents a node in a Huffman prefix code tree
41 */
42struct prefix_code_node {
43 /* Stores the symbol encoded by this node in the prefix code tree */
44 uint16_t symbol;
45
46 /* Indicates whether this node is a leaf in the tree */
47 uint8_t leaf;
48
49 /*
50 * Points to the node's two children. Values are indexes in
51 * the tree node array. The value -1 is used to indicate that
52 * a particular child does not exist
53 */
54 int16_t child[2];
55};
56
57/**
58 * Represent information about a Huffman-encoded symbol
59 */
60struct prefix_code_symbol {
61 /* Stores the symbol */
62 uint16_t symbol;
63
64 /* Stores the symbol’s Huffman prefix code length */
65 uint16_t length;
66};
67
68/**
69 * Represent a byte array as a bit string from which individual bits can
70 * be read
71 */
72struct bitstring {
73 /* The byte array */
74 const struct input *input;
75
76 /* The index in source from which the next set of bits will be pulled
77 * when the bits in mask have been consumed */
78 uint32_t bitstring_index;
79
80 /* Stores the next bits to be consumed in the bit string */
81 uint32_t mask;
82
83 /* Stores the number of bits in mask that remain to be consumed */
84 int32_t bits;
85};
86
87struct hf_tree {
88 struct prefix_code_node *root;
89 struct prefix_code_node nodes[TREE_SIZE1024];
90};
91
92static bool_Bool is_node_valid(struct hf_tree *tree, struct prefix_code_node *node)
93{
94 return (node && node >= tree->nodes && node < tree->nodes + TREE_SIZE1024);
95}
96
97/**
98 * Links a symbol's prefix_code_node into its correct position in a Huffman
99 * prefix code tree
100 */
101static int prefix_code_tree_add_leaf(struct hf_tree *tree,
102 uint32_t leaf_index,
103 uint32_t mask,
104 uint32_t bits,
105 uint32_t *out_index)
106{
107 struct prefix_code_node *node = &tree->nodes[0];
108 uint32_t i = leaf_index + 1;
109 uint32_t child_index;
110
111 if (leaf_index >= TREE_SIZE1024)
112 return -1;
113
114 while (bits > 1) {
115 bits = bits - 1;
116 child_index = (mask >> bits) & 1;
117 if (node->child[child_index] < 0) {
118 if (i >= TREE_SIZE1024)
119 return -1;
120 node->child[child_index] = i;
121 tree->nodes[i].leaf = false0;
122 i = i + 1;
123 }
124 node = tree->nodes + node->child[child_index];
125 if (!is_node_valid(tree, node))
126 return -1;
127 }
128
129 node->child[mask & 1] = leaf_index;
130
131 *out_index = i;
132 return 0;
133}
134
135/**
136 * Determines the sort order of one prefix_code_symbol relative to another
137 */
138static int compare_symbols(const void *ve1, const void *ve2)
139{
140 const struct prefix_code_symbol *e1 = (const struct prefix_code_symbol *)ve1;
141 const struct prefix_code_symbol *e2 = (const struct prefix_code_symbol *)ve2;
142
143 if (e1->length < e2->length)
144 return -1;
145 else if (e1->length > e2->length)
146 return 1;
147 else if (e1->symbol < e2->symbol)
148 return -1;
149 else if (e1->symbol > e2->symbol)
150 return 1;
151 else
152 return 0;
153}
154
155/**
156 * Rebuilds the Huffman prefix code tree that will be used to decode symbols
157 * during decompression
158 */
159static int PrefixCodeTreeRebuild( struct hf_tree *tree,
160 const struct input *input)
161{
162 struct prefix_code_symbol symbolInfo[SYMBOL_INFO_SIZE(2*256)];
163 uint32_t i, j, mask, bits;
164 int rc;
165
166 for (i = 0; i < TREE_SIZE1024; i++) {
167 tree->nodes[i].symbol = 0;
168 tree->nodes[i].leaf = false0;
169 tree->nodes[i].child[0] = -1;
170 tree->nodes[i].child[1] = -1;
171 }
172
173 if (input->size < ENCODED_TREE_SIZE256)
174 return -1;
175
176 for (i = 0; i < ENCODED_TREE_SIZE256; i++) {
177 symbolInfo[2*i].symbol = 2*i;
178 symbolInfo[2*i].length = tvb_get_uint8(input->tvb, input->offset+i) & 15;
179 symbolInfo[2*i+1].symbol = 2*i+1;
180 symbolInfo[2*i+1].length = tvb_get_uint8(input->tvb, input->offset+i) >> 4;
181 }
182
183 qsort(symbolInfo, SYMBOL_INFO_SIZE(2*256), sizeof(symbolInfo[0]), compare_symbols);
184
185 i = 0;
186 while (i < SYMBOL_INFO_SIZE(2*256) && symbolInfo[i].length == 0) {
187 i = i + 1;
188 }
189
190 mask = 0;
191 bits = 1;
192
193 tree->root = &tree->nodes[0];
194 tree->root->leaf = false0;
195
196 j = 1;
197 for (; i < 512; i++) {
198 //ws_assert(j < TREE_SIZE);
199 if (j >= TREE_SIZE1024) {
200 return -1;
201 }
202 tree->nodes[j].symbol = symbolInfo[i].symbol;
203 tree->nodes[j].leaf = true1;
204 mask <<= symbolInfo[i].length - bits;
205 bits = symbolInfo[i].length;
206 rc = prefix_code_tree_add_leaf(tree, j, mask, bits, &j);
207 if (rc)
208 return rc;
209 mask += 1;
210 }
211
212 return 0;
213}
214
215/**
216 * Initializes a bitstream data structure
217 */
218static void bitstring_init(struct bitstring *bstr,
219 const struct input *input,
220 uint32_t bitstring_index)
221{
222 bstr->mask = tvb_get_letohs(input->tvb, input->offset+bitstring_index);
223 bstr->mask <<= sizeof(bstr->mask) * 8 - 16;
224 bitstring_index += 2;
225
226 bstr->mask += tvb_get_letohs(input->tvb, input->offset+bitstring_index);
227 bitstring_index += 2;
228
229 bstr->bits = 32;
230 bstr->input = input;
231 bstr->bitstring_index = bitstring_index;
232}
233
234/**
235 * Returns the next n bits from the front of a bit string.
236 */
237static uint32_t bitstring_lookup(struct bitstring *bstr, uint32_t n)
238{
239 if (n == 0 || bstr->bits < 0 || n > (uint32_t)bstr->bits) {
240 return 0;
241 }
242 return bstr->mask >> (sizeof(bstr->mask) * 8 - n);
243}
244
245/**
246 * Advances the bit string's cursor by n bits.
247 */
248static void bitstring_skip(struct bitstring *bstr, uint32_t n)
249{
250 bstr->mask = bstr->mask << n;
251 bstr->bits = bstr->bits - n;
252
253 if (bstr->bits < 16) {
254 bstr->mask += tvb_get_letohs(bstr->input->tvb,
255 bstr->input->offset + bstr->bitstring_index)
256 << (16 - bstr->bits);
257 bstr->bitstring_index = bstr->bitstring_index + 2;
258 bstr->bits = bstr->bits + 16;
259 }
260}
261
262/**
263 * Returns the symbol encoded by the next prefix code in a bit string.
264 */
265static int prefix_code_tree_decode_symbol(struct hf_tree *tree,
266 struct bitstring *bstr,
267 uint32_t *out_symbol)
268{
269 uint32_t bit;
270 struct prefix_code_node *node = tree->root;
271
272 do {
273 bit = bitstring_lookup(bstr, 1);
274 bitstring_skip(bstr, 1);
275 node = tree->nodes + node->child[bit];
276 if (!is_node_valid(tree, node))
277 return -1;
278 } while (node->leaf == false0);
279
280 *out_symbol = node->symbol;
281 return 0;
282}
283
284static bool_Bool do_uncompress(struct input *input,
285 wmem_array_t *obuf, unsigned max_out)
286{
287 uint32_t symbol;
288 uint32_t length;
289 unsigned match_offset;
290 int rc;
291 struct hf_tree tree = {0};
292 struct bitstring bstr = {0};
293
294 if (!input->tvb)
295 return false0;
296
297 if (!input->size || input->size > MAX_INPUT_SIZE(16*1024*1024))
298 return false0;
299
300 rc = PrefixCodeTreeRebuild(&tree, input);
301 if (rc)
302 return false0;
303
304 bitstring_init(&bstr, input, ENCODED_TREE_SIZE256);
305
306 while (1) {
307 rc = prefix_code_tree_decode_symbol(&tree, &bstr, &symbol);
308 if (rc < 0)
309 return false0;
310
311 if (symbol < 256) {
312 uint8_t v = symbol & 0xFF;
313 if (wmem_array_get_count(obuf) >= max_out)
314 return false0;
315 wmem_array_append_one(obuf, v)wmem_array_append((obuf), &(v), 1);
316 } else {
317 if (symbol == 256) {
318 /* EOF symbol and everything consumed */
319 if (bstr.bitstring_index == bstr.input->size) {
320 return true1;
321 }
322 }
323 symbol = symbol - 256;
324 length = symbol & 0xF;
325 symbol = symbol >> 4;
326
327 match_offset = (1U << symbol) + bitstring_lookup(&bstr, symbol);
328
329 if (length == 15) {
330 if (bstr.bitstring_index >= bstr.input->size)
331 return false0;
332 length = tvb_get_uint8(bstr.input->tvb,
333 bstr.input->offset+bstr.bitstring_index) + 15;
334 bstr.bitstring_index += 1;
335
336 if (length == 270) {
337 if (bstr.bitstring_index+1 >= bstr.input->size)
338 return false0;
339 length = tvb_get_letohs(bstr.input->tvb, bstr.input->offset+bstr.bitstring_index);
340 bstr.bitstring_index += 2;
341 }
342 }
343
344 bitstring_skip(&bstr, symbol);
345
346 length += 3;
347 /* Limit the whole output, not just one match. */
348 if (length > max_out - wmem_array_get_count(obuf))
349 return false0;
350 if (match_offset > wmem_array_get_count(obuf))
351 return false0;
352 ws_assert(match_offset != 0)do { if ((1) && !(match_offset != 0)) ws_log_fatal_full
("", LOG_LEVEL_ERROR, "epan/tvbuff_lz77huff.c", 352, __func__
, "assertion failed: %s", "match_offset != 0"); } while (0)
;
353 /* Must call grow first, to realloc the array if needed
354 * and avoid invalidating the pointers returned from
355 * wmem_array_index. */
356 if (!wmem_array_grow(obuf, length))
357 return false0;
358 uint8_t *src;
359 unsigned i;
360 for (i = 0; i < length / match_offset; i++) {
361 src = wmem_array_index(obuf, wmem_array_get_count(obuf) - match_offset);
362 wmem_array_append(obuf, src, match_offset);
363 }
364 for (i *= match_offset; i < length; i++) {
365 src = wmem_array_index(obuf, wmem_array_get_count(obuf) - match_offset);
366 wmem_array_append(obuf, src, 1);
367 }
368 }
369 }
370 return true1;
371}
372
373tvbuff_t *
374tvb_uncompress_lz77huff_max(tvbuff_t *tvb,
375 const unsigned offset,
376 unsigned input_size,
377 unsigned max_out)
378{
379 volatile bool_Bool ok;
380 wmem_allocator_t *pool;
381 wmem_array_t *obuf;
382 tvbuff_t *out;
383 struct input input = {
384 .tvb = tvb,
385 .offset = offset,
386 .size = input_size
387 };
388
389 if (max_out > MAX_OUTPUT_SIZE(16*1024*1024))
3
Taking false branch
390 max_out = MAX_OUTPUT_SIZE(16*1024*1024);
391
392 pool = wmem_allocator_new(WMEM_ALLOCATOR_SIMPLE);
393 obuf = wmem_array_sized_new(pool, 1, MIN(input_size*2, max_out + 1)(((input_size*2) < (max_out + 1)) ? (input_size*2) : (max_out
+ 1))
);
4
Assuming the condition is true
5
'?' condition is true
394
395 TRY{ except_t *volatile exc; volatile int except_state = 0; static
const except_id_t catch_spec[] = { { 1, 0 } }; { struct except_stacknode
except_sn; struct except_catch except_ch; except_setup_try(&
except_sn, &except_ch, catch_spec, 1); if (_setjmp (except_ch
.except_jmp)) *(&exc) = &except_ch.except_obj; else *
(&exc) = 0; if(except_state & 1) except_state |= 2; except_state
&= ~1; if (except_state == 0 && exc == 0)
{
6
Assuming the condition is false
7
Taking false branch
8
Taking false branch
9
Taking true branch
396 ok = do_uncompress(&input, obuf, max_out);
397 } CATCH_ALLif (except_state == 0 && exc != 0 && (except_state
|=1))
{
398 ok = false0;
399 }
400 ENDTRYif(!(except_state&1) && exc != 0) except_rethrow(
exc); except_free(except_ch.except_obj.except_dyndata); except_pop
(); };}
;
10
Taking false branch
401
402 if (ok) {
11
Assuming 'ok' is true
12
Taking true branch
403 /*
404 * Cannot pass a tvb free callback that frees the wmem
405 * pool, so we make an extra copy that uses bare
406 * pointers. This could be optimized if tvb API had a
407 * free pool callback of some sort.
408 */
409 unsigned size = wmem_array_get_count(obuf);
410 uint8_t *p = (uint8_t *)g_malloc(size);
13
Memory is allocated
411 memcpy(p, wmem_array_get_raw(obuf), size);
412 out = tvb_new_real_data(p, size, size);
14
Potential leak of memory pointed to by 'p'
413 tvb_set_free_cb(out, g_free);
414 } else {
415 out = NULL((void*)0);
416 }
417
418 wmem_destroy_allocator(pool);
419
420 return out;
421}
422
423tvbuff_t *
424tvb_uncompress_lz77huff(tvbuff_t *tvb,
425 const unsigned offset,
426 unsigned input_size)
427{
428 return tvb_uncompress_lz77huff_max(tvb, offset, input_size, MAX_OUTPUT_SIZE(16*1024*1024));
2
Calling 'tvb_uncompress_lz77huff_max'
429}
430
431tvbuff_t *
432tvb_child_uncompress_lz77huff(tvbuff_t *parent, tvbuff_t *tvb, const unsigned offset, unsigned in_size)
433{
434 tvbuff_t *new_tvb = tvb_uncompress_lz77huff(tvb, offset, in_size);
1
Calling 'tvb_uncompress_lz77huff'
435 if (new_tvb)
436 tvb_set_child_real_data_tvbuff(parent, new_tvb);
437 return new_tvb;
438}
439
440/*
441 * Editor modelines - https://www.wireshark.org/tools/modelines.html
442 *
443 * Local variables:
444 * c-basic-offset: 8
445 * tab-width: 8
446 * indent-tabs-mode: t
447 * End:
448 *
449 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
450 * :indentSize=8:tabSize=8:noTabs=false:
451 */