| 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 | |
| 30 | #include <common/macros.h> |
| 31 | |
| 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.value); |
| 81 | str = node->u.load.u.string.value; |
| 82 | |
| 83 | for (;;) { |
| 84 | enum parse_char_result res; |
| 85 | |
| 86 | if (!(*str)) { |
| 87 | break; |
| 88 | } |
| 89 | |
| 90 | res = parse_char(&str); |
| 91 | str++; |
| 92 | |
| 93 | switch (res) { |
| 94 | case PARSE_CHAR_UNKNOWN: |
| 95 | ret = -EINVAL; |
| 96 | fprintf(stderr, |
| 97 | "Unsupported escape character detected.\n"); |
| 98 | goto end_load; |
| 99 | case PARSE_CHAR_NORMAL: |
| 100 | default: |
| 101 | break; |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | end_load: |
| 106 | return ret; |
| 107 | } |
| 108 | case IR_OP_UNARY: |
| 109 | return validate_string(node->u.unary.child); |
| 110 | case IR_OP_BINARY: |
| 111 | { |
| 112 | int ret = validate_string(node->u.binary.left); |
| 113 | |
| 114 | if (ret) |
| 115 | return ret; |
| 116 | return validate_string(node->u.binary.right); |
| 117 | } |
| 118 | case IR_OP_LOGICAL: |
| 119 | { |
| 120 | int ret; |
| 121 | |
| 122 | ret = validate_string(node->u.logical.left); |
| 123 | if (ret) |
| 124 | return ret; |
| 125 | return validate_string(node->u.logical.right); |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | LTTNG_HIDDEN |
| 131 | int filter_visitor_ir_validate_string(struct filter_parser_ctx *ctx) |
| 132 | { |
| 133 | return validate_string(ctx->ir_root); |
| 134 | } |