staging: greybus: Check for string truncation instead of NUL-termination
Subsystems (3)
PATCH v2 1/1 staging: greybus: Check for string truncation instead of NUL-termination
Commit 18f44de63f88 ("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 <[email protected]>
---
Changes in v2:
- Check the return value of strscpy_pad() for string truncation instead
of NUL-termination as suggested by Alex Elder
- Link to v1: https://lore.kernel.org/r/[email protected]/
--- drivers/staging/greybus/fw-management.c | 48 +++++++++---------------- 1 file changed, 17 insertions(+), 31 deletions(-)
drivers/staging/greybus/fw-management.c +17 -31
@@ -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 ; }
Signature · 1 line
2.49.0