linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] Cleanup: snprintf() always NUL-terminates: depend on it
@ 2015-01-04 18:05 Giel van Schijndel
  2015-01-04 19:34 ` Linus Torvalds
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Giel van Schijndel @ 2015-01-04 18:05 UTC (permalink / raw)
  To: linux-kernel
  Cc: Giel van Schijndel, Brian King, James E.J. Bottomley,
	open list:SCSI SUBSYSTEM

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.
---
 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


^ permalink raw reply related	[flat|nested] 6+ messages in thread
* Re: [PATCH] Cleanup: snprintf() always NUL-terminates: depend on it
@ 2015-01-12  9:07 Aleksandr P
  0 siblings, 0 replies; 6+ messages in thread
From: Aleksandr P @ 2015-01-12  9:07 UTC (permalink / raw)
  To: me; +Cc: linux-kernel

>   len = snprintf(fname, 99, "%s", buf);
> - fname[len-1] = '\0';

> I just deleted that "really, really" NUL-termination line because
> it was based on a misunderstanding of snprintf()'s postcondition.

Are you sure this code can be simple deleted? It does not only
terminate the string but deletes the last character. According to man:
" Upon successful return, these functions return the number of
characters printed (EXCLUDING the null  byte  used  to end output to
strings)."
So, if buf was "abc" len would be 3 and fname would become "ab".

Best regards,
Alexandr

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2015-01-12  9:07 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 ` [PATCH RESEND] scsi: cleanup: " Giel van Schijndel
  -- strict thread matches above, loose matches on Subject: below --
2015-01-12  9:07 [PATCH] Cleanup: " Aleksandr P

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).