diff --git a/src/pattern.c b/src/pattern.c index 2d0c88845..6817bf11e 100644 --- a/src/pattern.c +++ b/src/pattern.c @@ -432,6 +432,33 @@ struct pattern *pat_match_nothing(struct sample *smp, struct pattern_expr *expr, return NULL; } +/* ensure the input sample can be read as a string without knowing its size, + * that is, ensure the terminating null byte is there + * + * The function may fail. Returns 1 on success and 0 on failure + */ +static inline int pat_match_ensure_str(struct sample *smp) +{ + if (smp->data.u.str.data < smp->data.u.str.size) { + /* we have to force a trailing zero on the test pattern and + * the buffer is large enough to accommodate it. If the flag + * CONST is set, duplicate the string + */ + if (smp->flags & SMP_F_CONST) { + if (!smp_dup(smp)) + return 0; + } else + smp->data.u.str.area[smp->data.u.str.data] = '\0'; + } + else { + /* Otherwise, the sample is duplicated. A trailing zero + * is automatically added to the string. + */ + if (!smp_dup(smp)) + return 0; + } + return 1; +} /* NB: For two strings to be identical, it is required that their length match */ struct pattern *pat_match_str(struct sample *smp, struct pattern_expr *expr, int fill) @@ -446,34 +473,10 @@ struct pattern *pat_match_str(struct sample *smp, struct pattern_expr *expr, int /* Lookup a string in the expression's pattern tree. */ if (!eb_is_empty(&expr->pattern_tree)) { - char prev = 0; - - if (smp->data.u.str.data < smp->data.u.str.size) { - /* we may have to force a trailing zero on the test pattern and - * the buffer is large enough to accommodate it. If the flag - * CONST is set, duplicate the string - */ - prev = smp->data.u.str.area[smp->data.u.str.data]; - if (prev) { - if (smp->flags & SMP_F_CONST) { - if (!smp_dup(smp)) - return NULL; - } else { - smp->data.u.str.area[smp->data.u.str.data] = '\0'; - } - } - } - else { - /* Otherwise, the sample is duplicated. A trailing zero - * is automatically added to the string. - */ - if (!smp_dup(smp)) - return NULL; - } + if (!pat_match_ensure_str(smp)) + return NULL; node = ebst_lookup(&expr->pattern_tree, smp->data.u.str.area); - if (prev) - smp->data.u.str.area[smp->data.u.str.data] = prev; while (node) { elt = ebmb_entry(node, struct pattern_tree, node); @@ -647,35 +650,11 @@ struct pattern *pat_match_beg(struct sample *smp, struct pattern_expr *expr, int /* Lookup a string in the expression's pattern tree. */ if (!eb_is_empty(&expr->pattern_tree)) { - char prev = 0; - - if (smp->data.u.str.data < smp->data.u.str.size) { - /* we may have to force a trailing zero on the test pattern and - * the buffer is large enough to accommodate it. If the flag - * CONST is set, duplicate the string - */ - prev = smp->data.u.str.area[smp->data.u.str.data]; - if (prev) { - if (smp->flags & SMP_F_CONST) { - if (!smp_dup(smp)) - return NULL; - } else { - smp->data.u.str.area[smp->data.u.str.data] = '\0'; - } - } - } - else { - /* Otherwise, the sample is duplicated. A trailing zero - * is automatically added to the string. - */ - if (!smp_dup(smp)) - return NULL; - } + if (!pat_match_ensure_str(smp)) + return NULL; node = ebmb_lookup_longest(&expr->pattern_tree, smp->data.u.str.area); - if (prev) - smp->data.u.str.area[smp->data.u.str.data] = prev; while (node) { elt = ebmb_entry(node, struct pattern_tree, node);