From: Karl Fleischmann Date: Thu, 10 Nov 2022 12:28:54 +0000 (+0100) Subject: lib: Try to interpret filter values as sizes when parsing integer event values X-Git-Tag: 2.4.0~3395 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8ba3481f04a05ad3fda88af05830af4ea1dd4b48;p=thirdparty%2Fdovecot%2Fcore.git lib: Try to interpret filter values as sizes when parsing integer event values This allows event filter values to be given as "16M" or "1G". --- diff --git a/src/lib/event-filter-parser.y b/src/lib/event-filter-parser.y index 12b7d4bbc5..dbf936b3c4 100644 --- a/src/lib/event-filter-parser.y +++ b/src/lib/event-filter-parser.y @@ -10,6 +10,7 @@ %{ #include "lib.h" +#include "str-parse.h" #include "wildcard-match.h" #include "lib-event-private.h" #include "event-filter-private.h" @@ -97,13 +98,23 @@ static struct event_filter_node *key_value(struct event_filter_parser_state *sta /* Filter currently supports only comparing strings and numbers. */ - if (str_to_intmax(b, &node->field.value.intmax) < 0) { - /* not a number - no problem - Either we have a string, or a number with wildcards */ - node->field.value.intmax = INT_MIN; - } else { - /* leave a hint that this is in fact a valid number */ + if (str_to_intmax(b, &node->field.value.intmax) == 0) { + /* Leave a hint that this is in fact a valid number. */ node->field.value_type = EVENT_FIELD_VALUE_TYPE_INTMAX; + } else { + /* This field contains no valid number. + Either this is a string that contains a size unit, a + number with wildcard or another arbitrary string. */ + node->field.value.intmax = INT_MIN; + + /* If the field contains a size unit, take that. */ + uoff_t bytes; + const char *error; + int ret = str_parse_get_size(b, &bytes, &error); + if (ret == 0 && bytes <= INTMAX_MAX) { + node->field.value.intmax = (intmax_t) bytes; + node->field.value_type = EVENT_FIELD_VALUE_TYPE_INTMAX; + } } if (wildcard_is_literal(node->field.value.str))