aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoão Valverde <j@v6e.pt>2021-10-17 17:09:05 +0100
committerWireshark GitLab Utility <gerald+gitlab-utility@wireshark.org>2021-10-18 12:09:36 +0000
commit2e048df011a56eaf2a5b3d86b76a8b5675f9e113 (patch)
treeab9ab8d583c8f610e0559d8d9a656494d21ed80f
parente63f8d8daa7109123037ec8d86cae38f4c7260a4 (diff)
dfilter: Improve error message for "matches"
Should be more obvious that this error is caused by a string syntax error and not something else.
-rw-r--r--epan/dfilter/dfilter-int.h2
-rw-r--r--epan/dfilter/dfilter.c17
-rw-r--r--epan/dfilter/grammar.lemon5
-rw-r--r--test/suite_dfilter/group_syntax.py2
4 files changed, 18 insertions, 8 deletions
diff --git a/epan/dfilter/dfilter-int.h b/epan/dfilter/dfilter-int.h
index 44d1a448ae..c6cc1812c4 100644
--- a/epan/dfilter/dfilter-int.h
+++ b/epan/dfilter/dfilter-int.h
@@ -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);
diff --git a/epan/dfilter/dfilter.c b/epan/dfilter/dfilter.c
index 9dd34f1163..263b5c192c 100644
--- a/epan/dfilter/dfilter.c
+++ b/epan/dfilter/dfilter.c
@@ -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
diff --git a/epan/dfilter/grammar.lemon b/epan/dfilter/grammar.lemon
index b48d76da9c..97e85307a5 100644
--- a/epan/dfilter/grammar.lemon
+++ b/epan/dfilter/grammar.lemon
@@ -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);
diff --git a/test/suite_dfilter/group_syntax.py b/test/suite_dfilter/group_syntax.py
index 23b39052ac..42a7099e68 100644
--- a/test/suite_dfilter/group_syntax.py
+++ b/test/suite_dfilter/group_syntax.py
@@ -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"'