Bug Summary

File:builds/wireshark/wireshark/epan/tvbuff_lznt1.c
Warning:line 155, 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_lznt1.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-11-09-100327-3661-1 -x c /builds/wireshark/wireshark/epan/tvbuff_lznt1.c
1/*
2 * Decompression code for LZNT1. This encoding is used by
3 * Microsoft in various file formats and protocols including SMB3.
4 *
5 * See MS-XCA.
6 *
7 * Copyright (C) 2019 Aurélien Aptel
8 *
9 * Wireshark - Network traffic analyzer
10 * By Gerald Combs <[email protected]>
11 * Copyright 1998 Gerald Combs
12 *
13 * SPDX-License-Identifier: GPL-2.0-or-later
14 */
15
16#include <glib.h>
17#include <epan/exceptions.h>
18#include <epan/tvbuff.h>
19#include <epan/wmem_scopes.h>
20
21#define MAX_INPUT_SIZE(16*1024*1024) (16*1024*1024) /* 16MB */
22
23static bool_Bool
24uncompress_chunk(tvbuff_t *tvb, int offset, unsigned in_size, wmem_array_t *obuf)
25{
26 unsigned in_off = 0, out_off = 0, out_start = 0;
27 uint8_t flags;
28 unsigned i, j, pos;
29 uint8_t val;
30
31 out_start = wmem_array_get_count(obuf);
32
33 while (in_off < in_size) {
34 flags = tvb_get_uint8(tvb, offset+in_off);
35 in_off++;
36 for (i = 0; i < 8; i++) {
37 if (0 == ((flags>>i)&1)) {
38 val = tvb_get_uint8(tvb, offset+in_off);
39 in_off++;
40 wmem_array_append_one(obuf, val)wmem_array_append((obuf), &(val), 1);
41 out_off++;
42 } else {
43 unsigned f, l_mask = 0x0FFF, o_shift = 12;
44 unsigned match_len, match_off;
45
46 f = tvb_get_letohs(tvb, offset+in_off);
47 in_off += 2;
48 /* Let M be the largest value in [4...12] such
49 * that 2^{M-1} < U, or 4 if there is no such.
50 * Equivalently, we can repeatedly divide U by
51 * 2, but we need to subtract 1 first and test
52 * vs a weak inequality in order to account for
53 * truncation. (But don't overflow at U == 0.)
54 */
55 if (out_off > 0) {
56 pos = out_off-1;
57 while (pos >= 0x10) {
58 l_mask >>= 1;
59 o_shift -= 1;
60 pos >>= 1;
61 }
62 }
63
64 match_len = (f & l_mask) + 3;
65 match_off = (f >> o_shift) + 1;
66 /* out_off only increases, only test once. */
67 if (match_off > out_start + out_off)
68 return false0;
69 /* Must call grow first, to realloc the array
70 * if needed. */
71 if (!wmem_array_grow(obuf, match_len))
72 return false0;
73 uint8_t *src;
74 for (j = 0; j < match_len / match_off; j++) {
75 src = wmem_array_index(obuf, out_start + out_off - match_off);
76 wmem_array_append(obuf, src, match_off);
77 out_off += match_off;
78 }
79 for (j *= match_off; j < match_len; j++) {
80 src = wmem_array_index(obuf, out_start + out_off - match_off);
81 wmem_array_append(obuf, src, 1);
82 out_off++;
83 }
84 }
85 if (in_off == in_size) {
86 goto out;
87 }
88 }
89 }
90out:
91 return true1;
92}
93
94static bool_Bool
95do_uncompress(tvbuff_t *tvb, int offset, int in_size, wmem_array_t *obuf)
96{
97 int in_off = 0;
98 uint32_t header, length, i;
99 bool_Bool ok;
100
101 if (!tvb)
102 return false0;
103
104 if (!in_size || in_size > MAX_INPUT_SIZE(16*1024*1024))
105 return false0;
106
107 while (in_off < in_size) {
108 header = tvb_get_letohs(tvb, offset+in_off);
109 in_off += 2;
110 length = (header & 0x0FFF) + 1;
111 if (!(header & 0x8000)) {
112 for (i = 0; i < length; i++) {
113 uint8_t v = tvb_get_uint8(tvb, offset+in_off);
114 wmem_array_append_one(obuf, v)wmem_array_append((obuf), &(v), 1);
115 in_off++;
116 }
117 } else {
118 ok = uncompress_chunk(tvb, offset + in_off, length, obuf);
119 if (!ok)
120 return false0;
121 in_off += length;
122 }
123 }
124 return true1;
125}
126
127tvbuff_t *
128tvb_uncompress_lznt1(tvbuff_t *tvb, const unsigned offset, unsigned in_size)
129{
130 volatile bool_Bool ok = false0;
131 wmem_allocator_t *pool;
132 wmem_array_t *obuf;
133 tvbuff_t *out;
134
135 pool = wmem_allocator_new(WMEM_ALLOCATOR_SIMPLE);
136 obuf = wmem_array_sized_new(pool, 1, in_size*2);
137
138 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)
{
2
Assuming the condition is false
3
Taking false branch
4
Taking false branch
5
Taking true branch
139 ok = do_uncompress(tvb, offset, in_size, obuf);
140 } CATCH_ALLif (except_state == 0 && exc != 0 && (except_state
|=1))
{
141 ok = false0;
142 }
143 ENDTRYif(!(except_state&1) && exc != 0) except_rethrow(
exc); except_free(except_ch.except_obj.except_dyndata); except_pop
(); };}
;
6
Taking false branch
144
145 if (ok) {
7
Assuming 'ok' is true
8
Taking true branch
146 /*
147 * Cannot pass a tvb free callback that frees the wmem
148 * pool, so we make an extra copy that uses bare
149 * pointers. This could be optimized if tvb API had a
150 * free pool callback of some sort.
151 */
152 unsigned size = wmem_array_get_count(obuf);
153 uint8_t *p = (uint8_t *)g_malloc(size);
9
Memory is allocated
154 memcpy(p, wmem_array_get_raw(obuf), size);
155 out = tvb_new_real_data(p, size, size);
10
Potential leak of memory pointed to by 'p'
156 tvb_set_free_cb(out, g_free);
157 } else {
158 out = NULL((void*)0);
159 }
160
161 wmem_destroy_allocator(pool);
162
163 return out;
164}
165
166tvbuff_t *
167tvb_child_uncompress_lznt1(tvbuff_t *parent, tvbuff_t *tvb, const unsigned offset, unsigned in_size)
168{
169 tvbuff_t *new_tvb = tvb_uncompress_lznt1(tvb, offset, in_size);
1
Calling 'tvb_uncompress_lznt1'
170 if (new_tvb)
171 tvb_set_child_real_data_tvbuff(parent, new_tvb);
172 return new_tvb;
173}
174
175/*
176 * Editor modelines - https://www.wireshark.org/tools/modelines.html
177 *
178 * Local variables:
179 * c-basic-offset: 8
180 * tab-width: 8
181 * indent-tabs-mode: t
182 * End:
183 *
184 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
185 * :indentSize=8:tabSize=8:noTabs=false:
186 */