diff --git a/qa/coccinelle/malloc-error-check.cocci b/qa/coccinelle/malloc-error-check.cocci new file mode 100644 index 0000000000..93bc6c6a01 --- /dev/null +++ b/qa/coccinelle/malloc-error-check.cocci @@ -0,0 +1,44 @@ +@malloced@ +identifier x; +position p1; +identifier func =~ "(SCMalloc|SCStrdup|SCCalloc|SCMallocAligned|SCRealloc)"; +@@ + +x@p1 = func(...) + + +@inlinetested@ +identifier x; +position p1; +statement S; +identifier func =~ "(SCMalloc|SCStrdup|SCCalloc|SCMallocAligned|SCRealloc)"; +@@ + +if ((x@p1 = func(...)) == NULL) S + +@istested@ +identifier x; +position malloced.p1; +statement S1, S2; +identifier func =~ "(SCMalloc|SCStrdup|SCCalloc|SCMallocAligned|SCRealloc)"; +@@ + +x@p1 = func(...) +... when != x +( +if (unlikely(x == NULL)) S1 +| +if (x == NULL) S1 +| +if (x != NULL) S1 else S2 +| +BUG_ON(x == NULL) +) + +@script:python depends on malloced && !istested && !inlinetested @ +p1 << malloced.p1; +@@ + +print "Structure malloced at %s:%s but error is not checked." % (p1[0].file, p1[0].line) +import sys +sys.exit(1)