[PATCH] Add coccinelle script that makes sure that tables are NULL terminated

From: Daniel Granat
Date: Fri Feb 27 2015 - 08:22:14 EST


Signed-off-by: Daniel Granat <d.granat@xxxxxxxxxxx>
---
scripts/coccinelle/misc/device_id_tables.cocci | 95 ++++++++++++++++++++++++++
1 file changed, 95 insertions(+)
create mode 100644 scripts/coccinelle/misc/device_id_tables.cocci

diff --git a/scripts/coccinelle/misc/device_id_tables.cocci b/scripts/coccinelle/misc/device_id_tables.cocci
new file mode 100644
index 0000000..5968984
--- /dev/null
+++ b/scripts/coccinelle/misc/device_id_tables.cocci
@@ -0,0 +1,95 @@
+/// Make sure '*_device_id$' tables are NULL terminated
+//
+// Keywords: device_id
+// Confidence: Medium
+// Options: --include-headers
+
+virtual org
+virtual report
+virtual patch
+
+@initialize:python@
+@@
+import re
+
+postfix = '_device_id$'
+prefix_list = ['platform', 'of', 'i2c']
+
+@r1 depends on patch || org || report@
+position p1;
+identifier var, arr;
+identifier struct_name;
+expression E;
+@@
+
+(
+struct struct_name arr[] = {
+ ...,
+ {
+ .var = E,
+ }
+ @p1
+};
+|
+struct struct_name arr[] = {
+ ...,
+ { ..., var, ... },
+ @p1
+};
+)
+
+@script:python depends on report@
+struct_name << r1.struct_name;
+p1 << r1.p1;
+arr << r1.arr;
+pattern;
+@@
+
+for i in prefix_list:
+ pattern = str(i)+postfix
+ if re.match(pattern, struct_name) != None:
+ print "\nCOCCI: \"%s\" matchs required pattern \"%s\"" % (struct_name, pattern)
+ msg = "\"%s\" is not NULL terminated at line %s" % (arr, p1[0].line)
+ coccilib.report.print_report(p1[0],msg)
+ break
+
+@script:python match depends on patch@
+struct_name << r1.struct_name;
+matched_name;
+pattern;
+@@
+
+coccinelle.matched_name = ''
+
+for i in prefix_list:
+ pattern = str(i)+postfix
+ if re.match(pattern, struct_name) != None:
+ coccinelle.matched_name = struct_name
+ break
+
+@r2 depends on patch@
+position r1.p1;
+identifier var, arr;
+identifier match.matched_name;
+expression E;
+@@
+
+(
+struct matched_name arr[] = {
+ ...,
+ {
+ .var = E,
+- }
+ @p1
++ },
++ {},
+};
+|
+struct matched_name arr[] = {
+ ...,
+ { ..., var, ... },
+ @p1
++ {},
+};
+)
+
--
1.9.1

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/