linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Giel van Schijndel <me@mortis.eu>
To: linux-kernel@vger.kernel.org
Cc: Giel van Schijndel <me@mortis.eu>, Brian King <brking@us.ibm.com>,
	"James E.J. Bottomley" <JBottomley@parallels.com>,
	linux-scsi@vger.kernel.org (open list:SCSI SUBSYSTEM)
Subject: [PATCH RESEND] scsi: cleanup: snprintf() always NUL-terminates: depend on it
Date: Wed,  7 Jan 2015 20:20:15 +0100	[thread overview]
Message-ID: <1420658416-20839-1-git-send-email-me@mortis.eu> (raw)
In-Reply-To: <1420394759-20345-1-git-send-email-me@mortis.eu>

Especially since one very strange piece of code seems to be written in
such a way that a NUL needs to be placed where a NUL is present already.
The author probably meant to fill the last byte of the buffer with a NUL
instead. But regardless of that: that isn't necessary since snprintf()
already guarantees NUL termination for buffers sizes > 0 and <= INT_MAX.

Signed-off-by: Giel van Schijndel <me@mortis.eu>
Reported-at: http://www.viva64.com/en/b/0299/
---
 drivers/scsi/ipr.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)

diff --git a/drivers/scsi/ipr.c b/drivers/scsi/ipr.c
index df4e27c..b49fe45 100644
--- a/drivers/scsi/ipr.c
+++ b/drivers/scsi/ipr.c
@@ -3990,7 +3990,6 @@ static ssize_t ipr_store_update_fw(struct device *dev,
 		return -EACCES;
 
 	len = snprintf(fname, 99, "%s", buf);
-	fname[len-1] = '\0';
 
 	if (request_firmware(&fw_entry, fname, &ioa_cfg->pdev->dev)) {
 		dev_err(&ioa_cfg->pdev->dev, "Firmware file %s not found\n", fname);
@@ -9358,13 +9357,12 @@ static int ipr_enable_msi(struct ipr_ioa_cfg *ioa_cfg)
 
 static void name_msi_vectors(struct ipr_ioa_cfg *ioa_cfg)
 {
-	int vec_idx, n = sizeof(ioa_cfg->vectors_info[0].desc) - 1;
+	int vec_idx;
 
 	for (vec_idx = 0; vec_idx < ioa_cfg->nvectors; vec_idx++) {
-		snprintf(ioa_cfg->vectors_info[vec_idx].desc, n,
+		snprintf(ioa_cfg->vectors_info[vec_idx].desc,
+			 sizeof(ioa_cfg->vectors_info[vec_idx].desc),
 			 "host%d-%d", ioa_cfg->host->host_no, vec_idx);
-		ioa_cfg->vectors_info[vec_idx].
-			desc[strlen(ioa_cfg->vectors_info[vec_idx].desc)] = 0;
 	}
 }
 
-- 
2.1.4


      parent reply	other threads:[~2015-01-07 19:20 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-01-04 18:05 [PATCH] Cleanup: snprintf() always NUL-terminates: depend on it Giel van Schijndel
2015-01-04 19:34 ` Linus Torvalds
2015-01-04 22:38   ` Giel van Schijndel
2015-01-04 23:05 ` Giel van Schijndel
2015-01-07 19:20 ` Giel van Schijndel [this message]

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=1420658416-20839-1-git-send-email-me@mortis.eu \
    --to=me@mortis.eu \
    --cc=JBottomley@parallels.com \
    --cc=brking@us.ibm.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-scsi@vger.kernel.org \
    /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;
as well as URLs for NNTP newsgroup(s).