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