| 1 | /* |
| 2 | * filter-visitor-ir-validate-globbing.c |
| 3 | * |
| 4 | * LTTng filter IR validate globbing |
| 5 | * |
| 6 | * Copyright 2017 Philippe Proulx <pproulx@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/compat/errno.h> |
| 19 | #include <common/macros.h> |
| 20 | |
| 21 | #include "filter-ast.h" |
| 22 | #include "filter-parser.hpp" |
| 23 | #include "filter-ir.h" |
| 24 | |
| 25 | static |
| 26 | int validate_globbing(struct ir_op *node) |
| 27 | { |
| 28 | int ret; |
| 29 | |
| 30 | switch (node->op) { |
| 31 | case IR_OP_UNKNOWN: |
| 32 | default: |
| 33 | fprintf(stderr, "[error] %s: unknown op type\n", __func__); |
| 34 | return -EINVAL; |
| 35 | |
| 36 | case IR_OP_ROOT: |
| 37 | return validate_globbing(node->u.root.child); |
| 38 | case IR_OP_LOAD: |
| 39 | return 0; |
| 40 | case IR_OP_UNARY: |
| 41 | return validate_globbing(node->u.unary.child); |
| 42 | case IR_OP_BINARY: |
| 43 | { |
| 44 | struct ir_op *left = node->u.binary.left; |
| 45 | struct ir_op *right = node->u.binary.right; |
| 46 | |
| 47 | if (left->op == IR_OP_LOAD && right->op == IR_OP_LOAD && |
| 48 | left->data_type == IR_DATA_STRING && |
| 49 | right->data_type == IR_DATA_STRING) { |
| 50 | /* Test 1. */ |
| 51 | if (left->u.load.u.string.type == IR_LOAD_STRING_TYPE_GLOB_STAR && |
| 52 | right->u.load.u.string.type != IR_LOAD_STRING_TYPE_PLAIN) { |
| 53 | fprintf(stderr, "[error] Cannot compare two globbing patterns\n"); |
| 54 | return -1; |
| 55 | } |
| 56 | |
| 57 | if (right->u.load.u.string.type == IR_LOAD_STRING_TYPE_GLOB_STAR && |
| 58 | left->u.load.u.string.type != IR_LOAD_STRING_TYPE_PLAIN) { |
| 59 | fprintf(stderr, "[error] Cannot compare two globbing patterns\n"); |
| 60 | return -1; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | if ((left->op == IR_OP_LOAD && left->data_type == IR_DATA_STRING) || |
| 65 | (right->op == IR_OP_LOAD && right->data_type == IR_DATA_STRING)) { |
| 66 | if ((left->op == IR_OP_LOAD && left->u.load.u.string.type == IR_LOAD_STRING_TYPE_GLOB_STAR) || |
| 67 | (right->op == IR_OP_LOAD && right->u.load.u.string.type == IR_LOAD_STRING_TYPE_GLOB_STAR)) { |
| 68 | /* Test 2. */ |
| 69 | if (node->u.binary.type != AST_OP_EQ && |
| 70 | node->u.binary.type != AST_OP_NE) { |
| 71 | fprintf(stderr, "[error] Only the `==` and `!=` operators are allowed with a globbing pattern\n"); |
| 72 | return -1; |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | ret = validate_globbing(left); |
| 78 | if (ret) { |
| 79 | return ret; |
| 80 | } |
| 81 | |
| 82 | return validate_globbing(right); |
| 83 | } |
| 84 | case IR_OP_LOGICAL: |
| 85 | ret = validate_globbing(node->u.logical.left); |
| 86 | if (ret) |
| 87 | return ret; |
| 88 | return validate_globbing(node->u.logical.right); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | /* |
| 93 | * This function recursively validates that: |
| 94 | * |
| 95 | * 1. When there's a binary operation between two literal strings, |
| 96 | * if one of them has the IR_LOAD_STRING_TYPE_GLOB_STAR type, |
| 97 | * the other one has the IR_LOAD_STRING_TYPE_PLAIN type. |
| 98 | * |
| 99 | * In other words, you cannot compare two globbing patterns, except |
| 100 | * for two globbing patterns with only a star at the end for backward |
| 101 | * compatibility reasons. |
| 102 | * |
| 103 | * 2. When there's a binary operation between two literal strings, if |
| 104 | * one of them is a (full) star globbing pattern, the binary |
| 105 | * operation is either == or !=. |
| 106 | */ |
| 107 | int filter_visitor_ir_validate_globbing(struct filter_parser_ctx *ctx) |
| 108 | { |
| 109 | return validate_globbing(ctx->ir_root); |
| 110 | } |