staging: greybus: Check for string truncation instead of NUL-termination
Commit 18f44de63f ("staging: greybus: change strncpy() to
strscpy_pad()") didn't remove the now unnecessary NUL-termination
checks. Unlike strncpy(), strscpy_pad() guarantees that the destination
buffer is NUL-terminated, making these checks obsolete. Remove them and
check for string truncation instead.
Compile-tested only.
Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>
Link: https://lore.kernel.org/r/20250402115755.1929043-1-thorsten.blum@linux.dev
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
This commit is contained in:
committed by
Greg Kroah-Hartman
parent
d486f2e078
commit
c511db3f1d
@@ -123,17 +123,11 @@ static int fw_mgmt_interface_fw_version_operation(struct fw_mgmt *fw_mgmt,
|
||||
fw_info->major = le16_to_cpu(response.major);
|
||||
fw_info->minor = le16_to_cpu(response.minor);
|
||||
|
||||
strscpy_pad(fw_info->firmware_tag, response.firmware_tag);
|
||||
|
||||
/*
|
||||
* The firmware-tag should be NULL terminated, otherwise throw error but
|
||||
* don't fail.
|
||||
*/
|
||||
if (fw_info->firmware_tag[GB_FIRMWARE_TAG_MAX_SIZE - 1] != '\0') {
|
||||
ret = strscpy_pad(fw_info->firmware_tag, response.firmware_tag);
|
||||
if (ret == -E2BIG)
|
||||
dev_err(fw_mgmt->parent,
|
||||
"fw-version: firmware-tag is not NULL terminated\n");
|
||||
fw_info->firmware_tag[GB_FIRMWARE_TAG_MAX_SIZE - 1] = '\0';
|
||||
}
|
||||
"fw-version: truncated firmware tag: %s\n",
|
||||
fw_info->firmware_tag);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -152,14 +146,12 @@ static int fw_mgmt_load_and_validate_operation(struct fw_mgmt *fw_mgmt,
|
||||
}
|
||||
|
||||
request.load_method = load_method;
|
||||
strscpy_pad(request.firmware_tag, tag);
|
||||
|
||||
/*
|
||||
* The firmware-tag should be NULL terminated, otherwise throw error and
|
||||
* fail.
|
||||
*/
|
||||
if (request.firmware_tag[GB_FIRMWARE_TAG_MAX_SIZE - 1] != '\0') {
|
||||
dev_err(fw_mgmt->parent, "load-and-validate: firmware-tag is not NULL terminated\n");
|
||||
ret = strscpy_pad(request.firmware_tag, tag);
|
||||
if (ret == -E2BIG) {
|
||||
dev_err(fw_mgmt->parent,
|
||||
"load-and-validate: truncated firmware tag: %s\n",
|
||||
request.firmware_tag);
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
@@ -248,14 +240,11 @@ static int fw_mgmt_backend_fw_version_operation(struct fw_mgmt *fw_mgmt,
|
||||
struct gb_fw_mgmt_backend_fw_version_response response;
|
||||
int ret;
|
||||
|
||||
strscpy_pad(request.firmware_tag, fw_info->firmware_tag);
|
||||
|
||||
/*
|
||||
* The firmware-tag should be NULL terminated, otherwise throw error and
|
||||
* fail.
|
||||
*/
|
||||
if (request.firmware_tag[GB_FIRMWARE_TAG_MAX_SIZE - 1] != '\0') {
|
||||
dev_err(fw_mgmt->parent, "backend-version: firmware-tag is not NULL terminated\n");
|
||||
ret = strscpy_pad(request.firmware_tag, fw_info->firmware_tag);
|
||||
if (ret == -E2BIG) {
|
||||
dev_err(fw_mgmt->parent,
|
||||
"backend-fw-version: truncated firmware tag: %s\n",
|
||||
request.firmware_tag);
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
@@ -302,13 +291,10 @@ static int fw_mgmt_backend_fw_update_operation(struct fw_mgmt *fw_mgmt,
|
||||
int ret;
|
||||
|
||||
ret = strscpy_pad(request.firmware_tag, tag);
|
||||
|
||||
/*
|
||||
* The firmware-tag should be NULL terminated, otherwise throw error and
|
||||
* fail.
|
||||
*/
|
||||
if (ret == -E2BIG) {
|
||||
dev_err(fw_mgmt->parent, "backend-update: firmware-tag is not NULL terminated\n");
|
||||
dev_err(fw_mgmt->parent,
|
||||
"backend-fw-update: truncated firmware tag: %s\n",
|
||||
request.firmware_tag);
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user