18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/// This semantic patch looks for kmalloc etc that are not followed by a
38c2ecf20Sopenharmony_ci/// NULL check.  It only gives a report in the case where there is some
48c2ecf20Sopenharmony_ci/// error handling code later in the function, which may be helpful
58c2ecf20Sopenharmony_ci/// in determining what the error handling code for the call to kmalloc etc
68c2ecf20Sopenharmony_ci/// should be.
78c2ecf20Sopenharmony_ci///
88c2ecf20Sopenharmony_ci// Confidence: High
98c2ecf20Sopenharmony_ci// Copyright: (C) 2010 Nicolas Palix, DIKU.
108c2ecf20Sopenharmony_ci// Copyright: (C) 2010 Julia Lawall, DIKU.
118c2ecf20Sopenharmony_ci// Copyright: (C) 2010 Gilles Muller, INRIA/LiP6.
128c2ecf20Sopenharmony_ci// URL: http://coccinelle.lip6.fr/
138c2ecf20Sopenharmony_ci// Comments:
148c2ecf20Sopenharmony_ci// Options: --no-includes --include-headers
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_civirtual context
178c2ecf20Sopenharmony_civirtual org
188c2ecf20Sopenharmony_civirtual report
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci@withtest@
218c2ecf20Sopenharmony_ciexpression x;
228c2ecf20Sopenharmony_ciposition p;
238c2ecf20Sopenharmony_ciidentifier f,fld;
248c2ecf20Sopenharmony_ci@@
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_cix@p = f(...);
278c2ecf20Sopenharmony_ci... when != x->fld
288c2ecf20Sopenharmony_ci\(x == NULL \| x != NULL\)
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci@fixed depends on context && !org && !report@
318c2ecf20Sopenharmony_ciexpression x,x1;
328c2ecf20Sopenharmony_ciposition p1 != withtest.p;
338c2ecf20Sopenharmony_cistatement S;
348c2ecf20Sopenharmony_ciposition any withtest.p;
358c2ecf20Sopenharmony_ciidentifier f;
368c2ecf20Sopenharmony_ci@@
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci*x@p1 = \(kmalloc\|kzalloc\|kcalloc\)(...);
398c2ecf20Sopenharmony_ci...
408c2ecf20Sopenharmony_ci*x1@p = f(...);
418c2ecf20Sopenharmony_ciif (!x1) S
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci// ------------------------------------------------------------------------
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci@rfixed depends on (org || report) && !context exists@
468c2ecf20Sopenharmony_ciexpression x,x1;
478c2ecf20Sopenharmony_ciposition p1 != withtest.p;
488c2ecf20Sopenharmony_ciposition p2;
498c2ecf20Sopenharmony_cistatement S;
508c2ecf20Sopenharmony_ciposition any withtest.p;
518c2ecf20Sopenharmony_ciidentifier f;
528c2ecf20Sopenharmony_ci@@
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_cix@p1 = \(kmalloc\|kzalloc\|kcalloc\)(...);
558c2ecf20Sopenharmony_ci...
568c2ecf20Sopenharmony_cix1@p = f@p2(...);
578c2ecf20Sopenharmony_ciif (!x1) S
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci@script:python depends on org@
608c2ecf20Sopenharmony_cip1 << rfixed.p1;
618c2ecf20Sopenharmony_cip2 << rfixed.p2;
628c2ecf20Sopenharmony_ci@@
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_cicocci.print_main("alloc call",p1)
658c2ecf20Sopenharmony_cicocci.print_secs("possible model",p2)
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci@script:python depends on report@
688c2ecf20Sopenharmony_cip1 << rfixed.p1;
698c2ecf20Sopenharmony_cip2 << rfixed.p2;
708c2ecf20Sopenharmony_ci@@
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_cimsg = "alloc with no test, possible model on line %s" % (p2[0].line)
738c2ecf20Sopenharmony_cicoccilib.report.print_report(p1[0],msg)
74