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