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