From: Karl Fleischmann Date: Wed, 21 Dec 2022 13:15:22 +0000 (+0100) Subject: lib: Warn about event filter type mismatches if comparing integer fields with strcmp... X-Git-Tag: 2.4.0~3238 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=62b844fa3db84baccb99e50fcc04ccae6ae9d98e;p=thirdparty%2Fdovecot%2Fcore.git lib: Warn about event filter type mismatches if comparing integer fields with strcmp flag Reorder branches to preserve existing behavior. --- diff --git a/src/lib/event-filter.c b/src/lib/event-filter.c index 3bb52d6d2b..b4884a911d 100644 --- a/src/lib/event-filter.c +++ b/src/lib/event-filter.c @@ -634,8 +634,12 @@ event_match_field(struct event *event, struct event_filter_node *node, node->warned_ambiguous_unit = TRUE; } return FALSE; - } else if ((wanted_field->value_type != EVENT_FIELD_VALUE_TYPE_INTMAX) && - (node->type != EVENT_FILTER_NODE_TYPE_EVENT_FIELD_NUMERIC_WILDCARD)) { + } else if (wanted_field->value_type == EVENT_FIELD_VALUE_TYPE_INTMAX) { + /* compare against an integer */ + return event_filter_handle_numeric_operation( + node->op, field->value.intmax, wanted_field->value.intmax); + } else if (use_strcmp || + (node->type != EVENT_FILTER_NODE_TYPE_EVENT_FIELD_NUMERIC_WILDCARD)) { if (!node->warned_type_mismatch) { const char *name = event->sending_name; /* Use i_warning to prevent event filter recursions. */ @@ -649,18 +653,9 @@ event_match_field(struct event *event, struct event_filter_node *node, node->warned_type_mismatch = TRUE; } return FALSE; - } else if (wanted_field->value_type == EVENT_FIELD_VALUE_TYPE_INTMAX) { - /* compare against an integer */ - return event_filter_handle_numeric_operation( - node->op, field->value.intmax, wanted_field->value.intmax); } else if (node->op != EVENT_FILTER_OP_CMP_EQ) { /* we only support string equality comparisons */ return FALSE; - } else if (use_strcmp) { - /* If the matched value was a number, it was already matched - in the previous branch. So here we have a non-wildcard - string, which can never be a match to a number. */ - return FALSE; } else { char tmp[MAX_INT_STRLEN]; i_snprintf(tmp, sizeof(tmp), "%jd", field->value.intmax); diff --git a/src/lib/test-event-filter.c b/src/lib/test-event-filter.c index 1d7e71b6be..25d3359864 100644 --- a/src/lib/test-event-filter.c +++ b/src/lib/test-event-filter.c @@ -653,6 +653,14 @@ static void test_event_filter_numbers(void) test_assert(event_filter_match(filter, e, &failure_ctx)); event_filter_unref(&filter); + filter = event_filter_create(); + test_assert(event_filter_parse("number > fish", filter, &error) == 0); + test_expect_error_string("Event filter matches integer field 'number' " + "against non-integer value 'fish'"); + test_assert(!event_filter_match(filter, e, &failure_ctx)); + test_expect_no_more_errors(); + event_filter_unref(&filter); + filter = event_filter_create(); test_assert(event_filter_parse("number=fish", filter, &error) == 0); test_expect_error_string("Event filter matches integer field 'number' "