[PATCH] ptp: chardev: Fix confusing cleanup.h syntax

Krzysztof Kozlowski posted 1 patch 1 week, 4 days ago
drivers/ptp/ptp_chardev.c | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
[PATCH] ptp: chardev: Fix confusing cleanup.h syntax
Posted by Krzysztof Kozlowski 1 week, 4 days ago
Initializing automatic __free variables to NULL without need (e.g.
branches with different allocations), followed by actual allocation is
in contrary to explicit coding rules guiding cleanup.h:

"Given that the "__free(...) = NULL" pattern for variables defined at
the top of the function poses this potential interdependency problem the
recommendation is to always define and assign variables in one statement
and not group variable definitions at the top of the function when
__free() is used."

Code does not have a bug, but is less readable and uses discouraged
coding practice, so fix that by moving declaration to the place of
assignment.

Signed-off-by: Krzysztof Kozlowski <krzysztof.kozlowski@oss.qualcomm.com>
---
 drivers/ptp/ptp_chardev.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/drivers/ptp/ptp_chardev.c b/drivers/ptp/ptp_chardev.c
index c61cf9edac48..2a52cc7bccd1 100644
--- a/drivers/ptp/ptp_chardev.c
+++ b/drivers/ptp/ptp_chardev.c
@@ -350,13 +350,13 @@ typedef int (*ptp_gettimex_fn)(struct ptp_clock_info *,
 static long ptp_sys_offset_extended(struct ptp_clock *ptp, void __user *arg,
 				    ptp_gettimex_fn gettimex_fn)
 {
-	struct ptp_sys_offset_extended *extoff __free(kfree) = NULL;
 	struct ptp_system_timestamp sts;
 
 	if (!gettimex_fn)
 		return -EOPNOTSUPP;
 
-	extoff = memdup_user(arg, sizeof(*extoff));
+	struct ptp_sys_offset_extended *extoff __free(kfree) =
+		memdup_user(arg, sizeof(*extoff));
 	if (IS_ERR(extoff))
 		return PTR_ERR(extoff);
 
@@ -402,11 +402,11 @@ static long ptp_sys_offset_extended(struct ptp_clock *ptp, void __user *arg,
 
 static long ptp_sys_offset(struct ptp_clock *ptp, void __user *arg)
 {
-	struct ptp_sys_offset *sysoff __free(kfree) = NULL;
 	struct ptp_clock_time *pct;
 	struct timespec64 ts;
 
-	sysoff = memdup_user(arg, sizeof(*sysoff));
+	struct ptp_sys_offset *sysoff __free(kfree) =
+		memdup_user(arg, sizeof(*sysoff));
 	if (IS_ERR(sysoff))
 		return PTR_ERR(sysoff);
 
-- 
2.51.0
Re: [PATCH] ptp: chardev: Fix confusing cleanup.h syntax
Posted by Jakub Kicinski 1 week, 3 days ago
On Mon,  8 Dec 2025 03:08:20 +0100 Krzysztof Kozlowski wrote:
> Code does not have a bug, but is less readable and uses discouraged
> coding practice, so fix that by moving declaration to the place of
> assignment.

I disagree, you're making this code look much worse.
Also, kfree() handles error pointers now?
-- 
pw-bot: reject
Re: [PATCH] ptp: chardev: Fix confusing cleanup.h syntax
Posted by Simon Horman 1 week, 3 days ago
On Mon, Dec 08, 2025 at 03:08:20AM +0100, Krzysztof Kozlowski wrote:
> Initializing automatic __free variables to NULL without need (e.g.
> branches with different allocations), followed by actual allocation is
> in contrary to explicit coding rules guiding cleanup.h:
> 
> "Given that the "__free(...) = NULL" pattern for variables defined at
> the top of the function poses this potential interdependency problem the
> recommendation is to always define and assign variables in one statement
> and not group variable definitions at the top of the function when
> __free() is used."
> 
> Code does not have a bug, but is less readable and uses discouraged
> coding practice, so fix that by moving declaration to the place of
> assignment.
> 
> Signed-off-by: Krzysztof Kozlowski <krzysztof.kozlowski@oss.qualcomm.com>

Reviewed-by: Simon Horman <horms@kernel.org>