Commit | Line | Data |
---|---|---|
dcd5daf2 JG |
1 | /* |
2 | * filter-visitor-ir-validate-string.c | |
3 | * | |
4 | * LTTng filter IR validate string | |
5 | * | |
ab5be9fa | 6 | * Copyright 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
dcd5daf2 | 7 | * |
ab5be9fa | 8 | * SPDX-License-Identifier: LGPL-2.1-only |
dcd5daf2 | 9 | * |
dcd5daf2 JG |
10 | */ |
11 | ||
28ab034a JG |
12 | #include "filter-ast.hpp" |
13 | #include "filter-ir.hpp" | |
14 | #include "filter-parser.hpp" | |
ed4549a1 | 15 | |
c9e313bc | 16 | #include <common/compat/errno.hpp> |
28ab034a | 17 | #include <common/macros.hpp> |
ed4549a1 | 18 | |
28ab034a JG |
19 | #include <inttypes.h> |
20 | #include <stdio.h> | |
21 | #include <stdlib.h> | |
22 | #include <string.h> | |
23 | #include <unistd.h> | |
dcd5daf2 JG |
24 | |
25 | enum parse_char_result { | |
26 | PARSE_CHAR_UNKNOWN = -2, | |
27 | PARSE_CHAR_WILDCARD = -1, | |
28 | PARSE_CHAR_NORMAL = 0, | |
29 | }; | |
30 | ||
28ab034a | 31 | static enum parse_char_result parse_char(const char **p) |
dcd5daf2 JG |
32 | { |
33 | switch (**p) { | |
34 | case '\\': | |
35 | (*p)++; | |
36 | switch (**p) { | |
37 | case '\\': | |
38 | case '*': | |
39 | return PARSE_CHAR_NORMAL; | |
40 | default: | |
41 | return PARSE_CHAR_UNKNOWN; | |
42 | } | |
43 | case '*': | |
44 | return PARSE_CHAR_WILDCARD; | |
45 | default: | |
46 | return PARSE_CHAR_NORMAL; | |
47 | } | |
48 | } | |
49 | ||
28ab034a | 50 | static int validate_string(struct ir_op *node) |
dcd5daf2 JG |
51 | { |
52 | switch (node->op) { | |
53 | case IR_OP_UNKNOWN: | |
54 | default: | |
55 | fprintf(stderr, "[error] %s: unknown op type\n", __func__); | |
56 | return -EINVAL; | |
57 | ||
58 | case IR_OP_ROOT: | |
59 | return validate_string(node->u.root.child); | |
60 | case IR_OP_LOAD: | |
61 | { | |
62 | int ret = 0; | |
63 | ||
64 | if (node->data_type == IR_DATA_STRING) { | |
65 | const char *str; | |
66 | ||
a0377dfe | 67 | LTTNG_ASSERT(node->u.load.u.string.value); |
9f449915 | 68 | str = node->u.load.u.string.value; |
dcd5daf2 | 69 | |
dcd5daf2 JG |
70 | for (;;) { |
71 | enum parse_char_result res; | |
72 | ||
73 | if (!(*str)) { | |
74 | break; | |
75 | } | |
76 | ||
77 | res = parse_char(&str); | |
78 | str++; | |
79 | ||
80 | switch (res) { | |
dcd5daf2 JG |
81 | case PARSE_CHAR_UNKNOWN: |
82 | ret = -EINVAL; | |
28ab034a | 83 | fprintf(stderr, "Unsupported escape character detected.\n"); |
dcd5daf2 JG |
84 | goto end_load; |
85 | case PARSE_CHAR_NORMAL: | |
86 | default: | |
87 | break; | |
88 | } | |
89 | } | |
90 | } | |
28ab034a | 91 | end_load: |
dcd5daf2 JG |
92 | return ret; |
93 | } | |
94 | case IR_OP_UNARY: | |
95 | return validate_string(node->u.unary.child); | |
96 | case IR_OP_BINARY: | |
97 | { | |
98 | int ret = validate_string(node->u.binary.left); | |
99 | ||
100 | if (ret) | |
101 | return ret; | |
102 | return validate_string(node->u.binary.right); | |
103 | } | |
104 | case IR_OP_LOGICAL: | |
105 | { | |
106 | int ret; | |
107 | ||
108 | ret = validate_string(node->u.logical.left); | |
109 | if (ret) | |
110 | return ret; | |
111 | return validate_string(node->u.logical.right); | |
112 | } | |
113 | } | |
114 | } | |
115 | ||
116 | int filter_visitor_ir_validate_string(struct filter_parser_ctx *ctx) | |
117 | { | |
118 | return validate_string(ctx->ir_root); | |
119 | } |