Commit | Line | Data |
---|---|---|
dcd5daf2 JG |
1 | /* |
2 | * filter-visitor-ir-validate-string.c | |
3 | * | |
4 | * LTTng filter IR validate string | |
5 | * | |
6 | * Copyright 2014 - Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
7 | * | |
8 | * This library is free software; you can redistribute it and/or modify it | |
9 | * under the terms of the GNU Lesser General Public License, version 2.1 only, | |
10 | * as published by the Free Software Foundation. | |
11 | * | |
12 | * This library is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | * Lesser General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU Lesser General Public License | |
18 | * along with this library; if not, write to the Free Software Foundation, | |
19 | * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
20 | */ | |
21 | ||
22 | #include <stdio.h> | |
23 | #include <unistd.h> | |
24 | #include <string.h> | |
25 | #include <stdlib.h> | |
26 | #include <assert.h> | |
27 | #include <errno.h> | |
28 | #include <inttypes.h> | |
ed4549a1 DG |
29 | |
30 | #include <common/macros.h> | |
31 | ||
dcd5daf2 JG |
32 | #include "filter-ast.h" |
33 | #include "filter-parser.h" | |
34 | #include "filter-ir.h" | |
35 | ||
36 | enum parse_char_result { | |
37 | PARSE_CHAR_UNKNOWN = -2, | |
38 | PARSE_CHAR_WILDCARD = -1, | |
39 | PARSE_CHAR_NORMAL = 0, | |
40 | }; | |
41 | ||
42 | static | |
43 | enum parse_char_result parse_char(const char **p) | |
44 | { | |
45 | switch (**p) { | |
46 | case '\\': | |
47 | (*p)++; | |
48 | switch (**p) { | |
49 | case '\\': | |
50 | case '*': | |
51 | return PARSE_CHAR_NORMAL; | |
52 | default: | |
53 | return PARSE_CHAR_UNKNOWN; | |
54 | } | |
55 | case '*': | |
56 | return PARSE_CHAR_WILDCARD; | |
57 | default: | |
58 | return PARSE_CHAR_NORMAL; | |
59 | } | |
60 | } | |
61 | ||
62 | static | |
63 | int validate_string(struct ir_op *node) | |
64 | { | |
65 | switch (node->op) { | |
66 | case IR_OP_UNKNOWN: | |
67 | default: | |
68 | fprintf(stderr, "[error] %s: unknown op type\n", __func__); | |
69 | return -EINVAL; | |
70 | ||
71 | case IR_OP_ROOT: | |
72 | return validate_string(node->u.root.child); | |
73 | case IR_OP_LOAD: | |
74 | { | |
75 | int ret = 0; | |
76 | ||
77 | if (node->data_type == IR_DATA_STRING) { | |
78 | const char *str; | |
79 | ||
80 | assert(node->u.load.u.string); | |
81 | str = node->u.load.u.string; | |
82 | ||
83 | /* | |
84 | * Make sure that if a non-escaped wildcard is | |
85 | * present, it is the last character of the string. | |
86 | */ | |
87 | for (;;) { | |
88 | enum parse_char_result res; | |
89 | ||
90 | if (!(*str)) { | |
91 | break; | |
92 | } | |
93 | ||
94 | res = parse_char(&str); | |
95 | str++; | |
96 | ||
97 | switch (res) { | |
98 | case PARSE_CHAR_WILDCARD: | |
99 | { | |
100 | if (*str) { | |
101 | /* | |
102 | * Found a wildcard followed by non-null | |
103 | * character; unsupported. | |
104 | */ | |
105 | ret = -EINVAL; | |
106 | fprintf(stderr, | |
107 | "Wildcards may only be used as the last character of a string in a filter.\n"); | |
108 | goto end_load; | |
109 | } | |
110 | break; | |
111 | } | |
112 | case PARSE_CHAR_UNKNOWN: | |
113 | ret = -EINVAL; | |
114 | fprintf(stderr, | |
115 | "Unsupported escape character detected.\n"); | |
116 | goto end_load; | |
117 | case PARSE_CHAR_NORMAL: | |
118 | default: | |
119 | break; | |
120 | } | |
121 | } | |
122 | } | |
123 | end_load: | |
124 | return ret; | |
125 | } | |
126 | case IR_OP_UNARY: | |
127 | return validate_string(node->u.unary.child); | |
128 | case IR_OP_BINARY: | |
129 | { | |
130 | int ret = validate_string(node->u.binary.left); | |
131 | ||
132 | if (ret) | |
133 | return ret; | |
134 | return validate_string(node->u.binary.right); | |
135 | } | |
136 | case IR_OP_LOGICAL: | |
137 | { | |
138 | int ret; | |
139 | ||
140 | ret = validate_string(node->u.logical.left); | |
141 | if (ret) | |
142 | return ret; | |
143 | return validate_string(node->u.logical.right); | |
144 | } | |
145 | } | |
146 | } | |
147 | ||
ed4549a1 | 148 | LTTNG_HIDDEN |
dcd5daf2 JG |
149 | int filter_visitor_ir_validate_string(struct filter_parser_ctx *ctx) |
150 | { | |
151 | return validate_string(ctx->ir_root); | |
152 | } |