17f904d7eSThomas Gleixner// SPDX-License-Identifier: GPL-2.0-only 22e1e1605SNicolas Palix/// This semantic patch looks for kmalloc etc that are not followed by a 32e1e1605SNicolas Palix/// NULL check. It only gives a report in the case where there is some 42e1e1605SNicolas Palix/// error handling code later in the function, which may be helpful 52e1e1605SNicolas Palix/// in determining what the error handling code for the call to kmalloc etc 62e1e1605SNicolas Palix/// should be. 72e1e1605SNicolas Palix/// 82e1e1605SNicolas Palix// Confidence: High 97f904d7eSThomas Gleixner// Copyright: (C) 2010 Nicolas Palix, DIKU. 107f904d7eSThomas Gleixner// Copyright: (C) 2010 Julia Lawall, DIKU. 117f904d7eSThomas Gleixner// Copyright: (C) 2010 Gilles Muller, INRIA/LiP6. 12*f01701ceSJulia Lawall// URL: https://coccinelle.gitlabpages.inria.fr/website 132e1e1605SNicolas Palix// Comments: 1493f14468SNicolas Palix// Options: --no-includes --include-headers 152e1e1605SNicolas Palix 162e1e1605SNicolas Palixvirtual context 172e1e1605SNicolas Palixvirtual org 182e1e1605SNicolas Palixvirtual report 192e1e1605SNicolas Palix 202e1e1605SNicolas Palix@withtest@ 212e1e1605SNicolas Palixexpression x; 222e1e1605SNicolas Palixposition p; 232e1e1605SNicolas Palixidentifier f,fld; 242e1e1605SNicolas Palix@@ 252e1e1605SNicolas Palix 262e1e1605SNicolas Palixx@p = f(...); 272e1e1605SNicolas Palix... when != x->fld 282e1e1605SNicolas Palix\(x == NULL \| x != NULL\) 292e1e1605SNicolas Palix 302e1e1605SNicolas Palix@fixed depends on context && !org && !report@ 312e1e1605SNicolas Palixexpression x,x1; 322e1e1605SNicolas Palixposition p1 != withtest.p; 332e1e1605SNicolas Palixstatement S; 342e1e1605SNicolas Palixposition any withtest.p; 352e1e1605SNicolas Palixidentifier f; 362e1e1605SNicolas Palix@@ 372e1e1605SNicolas Palix 382e1e1605SNicolas Palix*x@p1 = \(kmalloc\|kzalloc\|kcalloc\)(...); 392e1e1605SNicolas Palix... 402e1e1605SNicolas Palix*x1@p = f(...); 412e1e1605SNicolas Palixif (!x1) S 422e1e1605SNicolas Palix 432e1e1605SNicolas Palix// ------------------------------------------------------------------------ 442e1e1605SNicolas Palix 452e1e1605SNicolas Palix@rfixed depends on (org || report) && !context exists@ 462e1e1605SNicolas Palixexpression x,x1; 472e1e1605SNicolas Palixposition p1 != withtest.p; 482e1e1605SNicolas Palixposition p2; 492e1e1605SNicolas Palixstatement S; 502e1e1605SNicolas Palixposition any withtest.p; 512e1e1605SNicolas Palixidentifier f; 522e1e1605SNicolas Palix@@ 532e1e1605SNicolas Palix 542e1e1605SNicolas Palixx@p1 = \(kmalloc\|kzalloc\|kcalloc\)(...); 552e1e1605SNicolas Palix... 562e1e1605SNicolas Palixx1@p = f@p2(...); 572e1e1605SNicolas Palixif (!x1) S 582e1e1605SNicolas Palix 592e1e1605SNicolas Palix@script:python depends on org@ 602e1e1605SNicolas Palixp1 << rfixed.p1; 612e1e1605SNicolas Palixp2 << rfixed.p2; 622e1e1605SNicolas Palix@@ 632e1e1605SNicolas Palix 642e1e1605SNicolas Palixcocci.print_main("alloc call",p1) 652e1e1605SNicolas Palixcocci.print_secs("possible model",p2) 662e1e1605SNicolas Palix 672e1e1605SNicolas Palix@script:python depends on report@ 682e1e1605SNicolas Palixp1 << rfixed.p1; 692e1e1605SNicolas Palixp2 << rfixed.p2; 702e1e1605SNicolas Palix@@ 712e1e1605SNicolas Palix 722e1e1605SNicolas Palixmsg = "alloc with no test, possible model on line %s" % (p2[0].line) 732e1e1605SNicolas Palixcoccilib.report.print_report(p1[0],msg) 74