| 1 | // SPDX-License-Identifier: GPL-2.0-only |
| 2 | /// |
| 3 | /// Check for code that could use struct_size(). |
| 4 | /// |
| 5 | // Confidence: Medium |
| 6 | // Author: Jacob Keller <[email protected]> |
| 7 | // Copyright: (C) 2023 Intel Corporation |
| 8 | // Options: --no-includes --include-headers |
| 9 | |
| 10 | virtual patch |
| 11 | virtual context |
| 12 | virtual org |
| 13 | virtual report |
| 14 | |
| 15 | // the overflow Kunit tests have some code which intentionally does not use |
| 16 | // the macros, so we want to ignore this code when reporting potential |
| 17 | // issues. |
| 18 | @overflow_tests@ |
| 19 | identifier f = overflow_size_helpers_test; |
| 20 | @@ |
| 21 | |
| 22 | f |
| 23 | |
| 24 | //---------------------------------------------------------- |
| 25 | // For context mode |
| 26 | //---------------------------------------------------------- |
| 27 | |
| 28 | @depends on !overflow_tests && context@ |
| 29 | expression E1, E2; |
| 30 | identifier m; |
| 31 | @@ |
| 32 | * sizeof(*E1) + (E2 * sizeof(*E1->m)) |
| 33 | |
| 34 | //---------------------------------------------------------- |
| 35 | // For patch mode |
| 36 | //---------------------------------------------------------- |
| 37 | |
| 38 | @depends on !overflow_tests && patch@ |
| 39 | expression E1, E2; |
| 40 | identifier m; |
| 41 | @@ |
| 42 | ( |
| 43 | - (sizeof(*E1) + (E2 * sizeof(*E1->m))) |
| 44 | + struct_size(E1, m, E2) |
| 45 | ) |
| 46 | |
| 47 | //---------------------------------------------------------- |
| 48 | // For org and report mode |
| 49 | //---------------------------------------------------------- |
| 50 | |
| 51 | @r depends on !overflow_tests && (org || report)@ |
| 52 | expression E1, E2; |
| 53 | identifier m; |
| 54 | position p; |
| 55 | @@ |
| 56 | sizeof(*E1)@p + (E2 * sizeof(*E1->m)) |
| 57 | |
| 58 | @script:python depends on org@ |
| 59 | p << r.p; |
| 60 | @@ |
| 61 | |
| 62 | coccilib.org.print_todo(p[0], "WARNING should use struct_size") |
| 63 | |
| 64 | @script:python depends on report@ |
| 65 | p << r.p; |
| 66 | @@ |
| 67 | |
| 68 | msg="WARNING: Use struct_size" |
| 69 | coccilib.report.print_report(p[0], msg) |
| 70 | |