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> | |
29 | #include "filter-ast.h" | |
30 | #include "filter-parser.h" | |
31 | #include "filter-ir.h" | |
32 | ||
33 | enum parse_char_result { | |
34 | PARSE_CHAR_UNKNOWN = -2, | |
35 | PARSE_CHAR_WILDCARD = -1, | |
36 | PARSE_CHAR_NORMAL = 0, | |
37 | }; | |
38 | ||
39 | static | |
40 | enum parse_char_result parse_char(const char **p) | |
41 | { | |
42 | switch (**p) { | |
43 | case '\\': | |
44 | (*p)++; | |
45 | switch (**p) { | |
46 | case '\\': | |
47 | case '*': | |
48 | return PARSE_CHAR_NORMAL; | |
49 | default: | |
50 | return PARSE_CHAR_UNKNOWN; | |
51 | } | |
52 | case '*': | |
53 | return PARSE_CHAR_WILDCARD; | |
54 | default: | |
55 | return PARSE_CHAR_NORMAL; | |
56 | } | |
57 | } | |
58 | ||
59 | static | |
60 | int validate_string(struct ir_op *node) | |
61 | { | |
62 | switch (node->op) { | |
63 | case IR_OP_UNKNOWN: | |
64 | default: | |
65 | fprintf(stderr, "[error] %s: unknown op type\n", __func__); | |
66 | return -EINVAL; | |
67 | ||
68 | case IR_OP_ROOT: | |
69 | return validate_string(node->u.root.child); | |
70 | case IR_OP_LOAD: | |
71 | { | |
72 | int ret = 0; | |
73 | ||
74 | if (node->data_type == IR_DATA_STRING) { | |
75 | const char *str; | |
76 | ||
77 | assert(node->u.load.u.string); | |
78 | str = node->u.load.u.string; | |
79 | ||
80 | /* | |
81 | * Make sure that if a non-escaped wildcard is | |
82 | * present, it is the last character of the string. | |
83 | */ | |
84 | for (;;) { | |
85 | enum parse_char_result res; | |
86 | ||
87 | if (!(*str)) { | |
88 | break; | |
89 | } | |
90 | ||
91 | res = parse_char(&str); | |
92 | str++; | |
93 | ||
94 | switch (res) { | |
95 | case PARSE_CHAR_WILDCARD: | |
96 | { | |
97 | if (*str) { | |
98 | /* | |
99 | * Found a wildcard followed by non-null | |
100 | * character; unsupported. | |
101 | */ | |
102 | ret = -EINVAL; | |
103 | fprintf(stderr, | |
104 | "Wildcards may only be used as the last character of a string in a filter.\n"); | |
105 | goto end_load; | |
106 | } | |
107 | break; | |
108 | } | |
109 | case PARSE_CHAR_UNKNOWN: | |
110 | ret = -EINVAL; | |
111 | fprintf(stderr, | |
112 | "Unsupported escape character detected.\n"); | |
113 | goto end_load; | |
114 | case PARSE_CHAR_NORMAL: | |
115 | default: | |
116 | break; | |
117 | } | |
118 | } | |
119 | } | |
120 | end_load: | |
121 | return ret; | |
122 | } | |
123 | case IR_OP_UNARY: | |
124 | return validate_string(node->u.unary.child); | |
125 | case IR_OP_BINARY: | |
126 | { | |
127 | int ret = validate_string(node->u.binary.left); | |
128 | ||
129 | if (ret) | |
130 | return ret; | |
131 | return validate_string(node->u.binary.right); | |
132 | } | |
133 | case IR_OP_LOGICAL: | |
134 | { | |
135 | int ret; | |
136 | ||
137 | ret = validate_string(node->u.logical.left); | |
138 | if (ret) | |
139 | return ret; | |
140 | return validate_string(node->u.logical.right); | |
141 | } | |
142 | } | |
143 | } | |
144 | ||
145 | int filter_visitor_ir_validate_string(struct filter_parser_ctx *ctx) | |
146 | { | |
147 | return validate_string(ctx->ir_root); | |
148 | } |