2 * filter-visitor-ir-validate-globbing.c
4 * LTTng filter IR validate globbing
6 * Copyright 2017 Philippe Proulx <pproulx@efficios.com>
8 * SPDX-License-Identifier: LGPL-2.1-only
12 #include "filter-ast.hpp"
13 #include "filter-ir.hpp"
14 #include "filter-parser.hpp"
16 #include <common/compat/errno.hpp>
17 #include <common/macros.hpp>
25 static int validate_globbing(struct ir_op
*node
)
32 fprintf(stderr
, "[error] %s: unknown op type\n", __func__
);
36 return validate_globbing(node
->u
.root
.child
);
40 return validate_globbing(node
->u
.unary
.child
);
43 struct ir_op
*left
= node
->u
.binary
.left
;
44 struct ir_op
*right
= node
->u
.binary
.right
;
46 if (left
->op
== IR_OP_LOAD
&& right
->op
== IR_OP_LOAD
&&
47 left
->data_type
== IR_DATA_STRING
&& right
->data_type
== IR_DATA_STRING
) {
49 if (left
->u
.load
.u
.string
.type
== IR_LOAD_STRING_TYPE_GLOB_STAR
&&
50 right
->u
.load
.u
.string
.type
!= IR_LOAD_STRING_TYPE_PLAIN
) {
51 fprintf(stderr
, "[error] Cannot compare two globbing patterns\n");
55 if (right
->u
.load
.u
.string
.type
== IR_LOAD_STRING_TYPE_GLOB_STAR
&&
56 left
->u
.load
.u
.string
.type
!= IR_LOAD_STRING_TYPE_PLAIN
) {
57 fprintf(stderr
, "[error] Cannot compare two globbing patterns\n");
62 if ((left
->op
== IR_OP_LOAD
&& left
->data_type
== IR_DATA_STRING
) ||
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
)) {
69 if (node
->u
.binary
.type
!= AST_OP_EQ
&&
70 node
->u
.binary
.type
!= AST_OP_NE
) {
72 "[error] Only the `==` and `!=` operators are allowed with a globbing pattern\n");
78 ret
= validate_globbing(left
);
83 return validate_globbing(right
);
86 ret
= validate_globbing(node
->u
.logical
.left
);
89 return validate_globbing(node
->u
.logical
.right
);
94 * This function recursively validates that:
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.
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.
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 !=.
108 int filter_visitor_ir_validate_globbing(struct filter_parser_ctx
*ctx
)
110 return validate_globbing(ctx
->ir_root
);