Skip to content

Commit 0781ef3

Browse files
committed
Filter: fix warnings and memory leak
1 parent 5172a6f commit 0781ef3

10 files changed

Lines changed: 76 additions & 45 deletions

File tree

include/libfds/ipfix_filter.h

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,47 +12,47 @@ extern "C" {
1212
typedef struct fds_ipfix_filter fds_ipfix_filter_t;
1313

1414
/**
15-
*
15+
*
1616
* Creates an IPFIX filter from an expression.
1717
*
1818
* \param[out] ipfil Pointer to where to allocate and construct the filter
1919
* \param[in] iemgr The information elements manager
2020
* \param[in] expr The filter expression
21-
*
21+
*
2222
* On error, more detailed information about the error can be obtained using fds_filter_ipfix_get_error.
2323
* After the filter structure is destroyed the error is destroyed along with it!
2424
*
2525
* \return FDS_OK on success, else one of FDS_ERR_NOMEM, FDS_ERR_SYNTAX, FDS_ERR_SEMANTIC.
2626
*/
27-
FDS_API int
28-
fds_ipfix_filter_create(fds_ipfix_filter_t **ipxfil, fds_iemgr_t *iemgr, const char *expr);
27+
FDS_API int
28+
fds_ipfix_filter_create(fds_ipfix_filter_t **ipxfil, const fds_iemgr_t *iemgr, const char *expr);
2929

3030
/**
3131
* Evaluates the IPFIX filter using the provided data record.
32-
*
32+
*
3333
* \param[in] ipxfil The IPFIX filter
34-
* \param[in] record A data record
34+
* \param[in] record A data record
3535
*
36-
* \return true if the data record passes the filter, false if not
36+
* \return true if the data record passes the filter, false if not
3737
*/
3838
FDS_API bool
3939
fds_ipfix_filter_eval(struct fds_ipfix_filter *ipxfil, struct fds_drec *record);
4040

4141
/**
4242
* Destroys the IPFIX filter.
43-
*
43+
*
4444
* \param[in] ipxfil The filter to be destroyed.
4545
*/
4646
FDS_API void
4747
fds_ipfix_filter_destroy(fds_ipfix_filter_t *ipxfil);
4848

4949
/**
5050
* Gets the error from an IPFIX filter.
51-
*
51+
*
5252
* If the filter parameter is NULL then memory error is assumed.
53-
*
53+
*
5454
* \param[in] ipxfil The IPFIX filter
55-
*
55+
*
5656
* \return Pointer to the error, or NULL if there is no error.
5757
*/
5858
FDS_API const char *

src/filter/common.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* \date 2020
66
*/
77

8-
/*
8+
/*
99
* Copyright (C) 2020 CESNET, z.s.p.o.
1010
*
1111
* Redistribution and use in source and binary forms, with or without
@@ -47,7 +47,7 @@
4747
#ifdef FDS_FILTER_DEBUG
4848
#define IF_DEBUG(X) X
4949
#else
50-
#define IF_DEBUG(X)
50+
#define IF_DEBUG(X)
5151
#endif
5252

5353
#define CONST_ARR_SIZE(x) (sizeof(x) / sizeof((x)[0]))

src/filter/error.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242

4343
#include "error.h"
4444

45-
const fds_filter_error_s memory_error = { .code = FDS_ERR_NOMEM, .msg = "out of memory" };
45+
fds_filter_error_s memory_error = { .code = FDS_ERR_NOMEM, .msg = "out of memory" };
4646

4747
error_t const NO_ERROR = NULL;
4848
error_t const MEMORY_ERROR = &memory_error;

src/filter/eval_common.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ destroy_eval_node(eval_node_s *en)
116116
}
117117
if (en->opcode == EVAL_OP_NONE && en->destructor_fn) {
118118
en->destructor_fn(&en->value);
119+
memset(&en->value, 0, sizeof(fds_filter_value_u));
119120
}
120121
free(en);
121122
}
@@ -161,7 +162,6 @@ print_eval_tree_rec(FILE *out, eval_node_s *node, int indent_level)
161162

162163
PRINT_INDENT();
163164

164-
fprintf(out, "(%s, ", eval_opcode_to_str(node->opcode));
165165
fprintf(out, "data type: %s, value: ", data_type_to_str(node->datatype));
166166

167167
print_value(out, node->datatype, &node->value);

src/filter/eval_generator.c

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ delete_value_from_et(fds_filter_value_u *value, eval_node_s *tree)
6565
// if the value matches by value zero it out
6666
if (memcmp(&tree->value, value, sizeof(fds_filter_value_u)) == 0) {
6767
tree->value = (fds_filter_value_u){0};
68+
tree->destructor_fn = NULL;
6869
}
6970
}
7071

@@ -167,7 +168,7 @@ list_to_literal(fds_filter_ast_node_s *ast, fds_filter_opts_t *opts, fds_filter_
167168
}
168169

169170
// Evaluate each list item node and populate the list
170-
int idx = 0;
171+
uint64_t idx = 0;
171172
for (fds_filter_ast_node_s *li = ast->child; li != NULL; li = li->next) {
172173
assert(idx < out_list->len);
173174

@@ -222,6 +223,8 @@ process_root_node(fds_filter_ast_node_s *ast, fds_filter_opts_t *opts, bool seco
222223
static error_t
223224
process_constructor_node(fds_filter_ast_node_s *ast, fds_filter_opts_t *opts, bool second_run, eval_node_s **out_eval_node)
224225
{
226+
(void) second_run;
227+
225228
// Must be const, should be assured by semantic analysis
226229
assert(ast->flags & FDS_FAF_CONST_SUBTREE);
227230

@@ -265,6 +268,9 @@ process_constructor_node(fds_filter_ast_node_s *ast, fds_filter_opts_t *opts, bo
265268
static error_t
266269
process_exists_node(fds_filter_ast_node_s *ast, fds_filter_opts_t *opts, bool second_run, eval_node_s **out_eval_node)
267270
{
271+
(void) second_run;
272+
(void) opts;
273+
268274
// Must have a __name__ node as its only child
269275
assert(ast_node_symbol_is(ast->child, "__name__"));
270276

@@ -286,6 +292,8 @@ process_exists_node(fds_filter_ast_node_s *ast, fds_filter_opts_t *opts, bool se
286292
static error_t
287293
process_name_node(fds_filter_ast_node_s *ast, fds_filter_opts_t *opts, bool second_run, eval_node_s **out_eval_node)
288294
{
295+
(void) second_run;
296+
289297
eval_node_s *en = create_eval_node();
290298
if (!en) {
291299
return MEMORY_ERROR;
@@ -309,6 +317,8 @@ process_name_node(fds_filter_ast_node_s *ast, fds_filter_opts_t *opts, bool seco
309317
static error_t
310318
process_literal_node(fds_filter_ast_node_s *ast, fds_filter_opts_t *opts, bool second_run, eval_node_s **out_eval_node)
311319
{
320+
(void) second_run;
321+
312322
eval_node_s *en = create_eval_node();
313323
if (!en) {
314324
return MEMORY_ERROR;
@@ -332,6 +342,8 @@ process_literal_node(fds_filter_ast_node_s *ast, fds_filter_opts_t *opts, bool s
332342
static error_t
333343
process_list_node(fds_filter_ast_node_s *ast, fds_filter_opts_t *opts, bool second_run, eval_node_s **out_eval_node)
334344
{
345+
(void) second_run;
346+
335347
eval_node_s *en = create_eval_node();
336348
if (!en) {
337349
return MEMORY_ERROR;
@@ -473,9 +485,17 @@ generate_eval_tree(fds_filter_ast_node_s *ast, fds_filter_opts_t *opts, bool sec
473485
en->opcode = EVAL_OP_NONE;
474486
IF_DEBUG(en->datatype = ast->datatype;)
475487
error_t err = ast_to_literal(ast, opts, &en->value);
488+
if (err != NO_ERROR) {
489+
destroy_eval_node(en);
490+
return err;
491+
}
492+
fds_filter_op_s *destructor = find_destructor(opts->op_list, ast->datatype);
493+
if (destructor) {
494+
en->destructor_fn = destructor->destructor_fn;
495+
}
476496
*out_eval_node = en;
477497

478-
// ast->flags &= ~FDS_FAF_DESTROY_VAL;
498+
ast->flags &= ~FDS_FAF_DESTROY_VAL;
479499

480500
return NO_ERROR;
481501
}

src/filter/opts.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ dummy_data_callback(void *user_ctx, bool reset_ctx, int id, void *data, fds_filt
8484
return FDS_ERR_NOTFOUND;
8585
}
8686

87+
/*
8788
static void
8889
print_op_list(FILE *out, fds_filter_op_s *op_list)
8990
{
@@ -92,6 +93,7 @@ print_op_list(FILE *out, fds_filter_op_s *op_list)
9293
fprintf(out, "\n");
9394
}
9495
}
96+
*/
9597

9698
static inline int
9799
op_list_count(const fds_filter_op_s *op_list)

src/filter/parser.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ const char *name_prefixes[] = {
112112
static bool
113113
token_is_name_prefix(struct token_s token)
114114
{
115-
for (int i = 0; i < CONST_ARR_SIZE(name_prefixes); i++) {
115+
for (size_t i = 0; i < CONST_ARR_SIZE(name_prefixes); i++) {
116116
if (token_is_symbol(token, name_prefixes[i])) {
117117
return true;
118118
}
@@ -124,7 +124,7 @@ token_is_name_prefix(struct token_s token)
124124
struct operator_s *
125125
find_operator(enum op_kind_e kind, const char *symbol)
126126
{
127-
for (int i = 0; i < CONST_ARR_SIZE(op_parse_def_table); i++) {
127+
for (size_t i = 0; i < CONST_ARR_SIZE(op_parse_def_table); i++) {
128128
struct operator_s *o = &op_parse_def_table[i];
129129
if (o->kind == kind && strcmp(o->symbol, symbol) == 0) {
130130
return o;
@@ -193,9 +193,9 @@ parse_infix_expr(struct scanner_s *scanner, int prec, fds_filter_ast_node_s **ou
193193
// we want the recursive parse call to only match the 2 and leave the next + to end up with a parse tree:
194194
//
195195
// (+) <-- the call one level above in the recursive call tree is creating this node
196-
// / \
196+
// / \ .
197197
// we are creating this node ---> (+) (4)
198-
// / \
198+
// / \ .
199199
// this is our left expr ---> (2) (3) <--- this part is returned from the following call
200200
//
201201
err = parse_infix_expr(scanner, o->prec + 1, &right_expr);
@@ -210,9 +210,9 @@ parse_infix_expr(struct scanner_s *scanner, int prec, fds_filter_ast_node_s **ou
210210
// the following ** to end up with a parse tree:
211211
//
212212
// (**) <--- we are creating this node
213-
// / \
213+
// / \ .
214214
// this is our left ---> (2) (**) <--- this part is returned from the following call
215-
// / \
215+
// / \ .
216216
// (3) (4)
217217
err = parse_infix_expr(scanner, o->prec, &right_expr);
218218
}
@@ -361,7 +361,6 @@ parse_prefix_expr(struct scanner_s *scanner, fds_filter_ast_node_s **out_ast)
361361
}
362362

363363
// create the list item node
364-
fds_filter_ast_node_s *parent_node = *item_node_ptr;
365364
*item_node_ptr = create_unary_ast_node("__listitem__", expr_node);
366365
if (*item_node_ptr == NULL) {
367366
destroy_ast(list_node);
@@ -405,7 +404,6 @@ parse_prefix_expr(struct scanner_s *scanner, fds_filter_ast_node_s **out_ast)
405404
fds_filter_ast_node_s *expr;
406405
err = parse_infix_expr(scanner, o->prec + 1, &expr);
407406
if (err != NO_ERROR) {
408-
409407
return err;
410408
}
411409

@@ -414,6 +412,8 @@ parse_prefix_expr(struct scanner_s *scanner, fds_filter_ast_node_s **out_ast)
414412
destroy_ast(expr);
415413
return MEMORY_ERROR;
416414
}
415+
ast->cursor_begin = cursor_begin;
416+
ast->cursor_end = token.cursor_end;
417417

418418
*out_ast = ast;
419419
return NO_ERROR;

0 commit comments

Comments
 (0)