Bug Summary

File:builds/wireshark/wireshark/epan/dfilter/syntax-tree.c
Warning:line 103, column 14
Value stored to 's' during its initialization is never read

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 syntax-tree.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 -D CARES_NO_DEPRECATED -D G_DISABLE_DEPRECATED -D G_DISABLE_SINGLE_INCLUDES -D WS_BUILD_DLL -D WS_DEBUG -D WS_DEBUG_UTF_8 -I /builds/wireshark/wireshark/build -I /builds/wireshark/wireshark -I /builds/wireshark/wireshark/include -I /builds/wireshark/wireshark/epan/dfilter -I /builds/wireshark/wireshark/build/epan/dfilter -I /builds/wireshark/wireshark/epan -I /builds/wireshark/wireshark/tools/lemon -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-03-100333-3661-1 -x c /builds/wireshark/wireshark/epan/dfilter/syntax-tree.c
1/*
2 * Wireshark - Network traffic analyzer
3 * By Gerald Combs <[email protected]>
4 * Copyright 2001 Gerald Combs
5 *
6 * SPDX-License-Identifier: GPL-2.0-or-later
7 */
8
9#include "config.h"
10#define WS_LOG_DOMAIN"DFilter" LOG_DOMAIN_DFILTER"DFilter"
11
12#include "syntax-tree.h"
13#include <wsutil/wmem/wmem.h>
14#include <wsutil/str_util.h>
15#include <wsutil/glib-compat.h>
16#include "sttype-op.h"
17#include "sttype-function.h"
18#include "dfilter-int.h"
19
20/* Keep track of sttype_t's via their sttype_id_t number */
21static sttype_t* type_list[STTYPE_NUM_TYPES];
22
23
24void
25sttype_init(void)
26{
27 sttype_register_field();
28 sttype_register_function();
29 sttype_register_number();
30 sttype_register_pointer();
31 sttype_register_set();
32 sttype_register_slice();
33 sttype_register_string();
34 sttype_register_opers();
35}
36
37void
38sttype_cleanup(void)
39{
40 /* nothing to do */
41}
42
43
44void
45sttype_register(sttype_t *type)
46{
47 sttype_id_t type_id;
48
49 type_id = type->id;
50
51 /* Check input */
52 ws_assert(type_id < STTYPE_NUM_TYPES)do { if ((1) && !(type_id < STTYPE_NUM_TYPES)) ws_log_fatal_full
("DFilter", LOG_LEVEL_ERROR, "epan/dfilter/syntax-tree.c", 52
, __func__, "assertion failed: %s", "type_id < STTYPE_NUM_TYPES"
); } while (0)
;
53
54 /* Don't re-register. */
55 ws_assert(type_list[type_id] == NULL)do { if ((1) && !(type_list[type_id] == ((void*)0))) ws_log_fatal_full
("DFilter", LOG_LEVEL_ERROR, "epan/dfilter/syntax-tree.c", 55
, __func__, "assertion failed: %s", "type_list[type_id] == ((void*)0)"
); } while (0)
;
56
57 type_list[type_id] = type;
58}
59
60static sttype_t*
61sttype_lookup(sttype_id_t type_id)
62{
63 sttype_t *result;
64
65 /* Check input */
66 ws_assert(type_id < STTYPE_NUM_TYPES)do { if ((1) && !(type_id < STTYPE_NUM_TYPES)) ws_log_fatal_full
("DFilter", LOG_LEVEL_ERROR, "epan/dfilter/syntax-tree.c", 66
, __func__, "assertion failed: %s", "type_id < STTYPE_NUM_TYPES"
); } while (0)
;
67
68 result = type_list[type_id];
69
70 /* Check output. */
71 ws_assert(result != NULL)do { if ((1) && !(result != ((void*)0))) ws_log_fatal_full
("DFilter", LOG_LEVEL_ERROR, "epan/dfilter/syntax-tree.c", 71
, __func__, "assertion failed: %s", "result != ((void*)0)"); }
while (0)
;
72
73 return result;
74}
75
76const char *
77sttype_name(const sttype_id_t type)
78{
79 switch (type) {
80 case STTYPE_UNINITIALIZED: return "UNINITIALIZED";
81 case STTYPE_TEST: return "TEST";
82 case STTYPE_LITERAL: return "LITERAL";
83 case STTYPE_UNPARSED: return "UNPARSED";
84 case STTYPE_REFERENCE: return "REFERENCE";
85 case STTYPE_STRING: return "STRING";
86 case STTYPE_CHARCONST: return "CHARCONST";
87 case STTYPE_NUMBER: return "NUMBER";
88 case STTYPE_FIELD: return "FIELD";
89 case STTYPE_FVALUE: return "FVALUE";
90 case STTYPE_SLICE: return "SLICE";
91 case STTYPE_FUNCTION: return "FUNCTION";
92 case STTYPE_SET: return "SET";
93 case STTYPE_PCRE: return "PCRE";
94 case STTYPE_ARITHMETIC: return "ARITHMETIC";
95 case STTYPE_NUM_TYPES: return "NUM_TYPES";
96 }
97 return "(unknown sttype)";
98}
99
100const char *
101stnode_op_name(const stnode_op_t op)
102{
103 const char *s = "(null)";
Value stored to 's' during its initialization is never read
104
105 switch(op) {
106 case STNODE_OP_NOT:
107 s = "TEST_NOT";
108 break;
109 case STNODE_OP_AND:
110 s = "TEST_AND";
111 break;
112 case STNODE_OP_OR:
113 s = "TEST_OR";
114 break;
115 case STNODE_OP_XOR:
116 s = "TEST_XOR";
117 break;
118 case STNODE_OP_ALL_EQ:
119 s = "TEST_ALL_EQ";
120 break;
121 case STNODE_OP_ANY_EQ:
122 s = "TEST_ANY_EQ";
123 break;
124 case STNODE_OP_ALL_NE:
125 s = "TEST_ALL_NE";
126 break;
127 case STNODE_OP_ANY_NE:
128 s = "TEST_ANY_NE";
129 break;
130 case STNODE_OP_GT:
131 s = "TEST_GT";
132 break;
133 case STNODE_OP_GE:
134 s = "TEST_GE";
135 break;
136 case STNODE_OP_LT:
137 s = "TEST_LT";
138 break;
139 case STNODE_OP_LE:
140 s = "TEST_LE";
141 break;
142 case STNODE_OP_BITWISE_AND:
143 s = "OP_BITWISE_AND";
144 break;
145 case STNODE_OP_UNARY_MINUS:
146 s = "OP_UNARY_MINUS";
147 break;
148 case STNODE_OP_ADD:
149 s = "OP_ADD";
150 break;
151 case STNODE_OP_SUBTRACT:
152 s = "OP_SUBTRACT";
153 break;
154 case STNODE_OP_MULTIPLY:
155 s = "OP_MULTIPLY";
156 break;
157 case STNODE_OP_DIVIDE:
158 s = "OP_DIVIDE";
159 break;
160 case STNODE_OP_MODULO:
161 s = "OP_MODULO";
162 break;
163 case STNODE_OP_CONTAINS:
164 s = "TEST_CONTAINS";
165 break;
166 case STNODE_OP_MATCHES:
167 s = "TEST_MATCHES";
168 break;
169 case STNODE_OP_IN:
170 s = "TEST_IN";
171 break;
172 case STNODE_OP_NOT_IN:
173 s = "TEST_NOT_IN";
174 break;
175 case STNODE_OP_UNINITIALIZED:
176 s = "(uninitialized)";
177 break;
178 }
179
180 return s;
181}
182
183void
184stnode_clear(stnode_t *node)
185{
186 if (node->type) {
187 if (node->type->func_free && node->data) {
188 node->type->func_free(node->data);
189 }
190 }
191 else {
192 ws_assert(!node->data)do { if ((1) && !(!node->data)) ws_log_fatal_full(
"DFilter", LOG_LEVEL_ERROR, "epan/dfilter/syntax-tree.c", 192
, __func__, "assertion failed: %s", "!node->data"); } while
(0)
;
193 }
194
195 node->type = NULL((void*)0);
196 node->data = NULL((void*)0);
197 g_free(node->repr_display)(__builtin_object_size ((node->repr_display), 0) != ((size_t
) - 1)) ? g_free_sized (node->repr_display, __builtin_object_size
((node->repr_display), 0)) : (g_free) (node->repr_display
)
;
198 node->repr_display = NULL((void*)0);
199 g_free(node->repr_debug)(__builtin_object_size ((node->repr_debug), 0) != ((size_t
) - 1)) ? g_free_sized (node->repr_debug, __builtin_object_size
((node->repr_debug), 0)) : (g_free) (node->repr_debug)
;
200 node->repr_debug = NULL((void*)0);
201 g_free(node->repr_token)(__builtin_object_size ((node->repr_token), 0) != ((size_t
) - 1)) ? g_free_sized (node->repr_token, __builtin_object_size
((node->repr_token), 0)) : (g_free) (node->repr_token)
;
202 node->repr_token = NULL((void*)0);
203 node->location.col_start = -1;
204 node->location.col_len = 0;
205 node->flags = 0;
206 node->depth = 0;
207}
208
209void
210stnode_init(stnode_t *node, sttype_id_t type_id, void *data, char *token, df_loc_t loc)
211{
212 sttype_t *type;
213
214 ws_assert(!node->type)do { if ((1) && !(!node->type)) ws_log_fatal_full(
"DFilter", LOG_LEVEL_ERROR, "epan/dfilter/syntax-tree.c", 214
, __func__, "assertion failed: %s", "!node->type"); } while
(0)
;
215 ws_assert(!node->data)do { if ((1) && !(!node->data)) ws_log_fatal_full(
"DFilter", LOG_LEVEL_ERROR, "epan/dfilter/syntax-tree.c", 215
, __func__, "assertion failed: %s", "!node->data"); } while
(0)
;
216 node->repr_display = NULL((void*)0);
217 node->repr_debug = NULL((void*)0);
218 node->repr_token = token;
219 node->location = loc;
220 node->flags = 0;
221 node->depth = 1;
222
223 if (type_id == STTYPE_UNINITIALIZED) {
224 node->type = NULL((void*)0);
225 node->data = NULL((void*)0);
226 }
227 else {
228 /* Creating an initialized node with a NULL pointer is
229 * allowed and needs to be safe. The parser relies on that. */
230 type = sttype_lookup(type_id);
231 ws_assert(type)do { if ((1) && !(type)) ws_log_fatal_full("DFilter",
LOG_LEVEL_ERROR, "epan/dfilter/syntax-tree.c", 231, __func__
, "assertion failed: %s", "type"); } while (0)
;
232 node->type = type;
233 if (type->func_new) {
234 node->data = type->func_new(data);
235 }
236 else {
237 node->data = data;
238 }
239 }
240}
241
242void
243stnode_replace(stnode_t *node, sttype_id_t type_id, void *data)
244{
245 char *token = g_strdup(node->repr_token)g_strdup_inline (node->repr_token);
246 df_loc_t loc = node->location;
247 uint16_t flags = node->flags;
248 stnode_clear(node);
249 stnode_init(node, type_id, data, token, loc);
250 node->flags = flags;
251}
252
253void
254stnode_mutate(stnode_t *node, sttype_id_t type_id)
255{
256 //FIXME: Assert there all the same sttype
257 node->type = sttype_lookup(type_id);
258 ws_assert(node->type)do { if ((1) && !(node->type)) ws_log_fatal_full("DFilter"
, LOG_LEVEL_ERROR, "epan/dfilter/syntax-tree.c", 258, __func__
, "assertion failed: %s", "node->type"); } while (0)
;
259}
260
261stnode_t*
262stnode_new(sttype_id_t type_id, void *data, char *token, df_loc_t loc)
263{
264 stnode_t *node = g_new0(stnode_t, 1)((stnode_t *) g_malloc0_n ((1), sizeof (stnode_t)));
265 stnode_init(node, type_id, data, token, loc);
266 return node;
267}
268
269stnode_t*
270stnode_new_empty(sttype_id_t type_id)
271{
272 df_loc_t loc = {-1, 0};
273 return stnode_new(type_id, NULL((void*)0), NULL((void*)0), loc);
274}
275
276stnode_t*
277stnode_dup(const stnode_t *node)
278{
279 stnode_t *new;
280
281 new = g_new(stnode_t, 1)((stnode_t *) g_malloc_n ((1), sizeof (stnode_t)));
282 new->repr_display = NULL((void*)0);
283 new->repr_debug = NULL((void*)0);
284 new->repr_token = g_strdup(node->repr_token)g_strdup_inline (node->repr_token);
285 new->location = node->location;
286 new->flags = node->flags;
287 new->depth = node->depth;
288
289 new->type = node->type;
290 if (node->type == NULL((void*)0))
291 new->data = NULL((void*)0);
292 else if (node->type->func_dup)
293 new->data = node->type->func_dup(node->data);
294 else
295 new->data = node->data;
296
297 return new;
298}
299
300void
301stnode_free(stnode_t *node)
302{
303 stnode_clear(node);
304 g_free(node)(__builtin_object_size ((node), 0) != ((size_t) - 1)) ? g_free_sized
(node, __builtin_object_size ((node), 0)) : (g_free) (node)
;
305}
306
307const char*
308stnode_type_name(const stnode_t *node)
309{
310 return sttype_name(node->type->id);
311}
312
313sttype_id_t
314stnode_type_id(const stnode_t *node)
315{
316 if (node->type)
317 return node->type->id;
318 else
319 return STTYPE_UNINITIALIZED;
320}
321
322void *
323stnode_data(stnode_t *node)
324{
325 return node->data;
326}
327
328GString *
329stnode_string(stnode_t *node)
330{
331 ws_assert(stnode_type_id(node) == STTYPE_STRING)do { if ((1) && !(stnode_type_id(node) == STTYPE_STRING
)) ws_log_fatal_full("DFilter", LOG_LEVEL_ERROR, "epan/dfilter/syntax-tree.c"
, 331, __func__, "assertion failed: %s", "stnode_type_id(node) == STTYPE_STRING"
); } while (0)
;
332 return stnode_data(node);
333}
334
335void *
336stnode_steal_data(stnode_t *node)
337{
338 void *data = node->data;
339 ws_assert(data)do { if ((1) && !(data)) ws_log_fatal_full("DFilter",
LOG_LEVEL_ERROR, "epan/dfilter/syntax-tree.c", 339, __func__
, "assertion failed: %s", "data"); } while (0)
;
340 node->data = NULL((void*)0);
341 return data;
342}
343
344const char *
345stnode_token(const stnode_t *node)
346{
347 return node->repr_token;
348}
349
350df_loc_t
351stnode_location(const stnode_t *node)
352{
353 return node->location;
354}
355
356void
357stnode_set_location(stnode_t *node, df_loc_t loc)
358{
359 node->location = loc;
360}
361
362bool_Bool
363stnode_get_flags(stnode_t *node, uint16_t flags)
364{
365 return node->flags & flags;
366}
367
368void
369stnode_set_flags(stnode_t *node, uint16_t flags)
370{
371 node->flags |= flags;
372}
373
374/* Finds the first and last location from a set and creates
375 * a new location from start of first (col_start) to end of
376 * last (col_start + col_len). Sets the result to dst. */
377void
378stnode_merge_location(stnode_t *dst, stnode_t *n1, stnode_t *n2)
379{
380 df_loc_t first, last;
381 df_loc_t loc2;
382
383 first = last = stnode_location(n1);
384 loc2 = stnode_location(n2);
385 if (loc2.col_start >= 0 && loc2.col_start > first.col_start)
386 last = loc2;
387 dst->location.col_start = first.col_start;
388 dst->location.col_len = last.col_start - first.col_start + last.col_len;
389}
390
391unsigned
392stnode_depth(const stnode_t *node)
393{
394 return node->depth;
395}
396
397void
398stnode_update_depth(stnode_t *node, const stnode_t *child)
399{
400 if (child != NULL((void*)0) && child->depth >= node->depth)
401 node->depth = child->depth + 1;
402}
403
404#define IS_OPERATOR(node)(stnode_type_id(node) == STTYPE_TEST || stnode_type_id(node) ==
STTYPE_ARITHMETIC)
\
405 (stnode_type_id(node) == STTYPE_TEST || \
406 stnode_type_id(node) == STTYPE_ARITHMETIC)
407
408static char *
409_node_tostr(stnode_t *node, bool_Bool pretty)
410{
411 char *s, *repr;
412
413 if (node->type->func_tostr == NULL((void*)0))
414 s = g_strdup("FIXME")g_strdup_inline ("FIXME");
415 else
416 s = node->type->func_tostr(node->data, pretty);
417
418 if (pretty)
419 return s;
420
421 if (IS_OPERATOR(node)(stnode_type_id(node) == STTYPE_TEST || stnode_type_id(node) ==
STTYPE_ARITHMETIC)
) {
422 repr = s;
423 }
424 else {
425 repr = ws_strdup_printf("%s(%s)", stnode_type_name(node), s)wmem_strdup_printf(((void*)0), "%s(%s)", stnode_type_name(node
), s)
;
426 g_free(s)(__builtin_object_size ((s), 0) != ((size_t) - 1)) ? g_free_sized
(s, __builtin_object_size ((s), 0)) : (g_free) (s)
;
427 }
428
429 return repr;
430}
431
432const char *
433stnode_tostr(stnode_t *node, bool_Bool pretty)
434{
435 if (pretty && IS_OPERATOR(node)(stnode_type_id(node) == STTYPE_TEST || stnode_type_id(node) ==
STTYPE_ARITHMETIC)
&& node->repr_token != NULL((void*)0)) {
436 /* Some operators can have synonyms, like "or" and "||".
437 * Show the user the same representation as he typed. */
438 g_free(node->repr_display)(__builtin_object_size ((node->repr_display), 0) != ((size_t
) - 1)) ? g_free_sized (node->repr_display, __builtin_object_size
((node->repr_display), 0)) : (g_free) (node->repr_display
)
;
439 node->repr_display = g_strdup(node->repr_token)g_strdup_inline (node->repr_token);
440 return node->repr_display;
441 }
442
443 char *str = _node_tostr(node, pretty);
444
445 if (pretty) {
446 g_free(node->repr_display)(__builtin_object_size ((node->repr_display), 0) != ((size_t
) - 1)) ? g_free_sized (node->repr_display, __builtin_object_size
((node->repr_display), 0)) : (g_free) (node->repr_display
)
;
447 node->repr_display = str;
448 }
449 else {
450 g_free(node->repr_debug)(__builtin_object_size ((node->repr_debug), 0) != ((size_t
) - 1)) ? g_free_sized (node->repr_debug, __builtin_object_size
((node->repr_debug), 0)) : (g_free) (node->repr_debug)
;
451 node->repr_debug = str;
452 }
453
454 return str;
455}
456
457static char *
458sprint_node(stnode_t *node)
459{
460 wmem_strbuf_t *buf = wmem_strbuf_new(NULL((void*)0), NULL((void*)0));
461
462 wmem_strbuf_append_printf(buf, "{ ");
463 wmem_strbuf_append_printf(buf, "type = %s, ", stnode_type_name(node));
464 wmem_strbuf_append_printf(buf, "data = %s, ", stnode_todebug(node)stnode_tostr(node, 0));
465 wmem_strbuf_append_printf(buf, "location = %ld:%zu",
466 node->location.col_start, node->location.col_len);
467 wmem_strbuf_append_printf(buf, " }");
468 return wmem_strbuf_finalize(buf);
469}
470
471void
472log_node_full(enum ws_log_level level,
473 const char *file, int line, const char *func,
474 stnode_t *node, const char *msg)
475{
476 if (!ws_log_msg_is_active(WS_LOG_DOMAIN"DFilter", level))
477 return;
478
479 if (node == NULL((void*)0)) {
480 ws_log_write_always_full(WS_LOG_DOMAIN"DFilter", level,
481 file, line, func, "%s is NULL", msg);
482 return;
483 }
484
485 char *str = sprint_node(node);
486
487 ws_log_write_always_full(WS_LOG_DOMAIN"DFilter", level, file, line, func,
488 "%s = %s", msg, str);
489
490 g_free(str)(__builtin_object_size ((str), 0) != ((size_t) - 1)) ? g_free_sized
(str, __builtin_object_size ((str), 0)) : (g_free) (str)
;
491}
492
493void
494log_test_full(enum ws_log_level level,
495 const char *file, int line, const char *func,
496 stnode_t *node, const char *msg)
497{
498 if (!ws_log_msg_is_active(WS_LOG_DOMAIN"DFilter", level))
499 return;
500
501 if (node == NULL((void*)0)) {
502 ws_log_write_always_full(WS_LOG_DOMAIN"DFilter", level,
503 file, line, func, "%s is NULL", msg);
504 return;
505 }
506
507 stnode_op_t st_op;
508 stnode_t *st_lhs = NULL((void*)0), *st_rhs = NULL((void*)0);
509 char *lhs = NULL((void*)0), *rhs = NULL((void*)0);
510
511 sttype_oper_get(node, &st_op, &st_lhs, &st_rhs);
512
513 if (st_lhs)
514 lhs = sprint_node(st_lhs);
515 if (st_rhs)
516 rhs = sprint_node(st_rhs);
517
518 ws_log_write_always_full(WS_LOG_DOMAIN"DFilter", level, file, line, func,
519 "%s:\n LHS = %s\n RHS = %s",
520 stnode_todebug(node)stnode_tostr(node, 0),
521 lhs ? lhs : "NULL",
522 rhs ? rhs : "NULL");
523
524 g_free(lhs)(__builtin_object_size ((lhs), 0) != ((size_t) - 1)) ? g_free_sized
(lhs, __builtin_object_size ((lhs), 0)) : (g_free) (lhs)
;
525 g_free(rhs)(__builtin_object_size ((rhs), 0) != ((size_t) - 1)) ? g_free_sized
(rhs, __builtin_object_size ((rhs), 0)) : (g_free) (rhs)
;
526}
527
528static void
529indent(wmem_strbuf_t *buf, int level)
530{
531 for (int i = 0; i < level * 2; i++) {
532 wmem_strbuf_append_c(buf, ' ');
533 }
534 wmem_strbuf_append_printf(buf, "% 2d ", level);
535}
536
537static void
538visit_tree(wmem_strbuf_t *buf, stnode_t *node, int level)
539{
540 stnode_t *left, *right;
541 stnode_t *lower, *upper;
542 GSList *params;
543 GSList *nodelist;
544
545 if (stnode_type_id(node) == STTYPE_TEST ||
546 stnode_type_id(node) == STTYPE_ARITHMETIC) {
547 wmem_strbuf_append_printf(buf, "%s:\n", stnode_todebug(node)stnode_tostr(node, 0));
548 sttype_oper_get(node, NULL((void*)0), &left, &right);
549 if (left && right) {
550 indent(buf, level + 1);
551 visit_tree(buf, left, level + 1);
552 wmem_strbuf_append_c(buf, '\n');
553 indent(buf, level + 1);
554 visit_tree(buf, right, level + 1);
555 }
556 else if (left) {
557 indent(buf, level + 1);
558 visit_tree(buf, left, level + 1);
559 }
560 else if (right) {
561 ws_assert_not_reached()ws_log_fatal_full("DFilter", LOG_LEVEL_ERROR, "epan/dfilter/syntax-tree.c"
, 561, __func__, "assertion \"not reached\" failed")
;
562 }
563 }
564 else if (stnode_type_id(node) == STTYPE_SET) {
565 nodelist = stnode_data(node);
566 wmem_strbuf_append_printf(buf, "SET(#%u):\n", g_slist_length(nodelist) / 2);
567 while (nodelist) {
568 indent(buf, level + 1);
569 lower = nodelist->data;
570 wmem_strbuf_append(buf, stnode_tostr(lower, false0));
571 /* Set elements are always in pairs; upper may be null. */
572 nodelist = g_slist_next(nodelist)((nodelist) ? (((GSList *)(nodelist))->next) : ((void*)0));
573 ws_assert(nodelist)do { if ((1) && !(nodelist)) ws_log_fatal_full("DFilter"
, LOG_LEVEL_ERROR, "epan/dfilter/syntax-tree.c", 573, __func__
, "assertion failed: %s", "nodelist"); } while (0)
;
574 upper = nodelist->data;
575 if (upper != NULL((void*)0)) {
576 wmem_strbuf_append(buf, " .. ");
577 wmem_strbuf_append(buf, stnode_tostr(upper, false0));
578 }
579 nodelist = g_slist_next(nodelist)((nodelist) ? (((GSList *)(nodelist))->next) : ((void*)0));
580 if (nodelist != NULL((void*)0)) {
581 wmem_strbuf_append_c(buf, '\n');
582 }
583 }
584 }
585 else if (stnode_type_id(node) == STTYPE_FUNCTION) {
586 wmem_strbuf_append_printf(buf, "%s:\n", stnode_todebug(node)stnode_tostr(node, 0));
587 params = sttype_function_params(node);
588 while (params) {
589 indent(buf, level + 1);
590 visit_tree(buf, params->data, level + 1);
591 if (params->next != NULL((void*)0)) {
592 wmem_strbuf_append_c(buf, '\n');
593 }
594 params = params->next;
595 }
596 }
597 else {
598 wmem_strbuf_append(buf, stnode_todebug(node)stnode_tostr(node, 0));
599 }
600}
601
602char *
603dump_syntax_tree_str(stnode_t *root)
604{
605 wmem_strbuf_t *buf = wmem_strbuf_new(NULL((void*)0), NULL((void*)0));
606 indent(buf, 0);
607 visit_tree(buf, root, 0);
608 return wmem_strbuf_finalize(buf);
609}
610
611void
612log_syntax_tree(enum ws_log_level level, stnode_t *root, const char *msg, char **cache_ptr)
613{
614 if (!ws_log_msg_is_active(LOG_DOMAIN_DFILTER"DFilter", level))
615 return;
616
617 char *str = dump_syntax_tree_str(root);
618
619 ws_log_write_always_full(LOG_DOMAIN_DFILTER"DFilter", level, NULL((void*)0), -1, NULL((void*)0),
620 "%s:\n%s", msg, str);
621
622 if (cache_ptr) {
623 *cache_ptr = str;
624 }
625 else {
626 g_free(str)(__builtin_object_size ((str), 0) != ((size_t) - 1)) ? g_free_sized
(str, __builtin_object_size ((str), 0)) : (g_free) (str)
;
627 }
628}
629
630/*
631 * Editor modelines - https://www.wireshark.org/tools/modelines.html
632 *
633 * Local variables:
634 * c-basic-offset: 8
635 * tab-width: 8
636 * indent-tabs-mode: t
637 * End:
638 *
639 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
640 * :indentSize=8:tabSize=8:noTabs=false:
641 */