Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp580950imm; Fri, 13 Jul 2018 02:45:48 -0700 (PDT) X-Google-Smtp-Source: AAOMgpfRtjHWzuB3DtCw5irpVkYtnysYb1lCnWBsHBpTdVXd3n4TXFHF/Gw44RJhQuzErrBd3BeT X-Received: by 2002:a63:5c52:: with SMTP id n18-v6mr5356984pgm.360.1531475148335; Fri, 13 Jul 2018 02:45:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1531475148; cv=none; d=google.com; s=arc-20160816; b=pRG3paEW/+xzY8a4EBaFMelpJBXyraTfEhQt/IWvOBRY8MpGIhEfgOGUUEMI+KhUlK y7oxHlYWh92E8YF4Zzjpw6Z3jMTD1OeezJIN9ow8bHNjWYkkiSSCGqcxkZF2Mg8Qh1XQ KdBHJj67tYmB3VmfojBjH/bkpJaGAzOUUFUSyEfkvWAb2lBlfzSWD6hnpiwW/j3OYGwe Rl50KsgaAtEfQUq+YdFeNeBxcewUti519V4Heu0Ef8hV/TzhISeDs3+qiT9Zq7iIlWq4 9Ho6ejLRaV1c/afZZP8/UvlCcNK8RqIkEYp+97iVXx6sFyVBVteY1s73qYQWUb2dnQH9 I8fA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :message-id:in-reply-to:subject:cc:to:from:date :arc-authentication-results; bh=LRZTQp+RSrDUxbzfp5gGdSwRDYQ/Wrw5lGOKJ/Rad/Q=; b=LNGXGXfvjGhho7WCNaYm2QB79FtJ+KFF9X168Aa8ncrIsitQHzRW5vJXU2VjEhP36d MhXSGemZ1O52+rWnBvbzwOJu5hiEaPu9aibbBUEyoQo9rpxP9qatXN+YBzz6WYnrmfz+ 6BkB1W0EQDFp+dKRTqy6ktHSQDGP1JLjtG1xpmgx4ky9K1r2aBcTFntFhbM15E/Mseka 8WAb0dJ8F1BuPtiByWCPE79U+Fm/fVWahsir1+VTy4qF0m2mRox13EmuQb3zLObIWkAQ sdFepLHfC2YJIGvMlJIGBJms8wNMaZLeDV8QTk1MxCna576YoWu5gIkIp77I9311dbAZ eA/g== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id l4-v6si23539491plb.213.2018.07.13.02.45.33; Fri, 13 Jul 2018 02:45:48 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727454AbeGMJ6D (ORCPT + 99 others); Fri, 13 Jul 2018 05:58:03 -0400 Received: from mail3-relais-sop.national.inria.fr ([192.134.164.104]:44773 "EHLO mail3-relais-sop.national.inria.fr" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726467AbeGMJ6D (ORCPT ); Fri, 13 Jul 2018 05:58:03 -0400 X-IronPort-AV: E=Sophos;i="5.51,347,1526335200"; d="scan'208";a="272902254" Received: from unknown (HELO hadrien.local) ([38.97.127.241]) by mail3-relais-sop.national.inria.fr with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 13 Jul 2018 11:44:08 +0200 Date: Fri, 13 Jul 2018 05:44:06 -0400 (EDT) From: Julia Lawall X-X-Sender: jll@hadrien To: Himanshu Jha cc: Dominique Martinet , Michal Marek , Nicolas Palix , linux-kernel@vger.kernel.org, =?ISO-8859-15?Q?Ville_Syrj=E4l=E4?= , yamada.masahiro@socionext.com, cocci@systeme.lip6.fr Subject: Re: [Cocci] [PATCH 01/18] coccinelle: change strncpy+truncation to strlcpy In-Reply-To: <20180713091412.GA11250@himanshu-Vostro-3559> Message-ID: References: <1531444483-17338-1-git-send-email-asmadeus@codewreck.org> <20180713074455.GA6859@himanshu-Vostro-3559> <20180713080023.GB9638@nautica> <20180713091412.GA11250@himanshu-Vostro-3559> User-Agent: Alpine 2.20 (DEB 67 2015-01-07) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, 13 Jul 2018, Himanshu Jha wrote: > On Fri, Jul 13, 2018 at 10:00:23AM +0200, Dominique Martinet wrote: > > Himanshu Jha wrote on Fri, Jul 13, 2018: > > > > I expect each maintainer will pick their share of the patchs if they > > > > agree with it and the rest will just be dropped? > > > > > > Masahiro Yamada takes coccinelle patches, > > > so please cc him or your patch would be lost. > > > > Thanks, will do. > > > > > > +virtual patch > > > > +virtual context > > > > > > You might consider adding context rule or remove this line perhaps ? > > > > Victim of copypasta, I'll remove this. > > > > > > +-strncpy@p( > > > > ++strlcpy( > > > > + dest, src, sz); > > > > +-dest[sz - 1] = '\0'; > > > > > > The above rule produces an output that I think is not correct: > > > -------------------------------------------------------------- > > > diff = > > > diff -u -p a//ti/wl1251/acx.c b//ti/wl1251/acx.c > > > --- a//ti/wl1251/acx.c > > > +++ b//ti/wl1251/acx.c > > > @@ -150,14 +150,7 @@ int wl1251_acx_fw_version(struct wl1251 > > > } > > > > > > /* be careful with the buffer sizes */ > > > - strncpy(buf, rev->fw_version, min(len, sizeof(rev->fw_version))); > > > - > > > - /* > > > - * if the firmware version string is exactly > > > - * sizeof(rev->fw_version) long or fw_len is less than > > > - * sizeof(rev->fw_version) it won't be null terminated > > > - */ > > > - buf[min(len, sizeof(rev->fw_version)) - 1] = '\0'; > > > + strlcpy(buf, rev->fw_version, min(len, sizeof(rev->fw_version))); > > > > > > ----------------------------------------------------------------- > > > > > > I think the comment is useful and should not be removed. > > > > I agree this comment is useful now that I'm taking a closer look, I > > glanced at this too fast. > > I'm not sure how to make coccinelle not remove comments between lines > > though? > > Well, there is no such facility in Coccinelle to ignore comments. > You can hack with other facilities provided in SmPL though ;) > > Try this: > > $ spatch -D patch --sp-file strlcopy.cocci --very-quiet drivers/net/wireless/ti/wl1251/acx.c > > --------------------------------------------------------------------- > virtual patch > > @depends on patch@ > expression dest, src, sz; > identifier f; > @@ > > ( > - strncpy( > + strlcpy( > dest, src, sizeof(sz)); > - dest[sizeof(sz) - 1] = '\0'; > | > - strncpy( > + strlcpy( > dest, src, f); > - dest[f - 1] = '\0'; > ) > --------------------------------------------------------------------- > > This eliminates that case because expression is generic metavariable and > it somehow matched whole "min(len, sizeof(...)..", so it better to > divide the rules as done above to be more specific about the matching > pattern. > > I thought to replace "identifier f" with "constant F" but that misses > few cases. > > Also, it is advised to put a space affer '+/-' Thanks Himanshu for the suggestions. However, I'm not sure to follow the discussion. The original problem was that Coccinelle was removing a comment that should be preserved. I think that this occurs because the line just below the comment is completely removed. Coccinelle considers that the comment belongs with that line and if the line is removed the comment won't make much sense. In Himanshu's solution, the code is just not transformed at all, so as a side effect the comment stays too. Is that what is wanted in this case? julia