nfp_nsp_hwinfo_lookup_optional: use strscpy() to copy default_val
Subsystems (2)
PATCH 1/1 nfp_nsp_hwinfo_lookup_optional: use strscpy() to copy default_val
From: David Laight <[email protected]>
Use the result from strscpy() to detect overflow, only do so when the default is used.
Replace min_t() with min().
---
This is one of a group of patches that remove potentially unbounded strcpy() calls.
They are mostly replaced by strscpy() or, when strlen() has just been called, with memcpy() (usually including the '\0').
Calls with copy string literals into arrays are left unchanged. They are safe and easily detected as such.
The changes were made by getting the compiler to detect the calls and then fixing the code by hand.
Note that all the changes are only compile tested.
Some Makefiles were changed to allow files to contain strcpy(). As well as 'difficult to fix' files, this included 'show' functions as they really need to use sysfs_emit() or seq_printf().
All the patches are being sent individually to avoid very long cc lists. Apologies for the terse commit messages and likely unexpected tags. (There are about 100 patches in total.)
.../ethernet/netronome/nfp/nfpcore/nfp_nsp.c | 27 ++++++++----------- 1 file changed, 11 insertions(+), 16 deletions(-)
drivers/net/ethernet/netronome/nfp/nfpcore/nfp_nsp.c +11 -16
@@ -921,7 +921,7 @@ int nfp_nsp_hwinfo_lookup(struct nfp_nsp *state, void *buf, unsigned int size) {int err; - size =min_t (u32, size,NFP_HWINFO_LOOKUP_SIZE ); + size =min (size,NFP_HWINFO_LOOKUP_SIZE ); err =__nfp_nsp_hwinfo_lookup (state, buf, size, false);if (err)
@@ -940,25 +940,15 @@ int nfp_nsp_hwinfo_lookup_optional(struct nfp_nsp *state, void *buf, {int err; -/* Ensure that the default value is usable irrespective of whether -* it is actually going to be used. -*/ -if (strnlen (default_val, size) == size) -return -EINVAL ; - -if (!nfp_nsp_has_hwinfo_lookup (state)) { -strcpy (buf, default_val); -return 0 ; - } + size =min (size,NFP_HWINFO_LOOKUP_SIZE ); - size =min_t (u32, size,NFP_HWINFO_LOOKUP_SIZE ); +if (!nfp_nsp_has_hwinfo_lookup (state)) + goto set_default; err =__nfp_nsp_hwinfo_lookup (state, buf, size, true);if (err) { -if (err == -ENOENT ) { -strcpy (buf, default_val); -return 0 ; - } +if (err == -ENOENT ) + goto set_default;nfp_err (state->cpp,"NSP HWinfo lookup failed: %d\n" , err);return err;
@@ -970,6 +960,11 @@ int nfp_nsp_hwinfo_lookup_optional(struct nfp_nsp *state, void *buf, }return 0 ; + +set_default: +if (strscpy (buf, default_val, size) <0 ) +return -EINVAL ; +return 0 ; }int nfp_nsp_hwinfo_set (struct nfp_nsp *state,void *buf,unsigned int size)
Signature · 1 line
2.39.5