Commit 1b60c86d authored by Justin Stitt's avatar Justin Stitt Committed by Martin K. Petersen
Browse files

scsi: devinfo: Replace strncpy() and manual pad



Depending on the state of @compatible, we are going to do different things
with our @to buffer.

When @compatible is true we want a NUL-term'd and NUL-padded destination
buffer. Conversely, if @compatible is false we just want a space-padded
destination buffer (no NUL-term required).

As per:
/**
 * scsi_dev_info_list_add_keyed - add one dev_info list entry.
 * @compatible: if true, null terminate short strings.  Otherwise space pad.
...

Note that we can't easily use strtomem_pad() here as the size of the @to
buffer is unknown to the compiler due to indirection layers.

Now, the intent of the code is more clear (I probably didn't even need
to add a comment -- that's how clear it is).

Reviewed-by: default avatarKees Cook <keescook@chromium.org>
Signed-off-by: default avatarJustin Stitt <justinstitt@google.com>
Link: https://lore.kernel.org/r/20240305-strncpy-drivers-scsi-mpi3mr-mpi3mr_fw-c-v3-5-5b78a13ff984@google.com


Signed-off-by: default avatarMartin K. Petersen <martin.petersen@oracle.com>
parent 4f94864d
Loading
Loading
Loading
Loading
+10 −8
Original line number Diff line number Diff line
@@ -293,14 +293,16 @@ static void scsi_strcpy_devinfo(char *name, char *to, size_t to_length,
	size_t from_length;

	from_length = strlen(from);
	/* This zero-pads the destination */
	strncpy(to, from, to_length);
	if (from_length < to_length && !compatible) {

	/*
		 * space pad the string if it is short.
	 * null pad and null terminate if compatible
	 * otherwise space pad
	 */
		memset(&to[from_length], ' ', to_length - from_length);
	}
	if (compatible)
		strscpy_pad(to, from, to_length);
	else
		memcpy_and_pad(to, to_length, from, from_length, ' ');

	if (from_length > to_length)
		 printk(KERN_WARNING "%s: %s string '%s' is too long\n",
			__func__, name, from);