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
19 #include <common/compat/errno.h>
20 #include <common/macros.h>
22 #include "filter-ast.h"
23 #include "filter-parser.h"
24 #include "filter-ir.h"
27 int validate_globbing(struct ir_op
*node
)
34 fprintf(stderr
, "[error] %s: unknown op type\n", __func__
);
38 return validate_globbing(node
->u
.root
.child
);
42 return validate_globbing(node
->u
.unary
.child
);
45 struct ir_op
*left
= node
->u
.binary
.left
;
46 struct ir_op
*right
= node
->u
.binary
.right
;
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
) {
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");
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");
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
)) {
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");
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 !=.
109 int filter_visitor_ir_validate_globbing(struct filter_parser_ctx
*ctx
)
111 return validate_globbing(ctx
->ir_root
);