mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] scsi: st: Avoid truncating the tape name for large tape numbers
@ 2026-11-06  2:17 xujiahao
  2026-11-06 13:44 ` Laurence Oberman
  0 siblings, 1 reply; 2+ messages in thread
From: xujiahao @ 2026-11-06  2:17 UTC (permalink / raw)
  To: Laurence Oberman, John Meneghini
  Cc: James E.J. Bottomley, Martin K. Petersen, linux-scsi,
	linux-kernel, xujiahao

create_one_cdev() builds the class device name in a 10-byte buffer as
"n" (no-rewind devices) + tape->name + a one-character mode suffix.
tape->name is a DISK_NAME_LEN (32) byte array holding "st%d", and the
tape index comes from an idr with an upper bound of ST_MAX_TAPES - 1
(131071, six digits), so "n" + "st131071" + suffix + terminator needs
11 bytes.  Names of tapes with large index numbers are truncated today
and lose the mode suffix, which leaves the rewind modes of such a tape
with the same name.

Size the buffer after the actual requirement, DISK_NAME_LEN + 2, and
pass sizeof(name) to snprintf() so the two cannot drift apart again.

GCC reports with -Wformat-truncation=2:

  drivers/scsi/st.c:4393:31: warning: '%s' directive output may be truncated writing up to 31 bytes into a region of size between 9 and 10 [-Wformat-truncation=]

Signed-off-by: xujiahao <xujiahao@cmss.chinamobile.com>
---
 drivers/scsi/st.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/scsi/st.c b/drivers/scsi/st.c
index e9d41e3cc..311781987 100644
--- a/drivers/scsi/st.c
+++ b/drivers/scsi/st.c
@@ -4366,7 +4366,7 @@ static int create_one_cdev(struct scsi_tape *tape, int mode, int rew)
 	struct cdev *cdev;
 	struct device *dev;
 	struct st_modedef *STm = &(tape->modes[mode]);
-	char name[10];
+	char name[DISK_NAME_LEN + 2];
 	int dev_num = tape->index;
 
 	cdev_devno = MKDEV(SCSI_TAPE_MAJOR, TAPE_MINOR(dev_num, mode, rew));
@@ -4390,7 +4390,7 @@ static int create_one_cdev(struct scsi_tape *tape, int mode, int rew)
 	}
 
 	i = mode << (4 - ST_NBR_MODE_BITS);
-	snprintf(name, 10, "%s%s%s", rew ? "n" : "",
+	snprintf(name, sizeof(name), "%s%s%s", rew ? "n" : "",
 		 tape->name, st_formats[i]);
 
 	dev = device_create(&st_sysfs_class, &tape->device->sdev_gendev,
-- 
2.43.0




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

end of thread, other threads:[~2026-11-06 13:44 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-06  2:17 [PATCH] scsi: st: Avoid truncating the tape name for large tape numbers xujiahao
2026-11-06 13:44 ` Laurence Oberman

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®