Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp3222158pxb; Wed, 13 Oct 2021 01:21:02 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzqd8SRjCrTEkPuGWtL3Fpy1CrSSBjVisWRD12ZYsLM5BuqGYdRUirZAj8FJhk4oqIKeBoB X-Received: by 2002:a63:6b03:: with SMTP id g3mr3798416pgc.123.1634113262066; Wed, 13 Oct 2021 01:21:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1634113262; cv=none; d=google.com; s=arc-20160816; b=YCXF+yNnnPUwME496cPTG+/JATaO/zV+rOhz/kSIcQW8M9uhSqmmwBbJyC2MaqS70E imspG8/PopKIUytAxcMinyDSDSCiFodimmmHVxJFfDUeuuSzP4rrfErtG2N8XvKgu0Pz gOwQhIHaE3MJMKxCaatYb5+1ZqCEN7dJcxQa/bxpP5a7zZiRStQ51VwayDi5MingUhdz 2/Q2iNaqJeUkLTaRGlNPDO/f2rzRtoZdsrxoDYiDW6PgHtAeM0wNShnWh45rG+Yd4j5h ysQOsK0bewp2pmvkpGtZ234ew95rHNp9rczBccaabsZS/cgZ4PMH1fxTdnkMvCqMg9Mu +4Ig== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:date:cc:to:from:subject :message-id; bh=VCCpTzxaMwrP2jIt31ynEmR2aT3QLKitl6GawPpzj+Q=; b=LFQSeMFrhB3XwOspSSb1J9AmQWVqm2tpd17pE/asTAoECQUTgV/YZsP00kmBHBjd4X yW5gxOpKjDz7uOBOefhA5nrlO0tKO34xu3oyUY2C66txWavQ46sabs15/lJD90K3mSBS eIN5878HChR6x6bzqwJ/mwGLMKIwb9EjIKXB99SyNSfyVf89C1rmCXLVj8EupwM5GRmv 8aohzc0bwR/MlgB1L950nNGPubDPalcLxVBpIvOSNSvNv96XXCWPs06ExtHOvdcHjr9i x/9G/sFM8vtL/Vu5adY2L2rvpewEQKmm8diIA6plYt/Tf+Z5YrKxJg1KAIkZ1TUB4AbO CDHA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id 129si16556851pfy.191.2021.10.13.01.20.49; Wed, 13 Oct 2021 01:21:02 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233345AbhJMITq (ORCPT + 99 others); Wed, 13 Oct 2021 04:19:46 -0400 Received: from smtprelay0014.hostedemail.com ([216.40.44.14]:41246 "EHLO smtprelay.hostedemail.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S238885AbhJMITo (ORCPT ); Wed, 13 Oct 2021 04:19:44 -0400 Received: from omf13.hostedemail.com (clb03-v110.bra.tucows.net [216.40.38.60]) by smtprelay01.hostedemail.com (Postfix) with ESMTP id B860E100E7B55; Wed, 13 Oct 2021 08:17:32 +0000 (UTC) Received: from [HIDDEN] (Authenticated sender: joe@perches.com) by omf13.hostedemail.com (Postfix) with ESMTPA id 20B1A1124F6; Wed, 13 Oct 2021 08:17:32 +0000 (UTC) Message-ID: <3c060010a6100aec264f7ad984d2c6920e628a42.camel@perches.com> Subject: Re: Use of '%#02x' is almost always wrong From: Joe Perches To: Dan Carpenter Cc: LKML , kernel-janitors Date: Wed, 13 Oct 2021 01:17:30 -0700 In-Reply-To: <20211013061547.GG8429@kadam> References: <20211013061547.GG8429@kadam> Content-Type: text/plain; charset="ISO-8859-1" User-Agent: Evolution 3.40.0-1 MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Rspamd-Server: rspamout01 X-Rspamd-Queue-Id: 20B1A1124F6 X-Spam-Status: No, score=0.10 X-Stat-Signature: m1y7herwymfyqngs6xfkd4uu5791ye86 X-Session-Marker: 6A6F6540706572636865732E636F6D X-Session-ID: U2FsdGVkX1+Zb3hrSvPg4j5uiwukw2qSeLjEWS9ZoJ8= X-HE-Tag: 1634113052-11164 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, 2021-10-13 at 09:15 +0300, Dan Carpenter wrote: > On Sun, Oct 10, 2021 at 09:45:30AM -0700, Joe Perches wrote: > > Perhaps too many developers do not know that using '%#x' > > in printf output _includes_ the 0x prefix in the output width. > > > > This is a good point. Presumably you're going to add it to checkpatch.pl? Yeah. Something like the below: > I looked at '%#04x' and you would think those would all be printing char > types but some are printing shorts. :/ It's harder to deal with that > because 50% are correct from the tiny sample I looked at. That'd be difficult as checkpatch doesn't know format argument types. --- diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl index c27d2312cfc30..97deae9dafcdc 100755 --- a/scripts/checkpatch.pl +++ b/scripts/checkpatch.pl @@ -6703,7 +6703,7 @@ sub process { } } -# check for vsprintf extension %p misuses +# check for vsprintf format and extension %p misuses if ($perl_version_ok && defined $stat && $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s && @@ -6720,6 +6720,18 @@ sub process { my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0)); $fmt =~ s/%%//g; + # check for %# (0x prefixed) width too small + while ($fmt =~ /(%#0?([123])([xX]))/g) { + my $hex = $1; + my $width = $2; + my $case = $3; + if (!defined($stat_real)) { + $stat_real = get_stat_real($linenr, $lc); + } + WARN("VSPRINTF_HEX_WIDTH", + "hex specifier '$hex' - output width '$width' does not include the '0$case' prefix, width should be probably be increased\n" . "$here\n$stat_real"); + } + pos($fmt) = 0; while ($fmt =~ /(\%[\*\d\.]*p(\w)(\w*))/g) { $specifier = $1; $extension = $2;