dfilter: Improve error message for "matches"

Should be more obvious that this error is caused
by a string syntax error and not something else.
This commit is contained in:
João Valverde 2021-10-17 17:09:05 +01:00 committed by Wireshark GitLab Utility
parent e63f8d8daa
commit 2e048df011
4 changed files with 18 additions and 8 deletions

View File

@ -87,7 +87,7 @@ stnode_t *
dfilter_new_function(dfwork_t *dfw, const char *name);
stnode_t *
dfilter_new_regex(dfwork_t *dfw, const char *patt);
dfilter_new_regex(dfwork_t *dfw, stnode_t *node);
gboolean
dfilter_str_to_gint32(dfwork_t *dfw, const char *s, gint32* pint);

View File

@ -80,11 +80,21 @@ dfilter_new_function(dfwork_t *dfw, const char *name)
/* Gets a GRegex from a string, and sets the error message on failure. */
stnode_t *
dfilter_new_regex(dfwork_t *dfw, const char *patt)
dfilter_new_regex(dfwork_t *dfw, stnode_t *node)
{
GError *regex_error = NULL;
GRegex *pcre;
const char *patt;
if (stnode_type_id(node) == STTYPE_STRING) {
patt = stnode_data(node);
}
else {
dfilter_parse_fail(dfw, "Expected a string not %s", stnode_todisplay(node));
return node;
}
patt = stnode_data(node);
ws_debug("Compile regex pattern: %s", patt);
/*
@ -108,10 +118,11 @@ dfilter_new_regex(dfwork_t *dfw, const char *patt)
if (regex_error) {
dfilter_parse_fail(dfw, "%s", regex_error->message);
g_error_free(regex_error);
pcre = NULL;
return node;
}
return stnode_new(STTYPE_PCRE, pcre, patt);
stnode_replace(node, STTYPE_PCRE, pcre);
return node;
}
gboolean

View File

@ -302,10 +302,9 @@ relation_test(T) ::= entity(E) rel_binop(O) relation_test(R).
}
/* "matches" does not chain with other relational tests. */
relation_test(T) ::= entity(E) TEST_MATCHES STRING(S).
relation_test(T) ::= entity(E) TEST_MATCHES entity(F).
{
stnode_t *R = dfilter_new_regex(dfw, stnode_token_value(S));
stnode_free(S);
stnode_t *R = dfilter_new_regex(dfw, F);
T = stnode_new(STTYPE_TEST, NULL, NULL);
sttype_test_set2(T, TEST_OP_MATCHES, E, R);

View File

@ -37,7 +37,7 @@ class case_syntax(unittest.TestCase):
def test_matches_2(self, checkDFilterFail):
dfilter = 'http.request.method matches HEAD'
checkDFilterFail(dfilter, '"HEAD" was unexpected in this context')
checkDFilterFail(dfilter, 'Expected a string')
def test_matches_3(self, checkDFilterFail):
dfilter = 'http.request.method matches "^HEAD" matches "^POST"'