Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751443AbdH2T3v (ORCPT ); Tue, 29 Aug 2017 15:29:51 -0400 Received: from smtprelay0246.hostedemail.com ([216.40.44.246]:39530 "EHLO smtprelay.hostedemail.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1751237AbdH2T3k (ORCPT ); Tue, 29 Aug 2017 15:29:40 -0400 X-Session-Marker: 6A6F6540706572636865732E636F6D X-Spam-Summary: 2,0,0,,d41d8cd98f00b204,joe@perches.com,:::::::::::::::::::::,RULES_HIT:41:69:355:379:541:599:973:982:988:989:1260:1277:1311:1313:1314:1345:1359:1373:1437:1515:1516:1518:1534:1541:1593:1594:1711:1730:1747:1777:1792:2198:2199:2393:2553:2559:2562:2828:3138:3139:3140:3141:3142:3352:3622:3653:3865:3866:3867:3868:3870:3871:3872:3873:4321:5007:10004:10400:10848:11026:11658:11914:12043:12295:12438:12683:12740:12895:13069:13095:13311:13357:13439:13894:14095:14659:14721:21080:21324:21433:21627:30012:30054:30090:30091,0,RBL:none,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:,MSBL:0,DNSBL:none,Custom_rules:0:0:0,LFtime:1,LUA_SUMMARY:none X-HE-Tag: cake61_8e3a256903c5e X-Filterd-Recvd-Size: 2305 Message-ID: <1504034975.2040.43.camel@perches.com> Subject: Re: [PATCH] scsi: remove memset before memcpy From: Joe Perches To: Himanshu Jha , jejb@linux.vnet.ibm.com Cc: martin.petersen@oracle.com, kashyap.desai@broadcom.com, anil.gurumurthy@qlogic.com, sudarsana.kalluru@qlogic.com, sumit.saxena@broadcom.com, QLogic-Storage-Upstream@qlogic.com, linux-scsi@vger.kernel.org, linux-kernel@vger.kernel.org, megaraidlinux.pdl@broadcom.com Date: Tue, 29 Aug 2017 12:29:35 -0700 In-Reply-To: <1504032559-24969-1-git-send-email-himanshujha199640@gmail.com> References: <1504032559-24969-1-git-send-email-himanshujha199640@gmail.com> Content-Type: text/plain; charset="ISO-8859-1" X-Mailer: Evolution 3.22.6-1ubuntu1 Mime-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 1227 Lines: 58 On Wed, 2017-08-30 at 00:19 +0530, Himanshu Jha wrote: > drivers/scsi/megaraid/megaraid_sas_fusion.c I don't know if you did this with coccinelle. If so, it would be good to show the tool and script in the commit message. If not, the input script is pretty simple. $ cat memset_before_memcpy.cocci @@ expression e1; expression e2; expression e3; @@ - memset(e1, 0, e3); memcpy(e1, e2, e3); $ Adding a test to make sure e1 or e3 isn't modified before any other code uses them by doing $ cat memset_before_memcpy_2.cocci @@ expression e1; expression e2; expressi on e3; @@ - memset(e1, 0, e3); ... when != \( e1 \|?e3 \) memcpy(e1, e2, e3); $ finds more cases but there may be a false positive if e1 is a passed function argument and if the operation isn't effectively atomic like below: ---------------------------------------------------------------- --- a/net/bridge/br_multicast.c +++ b/net/bridge/br_multicast.c @@ -2556,7 +2556,6 @@ void br_multicast_get_stats(const struct ? struct br_mcast_stats tdst; ? int i; ? - memset(dest, 0, sizeof(*dest)); ? if (p) ? stats = p->mcast_stats; ? else ---------------------------------------------------------------- where the memcpy is the last line of the function.