All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/4] ibmvscsi: remove redundant zero fill
@ 2009-11-21 22:09 Németh Márton
  0 siblings, 0 replies; only message in thread
From: Németh Márton @ 2009-11-21 22:09 UTC (permalink / raw)
  To: Dave Boutcher, Santiago Leon, Linda Xie, FUJITA Tomonori,
	linux-scsi
  Cc: cocci, LKML

From: Márton Németh <nm127@freemail.hu>

The buffer is first zeroed out by memset(). Then strncpy() is used to
fill the content. The strncpy() function also pads the string till the
end of the specified length, which is redundant. The strncpy() does not
ensures that the string will be properly closed with 0. Use strlcpy()
instead.

The semantic match that finds this kind of pattern is as follows:
(http://coccinelle.lip6.fr/)

// <smpl>
@@
expression buffer;
expression size;
expression str;
@@
	memset(buffer, 0, size);
	...
-	strncpy(
+	strlcpy(
	buffer, str, sizeof(buffer)
	);
@@
expression buffer;
expression size;
expression str;
@@
	memset(&buffer, 0, size);
	...
-	strncpy(
+	strlcpy(
	&buffer, str, sizeof(buffer));
@@
expression buffer;
identifier field;
expression size;
expression str;
@@
	memset(buffer, 0, size);
	...
-	strncpy(
+	strlcpy(
	buffer->field, str, sizeof(buffer->field)
	);
@@
expression buffer;
identifier field;
expression size;
expression str;
@@
	memset(&buffer, 0, size);
	...
-	strncpy(
+	strlcpy(
	buffer.field, str, sizeof(buffer.field));
// </smpl>

On strncpy() vs strlcpy() see http://www.gratisoft.us/todd/papers/strlcpy.html .

Signed-off-by: Márton Németh <nm127@freemail.hu>
---
diff -u -p a/drivers/scsi/ibmvscsi/ibmvstgt.c b/drivers/scsi/ibmvscsi/ibmvstgt.c
--- a/drivers/scsi/ibmvscsi/ibmvstgt.c 2009-09-10 00:13:59.000000000 +0200
+++ b/drivers/scsi/ibmvscsi/ibmvstgt.c 2009-11-21 22:10:13.000000000 +0100
@@ -339,7 +339,7 @@ int send_adapter_info(struct iu_entry *i
 	memset(info, 0, sizeof(*info));

 	strcpy(info->srp_version, "16.a");
-	strncpy(info->partition_name, partition_name,
+	strlcpy(info->partition_name, partition_name,
 		sizeof(info->partition_name));
 	info->partition_number = partition_number;
 	info->mad_version = 1;
diff -u -p a/drivers/scsi/ibmvscsi/rpa_vscsi.c b/drivers/scsi/ibmvscsi/rpa_vscsi.c
--- a/drivers/scsi/ibmvscsi/rpa_vscsi.c 2009-09-10 00:13:59.000000000 +0200
+++ b/drivers/scsi/ibmvscsi/rpa_vscsi.c 2009-11-21 22:10:15.000000000 +0100
@@ -181,7 +181,7 @@ static void set_adapter_info(struct ibmv
 	dev_info(hostdata->dev, "SRP_VERSION: %s\n", SRP_VERSION);
 	strcpy(hostdata->madapter_info.srp_version, SRP_VERSION);

-	strncpy(hostdata->madapter_info.partition_name, partition_name,
+	strlcpy(hostdata->madapter_info.partition_name, partition_name,
 			sizeof(hostdata->madapter_info.partition_name));

 	hostdata->madapter_info.partition_number = partition_number;

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2009-11-21 22:09 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-11-21 22:09 [PATCH 1/4] ibmvscsi: remove redundant zero fill Németh Márton

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.