xref: /OK3568_Linux_fs/kernel/scripts/coccinelle/misc/badty.cocci (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun// SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun/// Correct the size argument to alloc functions
3*4882a593Smuzhiyun///
4*4882a593Smuzhiyun//# This makes an effort to find cases where the argument to sizeof is wrong
5*4882a593Smuzhiyun//# in memory allocation functions by checking the type of the allocated memory
6*4882a593Smuzhiyun//# when it is a double pointer and ensuring the sizeof argument takes a pointer
7*4882a593Smuzhiyun//# to the the memory being allocated. There are false positives in cases the
8*4882a593Smuzhiyun//# sizeof argument is not used in constructing the return value. The result
9*4882a593Smuzhiyun//# may need some reformatting.
10*4882a593Smuzhiyun//
11*4882a593Smuzhiyun// Confidence: Moderate
12*4882a593Smuzhiyun// Copyright: (C) 2014 Himangi Saraogi.
13*4882a593Smuzhiyun// Comments:
14*4882a593Smuzhiyun// Options:
15*4882a593Smuzhiyun
16*4882a593Smuzhiyunvirtual patch
17*4882a593Smuzhiyunvirtual context
18*4882a593Smuzhiyunvirtual org
19*4882a593Smuzhiyunvirtual report
20*4882a593Smuzhiyun
21*4882a593Smuzhiyun//----------------------------------------------------------
22*4882a593Smuzhiyun//  For context mode
23*4882a593Smuzhiyun//----------------------------------------------------------
24*4882a593Smuzhiyun
25*4882a593Smuzhiyun@depends on context disable sizeof_type_expr@
26*4882a593Smuzhiyuntype T;
27*4882a593SmuzhiyunT **x;
28*4882a593Smuzhiyun@@
29*4882a593Smuzhiyun
30*4882a593Smuzhiyun  x =
31*4882a593Smuzhiyun  <+...sizeof(
32*4882a593Smuzhiyun* T
33*4882a593Smuzhiyun  )...+>
34*4882a593Smuzhiyun
35*4882a593Smuzhiyun//----------------------------------------------------------
36*4882a593Smuzhiyun//  For patch mode
37*4882a593Smuzhiyun//----------------------------------------------------------
38*4882a593Smuzhiyun
39*4882a593Smuzhiyun@depends on patch disable sizeof_type_expr@
40*4882a593Smuzhiyuntype T;
41*4882a593SmuzhiyunT **x;
42*4882a593Smuzhiyun@@
43*4882a593Smuzhiyun
44*4882a593Smuzhiyun  x =
45*4882a593Smuzhiyun  <+...sizeof(
46*4882a593Smuzhiyun- T
47*4882a593Smuzhiyun+ *x
48*4882a593Smuzhiyun  )...+>
49*4882a593Smuzhiyun
50*4882a593Smuzhiyun//----------------------------------------------------------
51*4882a593Smuzhiyun//  For org and report mode
52*4882a593Smuzhiyun//----------------------------------------------------------
53*4882a593Smuzhiyun
54*4882a593Smuzhiyun@r depends on (org || report) disable sizeof_type_expr@
55*4882a593Smuzhiyuntype T;
56*4882a593SmuzhiyunT **x;
57*4882a593Smuzhiyunposition p;
58*4882a593Smuzhiyun@@
59*4882a593Smuzhiyun
60*4882a593Smuzhiyun  x =
61*4882a593Smuzhiyun  <+...sizeof(
62*4882a593Smuzhiyun  T@p
63*4882a593Smuzhiyun  )...+>
64*4882a593Smuzhiyun
65*4882a593Smuzhiyun@script:python depends on org@
66*4882a593Smuzhiyunp << r.p;
67*4882a593Smuzhiyun@@
68*4882a593Smuzhiyun
69*4882a593Smuzhiyuncoccilib.org.print_todo(p[0], "WARNING sizeof argument should be pointer type, not structure type")
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun@script:python depends on report@
72*4882a593Smuzhiyunp << r.p;
73*4882a593Smuzhiyun@@
74*4882a593Smuzhiyun
75*4882a593Smuzhiyunmsg="WARNING: Use correct pointer type argument for sizeof"
76*4882a593Smuzhiyuncoccilib.report.print_report(p[0], msg)
77*4882a593Smuzhiyun
78