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
18 #include <common/compat/errno.hpp>
19 #include <common/macros.hpp>
21 #include "filter-ast.hpp"
22 #include "filter-parser.hpp"
23 #include "filter-ir.hpp"
26 int validate_globbing(struct ir_op
*node
)
33 fprintf(stderr
, "[error] %s: unknown op type\n", __func__
);
37 return validate_globbing(node
->u
.root
.child
);
41 return validate_globbing(node
->u
.unary
.child
);
44 struct ir_op
*left
= node
->u
.binary
.left
;
45 struct ir_op
*right
= node
->u
.binary
.right
;
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
) {
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");
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");
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
)) {
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");
77 ret
= validate_globbing(left
);
82 return validate_globbing(right
);
85 ret
= validate_globbing(node
->u
.logical
.left
);
88 return validate_globbing(node
->u
.logical
.right
);
93 * This function recursively validates that:
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.
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.
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 !=.
107 int filter_visitor_ir_validate_globbing(struct filter_parser_ctx
*ctx
)
109 return validate_globbing(ctx
->ir_root
);