[PATCH v2 2/2] add check for pointers with __free attribute initialized to NULL

From: Ally Heev
Date: Fri Oct 24 2025 - 13:30:10 EST


pointers with __free attribute initialized to NULL
pose potential cleanup issues [1] when a function uses
interdependent variables with cleanup attributes

Link: https://docs.kernel.org/core-api/cleanup.html [1]
Link: https://lore.kernel.org/all/68f7b830ec21a_10e910070@dwillia2-mobl4.notmuch/
Suggested-by: Dan Williams <dan.j.williams@xxxxxxxxx>
Signed-off-by: Ally Heev <allyheev@xxxxxxxxx>
---
Documentation/dev-tools/checkpatch.rst | 6 ++++++
scripts/checkpatch.pl | 6 ++++++
2 files changed, 12 insertions(+)

diff --git a/Documentation/dev-tools/checkpatch.rst b/Documentation/dev-tools/checkpatch.rst
index 1a304bf38bcd27e50bbb7cd4383b07ac54d20b0a..c39213b814f487290d2b0e5d320a4313ada9bbad 100644
--- a/Documentation/dev-tools/checkpatch.rst
+++ b/Documentation/dev-tools/checkpatch.rst
@@ -1015,6 +1015,12 @@ Functions and Variables
in case not initialized) to the pointer is freed automatically
when the pointer goes out of scope.

+ **NULL_INITIALIZED_PTR_WITH_FREE**
+ Pointers with __free attribute should not be initialized to NULL.
+ Always define and assign such pointers in one statement.
+
+ See: https://docs.kernel.org/core-api/cleanup.html
+
Permissions
-----------

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 1009a4a065e910143dabeee6640b3b3a4bd3fe06..cf186dafc191f1c39d01b3660f19101f6cc61a82 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -7728,6 +7728,12 @@ sub process {
ERROR("UNINITIALIZED_PTR_WITH_FREE",
"pointer '$1' with __free attribute should be initialized\n" . $herecurr);
}
+
+# check for pointers with __free attribute initialized to NULL
+ while ($line =~ /\*\s*($Ident)\s+$FreeAttribute\s*=\s*NULL\b/g) {
+ WARN("NULL_INITIALIZED_PTR_WITH_FREE",
+ "pointer '$1' with __free attribute should be initialized to a non-NULL address\n" . $herecurr);
+ }
}

# If we have no input at all, then there is nothing to report on

--
2.47.3