0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /// This semantic patch looks for kmalloc etc that are not followed by a
0003 /// NULL check. It only gives a report in the case where there is some
0004 /// error handling code later in the function, which may be helpful
0005 /// in determining what the error handling code for the call to kmalloc etc
0006 /// should be.
0007 ///
0008 // Confidence: High
0009 // Copyright: (C) 2010 Nicolas Palix, DIKU.
0010 // Copyright: (C) 2010 Julia Lawall, DIKU.
0011 // Copyright: (C) 2010 Gilles Muller, INRIA/LiP6.
0012 // URL: https://coccinelle.gitlabpages.inria.fr/website
0013 // Comments:
0014 // Options: --no-includes --include-headers
0015
0016 virtual context
0017 virtual org
0018 virtual report
0019
0020 @withtest@
0021 expression x;
0022 position p;
0023 identifier f,fld;
0024 @@
0025
0026 x@p = f(...);
0027 ... when != x->fld
0028 \(x == NULL \| x != NULL\)
0029
0030 @fixed depends on context && !org && !report@
0031 expression x,x1;
0032 position p1 != withtest.p;
0033 statement S;
0034 position any withtest.p;
0035 identifier f;
0036 @@
0037
0038 *x@p1 = \(kmalloc\|kzalloc\|kcalloc\)(...);
0039 ...
0040 *x1@p = f(...);
0041 if (!x1) S
0042
0043 // ------------------------------------------------------------------------
0044
0045 @rfixed depends on (org || report) && !context exists@
0046 expression x,x1;
0047 position p1 != withtest.p;
0048 position p2;
0049 statement S;
0050 position any withtest.p;
0051 identifier f;
0052 @@
0053
0054 x@p1 = \(kmalloc\|kzalloc\|kcalloc\)(...);
0055 ...
0056 x1@p = f@p2(...);
0057 if (!x1) S
0058
0059 @script:python depends on org@
0060 p1 << rfixed.p1;
0061 p2 << rfixed.p2;
0062 @@
0063
0064 cocci.print_main("alloc call",p1)
0065 cocci.print_secs("possible model",p2)
0066
0067 @script:python depends on report@
0068 p1 << rfixed.p1;
0069 p2 << rfixed.p2;
0070 @@
0071
0072 msg = "alloc with no test, possible model on line %s" % (p2[0].line)
0073 coccilib.report.print_report(p1[0],msg)