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 | ||
12 | #include <stdio.h> | |
13 | #include <unistd.h> | |
14 | #include <string.h> | |
15 | #include <stdlib.h> | |
16 | #include <assert.h> | |
9f449915 PP |
17 | #include <inttypes.h> |
18 | ||
edf4b93e | 19 | #include <common/compat/errno.h> |
9f449915 PP |
20 | #include <common/macros.h> |
21 | ||
22 | #include "filter-ast.h" | |
23 | #include "filter-parser.h" | |
24 | #include "filter-ir.h" | |
25 | ||
26 | static | |
27 | int validate_globbing(struct ir_op *node) | |
28 | { | |
29 | int ret; | |
30 | ||
31 | switch (node->op) { | |
32 | case IR_OP_UNKNOWN: | |
33 | default: | |
34 | fprintf(stderr, "[error] %s: unknown op type\n", __func__); | |
35 | return -EINVAL; | |
36 | ||
37 | case IR_OP_ROOT: | |
38 | return validate_globbing(node->u.root.child); | |
39 | case IR_OP_LOAD: | |
40 | return 0; | |
41 | case IR_OP_UNARY: | |
42 | return validate_globbing(node->u.unary.child); | |
43 | case IR_OP_BINARY: | |
44 | { | |
45 | struct ir_op *left = node->u.binary.left; | |
46 | struct ir_op *right = node->u.binary.right; | |
47 | ||
48 | if (left->op == IR_OP_LOAD && right->op == IR_OP_LOAD && | |
49 | left->data_type == IR_DATA_STRING && | |
50 | right->data_type == IR_DATA_STRING) { | |
51 | /* Test 1. */ | |
52 | if (left->u.load.u.string.type == IR_LOAD_STRING_TYPE_GLOB_STAR && | |
53 | right->u.load.u.string.type != IR_LOAD_STRING_TYPE_PLAIN) { | |
54 | fprintf(stderr, "[error] Cannot compare two globbing patterns\n"); | |
55 | return -1; | |
56 | } | |
57 | ||
58 | if (right->u.load.u.string.type == IR_LOAD_STRING_TYPE_GLOB_STAR && | |
59 | left->u.load.u.string.type != IR_LOAD_STRING_TYPE_PLAIN) { | |
60 | fprintf(stderr, "[error] Cannot compare two globbing patterns\n"); | |
61 | return -1; | |
62 | } | |
63 | } | |
64 | ||
65 | if ((left->op == IR_OP_LOAD && left->data_type == IR_DATA_STRING) || | |
66 | (right->op == IR_OP_LOAD && right->data_type == IR_DATA_STRING)) { | |
67 | if ((left->op == IR_OP_LOAD && left->u.load.u.string.type == IR_LOAD_STRING_TYPE_GLOB_STAR) || | |
68 | (right->op == IR_OP_LOAD && right->u.load.u.string.type == IR_LOAD_STRING_TYPE_GLOB_STAR)) { | |
69 | /* Test 2. */ | |
70 | if (node->u.binary.type != AST_OP_EQ && | |
71 | node->u.binary.type != AST_OP_NE) { | |
72 | fprintf(stderr, "[error] Only the `==` and `!=` operators are allowed with a globbing pattern\n"); | |
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 | */ | |
108 | LTTNG_HIDDEN | |
109 | int filter_visitor_ir_validate_globbing(struct filter_parser_ctx *ctx) | |
110 | { | |
111 | return validate_globbing(ctx->ir_root); | |
112 | } |