]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
qa/coccinelle: fix false positive in setter getter
authorEric Leblond <eric@regit.org>
Sat, 30 Nov 2019 16:20:44 +0000 (17:20 +0100)
committerVictor Julien <victor@inliniac.net>
Thu, 5 Dec 2019 12:40:02 +0000 (13:40 +0100)
Coccinelle test was doing a false positive on the function
AppLayerParserStateSetFlag and AppLayerParserStateIssetFlag.
To address that, this patch adds a new coccinelle markup:

 /* coccinelle: AppLayerParserStateSetFlag():2,2:APP_LAYER_PARSER_ */

It indicates that AppLayerParserStateSetFlag is a setter and getter
and that the checks should be disabled inside the function.

Currently this markup is only used for that but following patch will
add some checks on option value.

qa/coccinelle/struct-flags.py
src/app-layer-parser.c

index 81fa09aa84709e699a3cc30b689bdc5df8ce6d1b..93377bd9bb0b89ad0a04590236593d1ff83511e5 100755 (executable)
@@ -9,10 +9,17 @@ class Structure:
     def __init__(self, string):
         (self.struct, self.flags, self.values) = string.split(":")
 
+class SetterGetter:
+    def __init__(self, string):
+        (function, params, self.value) = string.split(":")
+        self.function = function.strip("()")
+        self.params = [int(a) for a in params.split(",")]
+
 
 cmd = "grep -h coccinelle ../../src/*[ch] | sed -e 's/.*coccinelle: \(.*\) \*\//\1/'"
 
 struct_list = []
+setter_getter_list = []
 
 dirList = listdir(SRC_DIR)
 for fname in dirList:
@@ -20,12 +27,21 @@ for fname in dirList:
         for line in open(SRC_DIR + fname):
             if "coccinelle:" in line:
                 m = re.search("coccinelle: (.*) \*\/", line)
-                struct = Structure(m.group(1))
-                struct_list.append(struct)
+                if "()" not in m.group(1):
+                    struct = Structure(m.group(1))
+                    struct_list.append(struct)
+                else:
+                    function = SetterGetter(m.group(1))
+                    setter_getter_list.append(function)
 
 header = "@flags@"
 body = []
 
+# Handle setter and getter
+setter_getter = [x.function for x in setter_getter_list]
+if len(setter_getter):
+    header += "\nidentifier NotSetterGetter !~ \"^(%s)$\";" % ("|".join(setter_getter))
+
 i = 0
 for struct in struct_list:
     header += """
@@ -46,9 +62,19 @@ print(header)
 print("position p1;")
 print("@@")
 print("")
+print("""
+NotSetterGetter(...)
+{
+    <...
+""")
+print("")
 print("(" + "|".join(body) + ")")
 print("")
-print("""@script:python@
+print("""
+...>
+}
+
+@script:python@
 p1 << flags.p1;
 @@
 
index 03f23dd1c7cba991d9a9e8d14e27c8e4ccc24d2e..b7de5c0f40399c815fcfb574e4b63d68cdc4c1d7 100644 (file)
@@ -1575,6 +1575,7 @@ void AppLayerParserRegisterProtocolParsers(void)
 }
 
 
+/* coccinelle: AppLayerParserStateSetFlag():2,2:APP_LAYER_PARSER_ */
 void AppLayerParserStateSetFlag(AppLayerParserState *pstate, uint8_t flag)
 {
     SCEnter();
@@ -1582,6 +1583,7 @@ void AppLayerParserStateSetFlag(AppLayerParserState *pstate, uint8_t flag)
     SCReturn;
 }
 
+/* coccinelle: AppLayerParserStateIssetFlag():2,2:APP_LAYER_PARSER_ */
 int AppLayerParserStateIssetFlag(AppLayerParserState *pstate, uint8_t flag)
 {
     SCEnter();