]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
qa/coccinelle: flag check for setter and getter
authorEric Leblond <eric@regit.org>
Sat, 30 Nov 2019 17:24:06 +0000 (18:24 +0100)
committerVictor Julien <victor@inliniac.net>
Thu, 5 Dec 2019 12:40:02 +0000 (13:40 +0100)
WHen adding something like
/* coccinelle: AppLayerParserStateIssetFlag():4,2:APP_LAYER_PARSER_ */
the coccinelle check will consider that AppLayerParserStateIssetFlag
is taking 4 parameters and that the second one is a flag that needs
to be checked against APP_LAYER_PARSER_.

qa/coccinelle/struct-flags.py

index 93377bd9bb0b89ad0a04590236593d1ff83511e5..38c40b33e4906d8343f98792f1f234ebb74351b5 100755 (executable)
@@ -1,6 +1,7 @@
 #!/usr/bin/env python
 import re
 from os import listdir
+from string import Template
 
 SRC_DIR = "../../src/"
 
@@ -81,3 +82,36 @@ p1 << flags.p1;
 print "Invalid usage of flags field at %s:%s, flags value is incorrect (wrong family)." % (p1[0].file, p1[0].line)
 import sys
 sys.exit(1)""")
+
+i = 1
+setter_template = """
+@settergetter${i}@
+identifier SetterGetter =~ "${function}";
+identifier f_flags =~ "^(?!${value}).+";
+identifier ${params_line};
+position p1;
+@@
+
+SetterGetter@p1(${prefix_param}f_flags${suffix_param})
+
+@script:python@
+p1 << settergetter${i}.p1;
+@@
+print "Invalid usage of ${function} at %s:%s, flags value is incorrect (wrong family)." % (p1[0].file, p1[0].line)
+import sys
+sys.exit(1)
+"""
+
+for sg in setter_getter_list:
+    prefix_param = ""
+    for index in list(range(1, sg.params[1])):
+        prefix_param += "param%d, " % (index)
+    if sg.params[1] < sg.params[0]:
+        suffix_param = ", " + ", ".join(["param%d" % (index + 1) for index in list(range(sg.params[1], sg.params[0]))])
+    else:
+        suffix_param = ""
+    params_elts = list(range(1, sg.params[1])) + list(range(sg.params[1] + 1, sg.params[0] + 1))
+    params_line = ", ".join(["param%d" % (x) for x in params_elts])
+    print(Template(setter_template).substitute(i=i, function=sg.function, value=sg.value,
+                prefix_param=prefix_param, suffix_param=suffix_param, params_line=params_line))
+    i += 1