Received: by 2002:a05:6a10:c7d3:0:0:0:0 with SMTP id h19csp317238pxy; Sat, 14 Aug 2021 07:48:00 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzppRD7HAG5TLTJUjiY/459DpmPE6+ZKqUmUXQuuPNYaMkCk+VuEIBxD9y4Ecsq9+/lYLoN X-Received: by 2002:a05:6e02:eb0:: with SMTP id u16mr4642960ilj.303.1628952480461; Sat, 14 Aug 2021 07:48:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1628952480; cv=none; d=google.com; s=arc-20160816; b=b1IP44HEZPgD860bpHKY2BEL5KhYjEcO+gLs9i/xaiM8I+E0JRDtYuivl3RRAzPYH3 vOYBEfGY2SgCIuvI94sSpdzZpxuNkQ54rAq57Wf5D10TeigR1Voq3LsOmSLdjzcztBvb xOBSiBauxqPntU2w+nmSCpMDeeB4q5mJbMcPZfYdKR8oAiOMy2Ss0GapUkdo7fxR3WGP wKLLWbgkSmVMFGLpyd0jSOttywsd9reILPG0W+S1TD+8YoLT7KZnOxlLMAlBJF7G3gC9 hnKB7D8fa/sWhHoJifrBRj6qSIiHYlN2TFS6cYjotrQxsff6SG4w1pkNCTurP79lfl6e pqpQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-transfer-encoding:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=yTyK0uKOoSi7AVjCsk5/5RMEN0ZCT9jXzFnVBMbN2bc=; b=BUSvxKYD0t1+wqEkoQMvFx+fkLahtU7+cmA0S1ivBsc0TbVtCZ0RH2bU16Qmw6ICB3 wbJLYAJgX4CJUegzK2oGhRjUacqQv9m+5O3Qu6YOevEirDgWlV9aA2rAOegd2dL4T1TP opYzsc0GZSx4ESLMJ900h7ucFM9FpbQBIAIMwZZqDMcTzIcRJ46hPNJ4UYyoYojIin+i KJxwuM0ALHJGIeBRhsyo7cZsicuuAGDsoOQkK+aOtol02nmLF6MsQRu+2L909i46Xnwb lde7q+wPCjx3yzlhUTQnmKMugqpEtybVIofSLDDdBs1K8vP8/YDTLWR1nzyQn4vncMpZ 0+yg== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail (test mode) header.i=@armlinux.org.uk header.s=pandora-2019 header.b=lIfqO7uw; 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=armlinux.org.uk Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id g7si172335jar.3.2021.08.14.07.47.47; Sat, 14 Aug 2021 07:48:00 -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; dkim=fail (test mode) header.i=@armlinux.org.uk header.s=pandora-2019 header.b=lIfqO7uw; 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=armlinux.org.uk Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238643AbhHNOpR (ORCPT + 99 others); Sat, 14 Aug 2021 10:45:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51960 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238522AbhHNOpG (ORCPT ); Sat, 14 Aug 2021 10:45:06 -0400 Received: from pandora.armlinux.org.uk (pandora.armlinux.org.uk [IPv6:2001:4d48:ad52:32c8:5054:ff:fe00:142]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 47494C0613CF; Sat, 14 Aug 2021 07:44:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=armlinux.org.uk; s=pandora-2019; h=Sender:In-Reply-To: Content-Transfer-Encoding:Content-Type:MIME-Version:References:Message-ID: Subject:Cc:To:From:Date:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=yTyK0uKOoSi7AVjCsk5/5RMEN0ZCT9jXzFnVBMbN2bc=; b=lIfqO7uwZI02NXO9QC2J3hrHJ hLsdVmwyJqFo0VuHVQH51kQsrpWQxfycCDdNCHI9Ex69H/THCdsjCTM2NecI2CS+444LDrvfGo72h f6one6iV2sBQOJiPVTXJLA5du7UOJRxLYUG7DWzlmWTaMxTz+Ta1WW/Nb/IqXUgWtA4Oy7vszUJ9z HR73u6/xxaU4LkZZyFaEcPRo1PRMNa6WienvkinDKt7ab9LyKcyvQsKlp/j639mY5LgIgd/Jw/7pJ VvpXeTC3SrFI7yt/lKGR68Q2eQWo4soTywbTOzhkdS/n7AJ5yLvqGgGqFYCxXpWHoGiJeU1iOZiX0 ci+UK5PCg==; Received: from shell.armlinux.org.uk ([fd8f:7570:feb6:1:5054:ff:fe00:4ec]:47288) by pandora.armlinux.org.uk with esmtpsa (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mEuth-0005Jg-Oz; Sat, 14 Aug 2021 15:44:29 +0100 Received: from linux by shell.armlinux.org.uk with local (Exim 4.92) (envelope-from ) id 1mEutf-000684-T8; Sat, 14 Aug 2021 15:44:27 +0100 Date: Sat, 14 Aug 2021 15:44:27 +0100 From: "Russell King (Oracle)" To: Christophe JAILLET Cc: Dan Carpenter , Leon Romanovsky , Joe Perches , Dwaipayan Ray , Andy Whitcroft , Lukas Bulwahn , linux-kernel@vger.kernel.org, kernel-janitors@vger.kernel.org, Julia Lawall Subject: Re: [PATCH] checkpatch: prefer = {} initializations to = {0} Message-ID: <20210814144427.GZ22278@shell.armlinux.org.uk> References: <20210805104353.GD26417@kili> <1b94e688-a070-998a-3014-96bcbaed4cae@wanadoo.fr> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <1b94e688-a070-998a-3014-96bcbaed4cae@wanadoo.fr> User-Agent: Mutt/1.10.1 (2018-07-13) Sender: Russell King (Oracle) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sat, Aug 14, 2021 at 03:59:22PM +0200, Christophe JAILLET wrote: > Hi all, > > Le 05/08/2021 ? 12:43, Dan Carpenter a ?crit?: > > The "= {};" style empty struct initializer is preferred over = {0}. > > It avoids the situation where the first struct member is a pointer and > > that generates a Sparse warning about assigning using zero instead of > > NULL. Also it's just nicer to look at. > > > > Some people complain that {} is less portable but the kernel has > > different portability requirements from userspace so this is not a > > issue that we care about. > > > > Signed-off-by: Dan Carpenter > > --- > > scripts/checkpatch.pl | 6 ++++++ > > 1 file changed, 6 insertions(+) > > > > diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl > > index 461d4221e4a4..32c8a0ca6fd0 100755 > > --- a/scripts/checkpatch.pl > > +++ b/scripts/checkpatch.pl > > @@ -4029,6 +4029,12 @@ sub process { > > "Using $1 is unnecessary\n" . $herecurr); > > } > > +# prefer = {}; to = {0}; > > + if ($line =~ /= \{ *0 *\}/) { > > + WARN("ZERO_INITIALIZER", > > + "= {} is preferred over = {0}\n" . $herecurr); > > + } > > + > > # Check for potential 'bare' types > > my ($stat, $cond, $line_nr_next, $remain_next, $off_next, > > $realline_next); > > > > [1] and [2] state that {} and {0} don't have the same effect. So if correct, > this is not only a matter of style. This comes down to the fundamental question whether initialising a pointer with the integer 0 is permitted or not. The first thing to realise is that C allows integer 0 to be implicitly the null pointer. So, initialising a pointer to integer 0 sets it to the null pointer. Hence: void *bar = 0; is entirely legal, as is. struct foo { void *bar; }; struct foo foo = {0}; Things get more complex when you have: struct foo { int bar[16]; }; struct foo foo = {0}; or: struct foo { struct bar bar; }; struct foo foo = {0}; Depending on your GCC version, GCC may or may not emit: warning: missing braces around initializer [-Wmissing-braces] Clang does emit a very similar warning (see the thread that Vladimir linked to in the links in your email.) C99 does permit this, but in terms of portability across the currently supported compilers for the kernel, some will warn as per the above. So, it's best to ensure that the extra { } are present, or just avoid using the {0} notation and use the empty initialiser { }. > When testing with gcc 10.3.0, I arrived at the conclusion that both {} and > {0} HAVE the same behavior (i.e the whole structure and included structures > are completely zeroed) and I don't have a C standard to check what the rules > are. It should do. C defines that unmentioned members in an initialiser shall be "initialised implicitly the same as objects that have static storage duration". Given the platforms we currently support with the kernel, that basically means unmentioned members will be zeroed. The empty initialiser lists no members, so all members are not mentioned, so this applies. Lastly, {0} is extra unnecessary typing. Apart from one's style (which I think having warning-free builds trumps), I'm not sure why you'd want the extra work of typing the extra 0. :) -- RMK's Patch system: https://www.armlinux.org.uk/developer/patches/ FTTP is here! 40Mbps down 10Mbps up. Decent connectivity at last!