0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /// Correct the size argument to alloc functions
0003 ///
0004 //# This makes an effort to find cases where the argument to sizeof is wrong
0005 //# in memory allocation functions by checking the type of the allocated memory
0006 //# when it is a double pointer and ensuring the sizeof argument takes a pointer
0007 //# to the the memory being allocated. There are false positives in cases the
0008 //# sizeof argument is not used in constructing the return value. The result
0009 //# may need some reformatting.
0010 //
0011 // Confidence: Moderate
0012 // Copyright: (C) 2014 Himangi Saraogi.
0013 // Comments:
0014 // Options:
0015
0016 virtual patch
0017 virtual context
0018 virtual org
0019 virtual report
0020
0021 //----------------------------------------------------------
0022 // For context mode
0023 //----------------------------------------------------------
0024
0025 @depends on context disable sizeof_type_expr@
0026 type T;
0027 T **x;
0028 @@
0029
0030 x =
0031 <+...sizeof(
0032 * T
0033 )...+>
0034
0035 //----------------------------------------------------------
0036 // For patch mode
0037 //----------------------------------------------------------
0038
0039 @depends on patch disable sizeof_type_expr@
0040 type T;
0041 T **x;
0042 @@
0043
0044 x =
0045 <+...sizeof(
0046 - T
0047 + *x
0048 )...+>
0049
0050 //----------------------------------------------------------
0051 // For org and report mode
0052 //----------------------------------------------------------
0053
0054 @r depends on (org || report) disable sizeof_type_expr@
0055 type T;
0056 T **x;
0057 position p;
0058 @@
0059
0060 x =
0061 <+...sizeof(
0062 T@p
0063 )...+>
0064
0065 @script:python depends on org@
0066 p << r.p;
0067 @@
0068
0069 coccilib.org.print_todo(p[0], "WARNING sizeof argument should be pointer type, not structure type")
0070
0071 @script:python depends on report@
0072 p << r.p;
0073 @@
0074
0075 msg="WARNING: Use correct pointer type argument for sizeof"
0076 coccilib.report.print_report(p[0], msg)
0077