[v2] devtools: check %l format specifier

Message ID 20210519192449.2688051-1-thomas@monjalon.net (mailing list archive)
State Superseded, archived
Headers
Series [v2] devtools: check %l format specifier |

Checks

Context Check Description
ci/checkpatch success coding style OK
ci/Intel-compilation success Compilation OK
ci/iol-abi-testing success Testing PASS
ci/iol-testing success Testing PASS
ci/github-robot success github build: passed
ci/intel-Testing success Testing PASS
ci/iol-mellanox-Performance fail Performance Testing issues
ci/iol-intel-Functional success Functional Testing PASS
ci/iol-intel-Performance success Performance Testing PASS
ci/iol-mellanox-Functional fail Functional Testing issues

Commit Message

Thomas Monjalon May 19, 2021, 7:24 p.m. UTC
  From: Ferruh Yigit <ferruh.yigit@intel.com>

%lx or %llx tend to be wrong for 32-bit platform
if used for fixed size variable like uint64_t.
A checkpatch warning will avoid this common mistake.

Signed-off-by: Ferruh Yigit <ferruh.yigit@intel.com>
Signed-off-by: Thomas Monjalon <thomas@monjalon.net>
---
v2: proposal to reword the message and comment
---
 devtools/checkpatches.sh | 8 ++++++++
 1 file changed, 8 insertions(+)
  

Comments

Ferruh Yigit May 21, 2021, 12:01 p.m. UTC | #1
On 5/19/2021 8:24 PM, Thomas Monjalon wrote:
> From: Ferruh Yigit <ferruh.yigit@intel.com>
> 
> %lx or %llx tend to be wrong for 32-bit platform
> if used for fixed size variable like uint64_t.
> A checkpatch warning will avoid this common mistake.
> 
> Signed-off-by: Ferruh Yigit <ferruh.yigit@intel.com>
> Signed-off-by: Thomas Monjalon <thomas@monjalon.net>
> ---
> v2: proposal to reword the message and comment
> ---
>  devtools/checkpatches.sh | 8 ++++++++
>  1 file changed, 8 insertions(+)
> 
> diff --git a/devtools/checkpatches.sh b/devtools/checkpatches.sh
> index db4c7d8301..0e09b2cab8 100755
> --- a/devtools/checkpatches.sh
> +++ b/devtools/checkpatches.sh
> @@ -69,6 +69,14 @@ check_forbidden_additions() { # <patch>
>  		-f $(dirname $(readlink -f $0))/check-forbidden-tokens.awk \
>  		"$1" || res=1
>  
> +	# check %l or %ll format specifier
> +	awk -v FOLDERS='lib drivers app examples' \
> +		-v EXPRESSIONS='%ll*[xud]' \
> +		-v RET_ON_FAIL=1 \
> +		-v MESSAGE='Using %l format, should it be %PRI*64?' \
> +		-f $(dirname $(readlink -f $0))/check-forbidden-tokens.awk \
> +		"$1" || res=1
> +
>  	# forbid variable declaration inside "for" loop
>  	awk -v FOLDERS='.' \
>  		-v EXPRESSIONS='for[[:space:]]*\\((char|u?int|unsigned|s?size_t)' \
> 

Using the %l or %ll format specifier is correct when the variable type is "long
int" or "long long int", it is only wrong if the variable type is fixed size
like 'unit64_t'.

My concern is above warning log may cause people change the correct usage.

That was why I tried to make wording less strict, more like a reminder to double
check the usage.

If we can check that format specifier is used for 'unit64_t' variable, that will
be the best solution but that is very hard to do.
Should we add a little more information to the message to prevent false hit on
the correct usage?
  
Thomas Monjalon May 21, 2021, 1:09 p.m. UTC | #2
21/05/2021 14:01, Ferruh Yigit:
> On 5/19/2021 8:24 PM, Thomas Monjalon wrote:
> > From: Ferruh Yigit <ferruh.yigit@intel.com>
> > 
> > %lx or %llx tend to be wrong for 32-bit platform
> > if used for fixed size variable like uint64_t.
> > A checkpatch warning will avoid this common mistake.
> > 
> > Signed-off-by: Ferruh Yigit <ferruh.yigit@intel.com>
> > Signed-off-by: Thomas Monjalon <thomas@monjalon.net>
> > ---
> > v2: proposal to reword the message and comment
> > ---
> > +	# check %l or %ll format specifier
> > +	awk -v FOLDERS='lib drivers app examples' \
> > +		-v EXPRESSIONS='%ll*[xud]' \
> > +		-v RET_ON_FAIL=1 \
> > +		-v MESSAGE='Using %l format, should it be %PRI*64?' \
> > +		-f $(dirname $(readlink -f $0))/check-forbidden-tokens.awk \
> > +		"$1" || res=1
> 
> Using the %l or %ll format specifier is correct when the variable type is "long
> int" or "long long int", it is only wrong if the variable type is fixed size
> like 'unit64_t'.
> 
> My concern is above warning log may cause people change the correct usage.
> 
> That was why I tried to make wording less strict, more like a reminder to double
> check the usage.

This is a question now: "should it be", why do you think it is strict?

> If we can check that format specifier is used for 'unit64_t' variable, that will
> be the best solution but that is very hard to do.
> Should we add a little more information to the message to prevent false hit on
> the correct usage?

Your message was:
"Please check %llx usage which tends to be wrong most of the times"
Mine:
"Using %l format, should it be %PRI*64?"

Trying to give more info about what can be wrong while keeping short:

"Using %l format, is it a long variable or should it be %PRI*64?
  

Patch

diff --git a/devtools/checkpatches.sh b/devtools/checkpatches.sh
index db4c7d8301..0e09b2cab8 100755
--- a/devtools/checkpatches.sh
+++ b/devtools/checkpatches.sh
@@ -69,6 +69,14 @@  check_forbidden_additions() { # <patch>
 		-f $(dirname $(readlink -f $0))/check-forbidden-tokens.awk \
 		"$1" || res=1
 
+	# check %l or %ll format specifier
+	awk -v FOLDERS='lib drivers app examples' \
+		-v EXPRESSIONS='%ll*[xud]' \
+		-v RET_ON_FAIL=1 \
+		-v MESSAGE='Using %l format, should it be %PRI*64?' \
+		-f $(dirname $(readlink -f $0))/check-forbidden-tokens.awk \
+		"$1" || res=1
+
 	# forbid variable declaration inside "for" loop
 	awk -v FOLDERS='.' \
 		-v EXPRESSIONS='for[[:space:]]*\\((char|u?int|unsigned|s?size_t)' \