Improved filter validator
- Can no longer do "stringfield = INTEGER" or "intfield = string" - Print slightly more sane error messages
This commit is contained in:
parent
a01a99e239
commit
f483288e5a
61
src/filter.c
61
src/filter.c
@ -13,20 +13,55 @@ node_t *zfilter;
|
|||||||
|
|
||||||
static int validate_node(node_t *node, fielddefset_t *fields)
|
static int validate_node(node_t *node, fielddefset_t *fields)
|
||||||
{
|
{
|
||||||
int i;
|
int index, found = 0;
|
||||||
if (node->type != FIELD) {
|
if (node->type == OP) {
|
||||||
|
// These end up getting validated later
|
||||||
|
if (node->value.op == AND || node->value.op == OR) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
// Comparison node (=, >, <, etc.)
|
||||||
for (i = 0; i < fields->len; i++) {
|
// Validate that the field (left child) exists in the fieldset
|
||||||
if (fields->fielddefs[i].name) {
|
for (index = 0; index < fields->len; index++) {
|
||||||
if (strcmp(fields->fielddefs[i].name, node->value.field.fieldname) == 0) {
|
if (fields->fielddefs[index].name) {
|
||||||
node->value.field.index = i;
|
if (strcmp(fields->fielddefs[index].name,
|
||||||
|
node->left_child->value.field.fieldname) == 0) {
|
||||||
|
node->left_child->value.field.index = index;
|
||||||
|
found = 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!found) {
|
||||||
|
fprintf(stderr, "Field '%s' does not exist\n",
|
||||||
|
node->left_child->value.field.fieldname);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
// Fieldname is fine, match the type.
|
||||||
|
switch (node->right_child->type) {
|
||||||
|
case STRING:
|
||||||
|
if (strcmp(fields->fielddefs[index].type, "string") == 0) {
|
||||||
|
return 1;
|
||||||
|
} else {
|
||||||
|
fprintf(stderr, "Field '%s' is not of type 'string'\n",
|
||||||
|
fields->fielddefs[index].name);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
case INT:
|
||||||
|
if (strcmp(fields->fielddefs[index].type, "int") == 0) {
|
||||||
|
return 1;
|
||||||
|
} else {
|
||||||
|
fprintf(stderr, "Field '%s' is not of type 'int'\n",
|
||||||
|
fields->fielddefs[index].name);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// All non-op nodes are valid
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
// Didn't validate
|
||||||
}
|
|
||||||
// Didn't find it
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -45,6 +80,11 @@ int parse_filter_string(char *filter)
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 0 Valid
|
||||||
|
* -1 Invalid Field Name
|
||||||
|
* -2 Type Mismatch
|
||||||
|
*/
|
||||||
int validate_filter(node_t *root, fielddefset_t *fields)
|
int validate_filter(node_t *root, fielddefset_t *fields)
|
||||||
{
|
{
|
||||||
int valid;
|
int valid;
|
||||||
@ -55,6 +95,5 @@ int validate_filter(node_t *root, fielddefset_t *fields)
|
|||||||
if (!valid) {
|
if (!valid) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
return (validate_filter(root->left_child, fields) && validate_filter(root->right_child, fields));
|
return (validate_filter(root->left_child, fields) && validate_filter(root->right_child, fields));
|
||||||
}
|
}
|
||||||
|
@ -508,7 +508,7 @@ int main(int argc, char *argv[])
|
|||||||
|
|
||||||
// Check the fields used against the fieldset in use
|
// Check the fields used against the fieldset in use
|
||||||
if (!validate_filter(zconf.filter.expression, &zconf.fsconf.defs)) {
|
if (!validate_filter(zconf.filter.expression, &zconf.fsconf.defs)) {
|
||||||
log_fatal("zmap", "Field does not exist");
|
log_fatal("zmap", "Invalid filter");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user