public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Joe Perches <joe@perches.com>
To: Himanshu Jha <himanshujha199640@gmail.com>, 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
Subject: Re: [PATCH] scsi: remove memset before memcpy
Date: Tue, 29 Aug 2017 12:29:35 -0700	[thread overview]
Message-ID: <1504034975.2040.43.camel@perches.com> (raw)
In-Reply-To: <1504032559-24969-1-git-send-email-himanshujha199640@gmail.com>

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.

  reply	other threads:[~2017-08-29 19:29 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-08-29 18:49 [PATCH] scsi: remove memset before memcpy Himanshu Jha
2017-08-29 19:29 ` Joe Perches [this message]
2017-08-29 19:59   ` Himanshu Jha

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1504034975.2040.43.camel@perches.com \
    --to=joe@perches.com \
    --cc=QLogic-Storage-Upstream@qlogic.com \
    --cc=anil.gurumurthy@qlogic.com \
    --cc=himanshujha199640@gmail.com \
    --cc=jejb@linux.vnet.ibm.com \
    --cc=kashyap.desai@broadcom.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-scsi@vger.kernel.org \
    --cc=martin.petersen@oracle.com \
    --cc=megaraidlinux.pdl@broadcom.com \
    --cc=sudarsana.kalluru@qlogic.com \
    --cc=sumit.saxena@broadcom.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox