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