X-Git-Url: https://git.lttng.org/?p=lttng-tools.git;a=blobdiff_plain;f=src%2Flib%2Flttng-ctl%2Ffilter%2Ffilter-visitor-ir-validate-globbing.c;fp=src%2Flib%2Flttng-ctl%2Ffilter%2Ffilter-visitor-ir-validate-globbing.c;h=4f825cbc46c1fa892f28624f689d5a41013a1832;hp=0000000000000000000000000000000000000000;hb=9f449915b9d44ce3c9c9255f5d491a62545fee25;hpb=9c55c24114a3ba83a423690b0e86d64c9b2fb027 diff --git a/src/lib/lttng-ctl/filter/filter-visitor-ir-validate-globbing.c b/src/lib/lttng-ctl/filter/filter-visitor-ir-validate-globbing.c new file mode 100644 index 000000000..4f825cbc4 --- /dev/null +++ b/src/lib/lttng-ctl/filter/filter-visitor-ir-validate-globbing.c @@ -0,0 +1,122 @@ +/* + * filter-visitor-ir-validate-globbing.c + * + * LTTng filter IR validate globbing + * + * Copyright 2017 - Philippe Proulx + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License, version 2.1 only, + * as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include +#include +#include +#include +#include +#include +#include + +#include + +#include "filter-ast.h" +#include "filter-parser.h" +#include "filter-ir.h" + +static +int validate_globbing(struct ir_op *node) +{ + int ret; + + switch (node->op) { + case IR_OP_UNKNOWN: + default: + fprintf(stderr, "[error] %s: unknown op type\n", __func__); + return -EINVAL; + + case IR_OP_ROOT: + return validate_globbing(node->u.root.child); + case IR_OP_LOAD: + return 0; + case IR_OP_UNARY: + return validate_globbing(node->u.unary.child); + case IR_OP_BINARY: + { + struct ir_op *left = node->u.binary.left; + struct ir_op *right = node->u.binary.right; + + if (left->op == IR_OP_LOAD && right->op == IR_OP_LOAD && + left->data_type == IR_DATA_STRING && + right->data_type == IR_DATA_STRING) { + /* Test 1. */ + if (left->u.load.u.string.type == IR_LOAD_STRING_TYPE_GLOB_STAR && + right->u.load.u.string.type != IR_LOAD_STRING_TYPE_PLAIN) { + fprintf(stderr, "[error] Cannot compare two globbing patterns\n"); + return -1; + } + + if (right->u.load.u.string.type == IR_LOAD_STRING_TYPE_GLOB_STAR && + left->u.load.u.string.type != IR_LOAD_STRING_TYPE_PLAIN) { + fprintf(stderr, "[error] Cannot compare two globbing patterns\n"); + return -1; + } + } + + if ((left->op == IR_OP_LOAD && left->data_type == IR_DATA_STRING) || + (right->op == IR_OP_LOAD && right->data_type == IR_DATA_STRING)) { + if ((left->op == IR_OP_LOAD && left->u.load.u.string.type == IR_LOAD_STRING_TYPE_GLOB_STAR) || + (right->op == IR_OP_LOAD && right->u.load.u.string.type == IR_LOAD_STRING_TYPE_GLOB_STAR)) { + /* Test 2. */ + if (node->u.binary.type != AST_OP_EQ && + node->u.binary.type != AST_OP_NE) { + fprintf(stderr, "[error] Only the `==` and `!=` operators are allowed with a globbing pattern\n"); + return -1; + } + } + } + + ret = validate_globbing(left); + if (ret) { + return ret; + } + + return validate_globbing(right); + } + case IR_OP_LOGICAL: + ret = validate_globbing(node->u.logical.left); + if (ret) + return ret; + return validate_globbing(node->u.logical.right); + } +} + +/* + * This function recursively validates that: + * + * 1. When there's a binary operation between two literal strings, + * if one of them has the IR_LOAD_STRING_TYPE_GLOB_STAR type, + * the other one has the IR_LOAD_STRING_TYPE_PLAIN type. + * + * In other words, you cannot compare two globbing patterns, except + * for two globbing patterns with only a star at the end for backward + * compatibility reasons. + * + * 2. When there's a binary operation between two literal strings, if + * one of them is a (full) star globbing pattern, the binary + * operation is either == or !=. + */ +LTTNG_HIDDEN +int filter_visitor_ir_validate_globbing(struct filter_parser_ctx *ctx) +{ + return validate_globbing(ctx->ir_root); +}