Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

scripts/coccinelle: Find PTR_ERR() to %pe candidates

Add a new Coccinelle script to identify places where PTR_ERR() is used
in print functions and suggest using the %pe format specifier instead.

For printing error pointers (i.e., a pointer for which IS_ERR() is true)
%pe will print a symbolic error name (e.g,. -EINVAL), opposed to the raw
errno (e.g,. -22) produced by PTR_ERR().
It also makes the code cleaner by saving a redundant call to PTR_ERR().

The script supports context, report, and org modes.

Example transformation:
printk("Error: %ld\n", PTR_ERR(ptr)); // Before
printk("Error: %pe\n", ptr); // After

Signed-off-by: Gal Pressman <gal@nvidia.com>
Reviewed-by: Alexei Lazar <alazar@nvidia.com>
Signed-off-by: Tariq Toukan <tariqt@nvidia.com>
Reviewed-by: Simon Horman <horms@kernel.org>
Link: https://patch.msgid.link/1758192227-701925-2-git-send-email-tariqt@nvidia.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>

authored by

Gal Pressman and committed by
Jakub Kicinski
57c49d23 203e3beb

+34
+34
scripts/coccinelle/misc/ptr_err_to_pe.cocci
··· 1 + // SPDX-License-Identifier: GPL-2.0-only 2 + /// Use %pe format specifier instead of PTR_ERR() for printing error pointers. 3 + /// 4 + /// For printing error pointers (i.e., a pointer for which IS_ERR() is true) 5 + /// %pe will print a symbolic error name (e.g., -EINVAL), opposed to the raw 6 + /// errno (e.g., -22) produced by PTR_ERR(). 7 + /// It also makes the code cleaner by saving a redundant call to PTR_ERR(). 8 + /// 9 + // Confidence: High 10 + // Copyright: (C) 2025 NVIDIA CORPORATION & AFFILIATES. 11 + // URL: https://coccinelle.gitlabpages.inria.fr/website 12 + // Options: --no-includes --include-headers 13 + 14 + virtual context 15 + virtual org 16 + virtual report 17 + 18 + @r@ 19 + expression ptr; 20 + constant fmt; 21 + position p; 22 + identifier print_func; 23 + @@ 24 + * print_func(..., fmt, ..., PTR_ERR@p(ptr), ...) 25 + 26 + @script:python depends on r && report@ 27 + p << r.p; 28 + @@ 29 + coccilib.report.print_report(p[0], "WARNING: Consider using %pe to print PTR_ERR()") 30 + 31 + @script:python depends on r && org@ 32 + p << r.p; 33 + @@ 34 + coccilib.org.print_todo(p[0], "WARNING: Consider using %pe to print PTR_ERR()")