[PATCH] platform/x86: replace strcpy() with strscpy()

Melih Emik posted 1 patch 3 weeks, 3 days ago
drivers/platform/x86/dell/dell_rbu.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
[PATCH] platform/x86: replace strcpy() with strscpy()
Posted by Melih Emik 3 weeks, 3 days ago
Replace strcpy() with strscpy() in dell_rbu to use the preferred

kernel string API for bounded copies.

The destination buffer size is known at compile time

(sizeof(image_type)), so this conversion is safe.

No functional change intended.

Signed-off-by: Melih Emik <melihemik@noirlang.tr>
---
 drivers/platform/x86/dell/dell_rbu.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/platform/x86/dell/dell_rbu.c b/drivers/platform/x86/dell/dell_rbu.c
index 3fa9de9aa47b..85eea8898fac 100644
--- a/drivers/platform/x86/dell/dell_rbu.c
+++ b/drivers/platform/x86/dell/dell_rbu.c
@@ -562,9 +562,9 @@ static ssize_t image_type_write(struct file *filp, struct kobject *kobj,
 		buffer[count] = '\0';
 
 	if (strstr(buffer, "mono"))
-		strcpy(image_type, "mono");
+		strscpy(image_type, "mono", sizeof(image_type));
 	else if (strstr(buffer, "packet"))
-		strcpy(image_type, "packet");
+		strscpy(image_type, "packet", sizeof(image_type));
 	else if (strstr(buffer, "init")) {
 		/*
 		 * If due to the user error the driver gets in a bad
-- 
2.54.0
Re: [PATCH] platform/x86: replace strcpy() with strscpy()
Posted by David Laight 2 weeks, 5 days ago
On Wed,  6 May 2026 20:50:31 +0300
Melih Emik <melihemik@noirlang.tr> wrote:

> Replace strcpy() with strscpy() in dell_rbu to use the preferred
> kernel string API for bounded copies.

I think I'm going to find that recommendation and change it :-)

For copies of constant strings into arrays it is actually slightly
better to use strcpy().
There is a subtle difference if the string is too long.
strscpy() will truncate it whereas you'll get a compile-time error
for strcpy().

Normally both calls result in a call to memcpy().
Which can get converted to writes on constants.

You are much better off looking for non-constant strcpy(), strcat()
and strlcat() call.

Once all the 'variable' strcpy() calls have gone the header can be
changed to error and new ones while still allowing strcpy() of quoted
strings into arrays - so there is no need to get rid of them at all.

-- David

> 
> The destination buffer size is known at compile time
> 
> (sizeof(image_type)), so this conversion is safe.
> 
> No functional change intended.
> 
> Signed-off-by: Melih Emik <melihemik@noirlang.tr>
> ---
>  drivers/platform/x86/dell/dell_rbu.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/platform/x86/dell/dell_rbu.c b/drivers/platform/x86/dell/dell_rbu.c
> index 3fa9de9aa47b..85eea8898fac 100644
> --- a/drivers/platform/x86/dell/dell_rbu.c
> +++ b/drivers/platform/x86/dell/dell_rbu.c
> @@ -562,9 +562,9 @@ static ssize_t image_type_write(struct file *filp, struct kobject *kobj,
>  		buffer[count] = '\0';
>  
>  	if (strstr(buffer, "mono"))
> -		strcpy(image_type, "mono");
> +		strscpy(image_type, "mono", sizeof(image_type));
>  	else if (strstr(buffer, "packet"))
> -		strcpy(image_type, "packet");
> +		strscpy(image_type, "packet", sizeof(image_type));
>  	else if (strstr(buffer, "init")) {
>  		/*
>  		 * If due to the user error the driver gets in a bad
Re: [PATCH] platform/x86: replace strcpy() with strscpy()
Posted by Ilpo Järvinen 2 weeks, 5 days ago
On Wed, 6 May 2026, Melih Emik wrote:

> Replace strcpy() with strscpy() in dell_rbu to use the preferred
> 
> kernel string API for bounded copies.
> 
> The destination buffer size is known at compile time
> 
> (sizeof(image_type)), so this conversion is safe.
> 
> No functional change intended.
> 
> Signed-off-by: Melih Emik <melihemik@noirlang.tr>
> ---
>  drivers/platform/x86/dell/dell_rbu.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/platform/x86/dell/dell_rbu.c b/drivers/platform/x86/dell/dell_rbu.c
> index 3fa9de9aa47b..85eea8898fac 100644
> --- a/drivers/platform/x86/dell/dell_rbu.c
> +++ b/drivers/platform/x86/dell/dell_rbu.c
> @@ -562,9 +562,9 @@ static ssize_t image_type_write(struct file *filp, struct kobject *kobj,
>  		buffer[count] = '\0';
>  
>  	if (strstr(buffer, "mono"))
> -		strcpy(image_type, "mono");
> +		strscpy(image_type, "mono", sizeof(image_type));
>  	else if (strstr(buffer, "packet"))
> -		strcpy(image_type, "packet");
> +		strscpy(image_type, "packet", sizeof(image_type));

As the sizeof information for image_type is available here, you should use 
two input strscpy() variant instead.


-- 
 i.
Re: [PATCH] platform/x86: replace strcpy() with strscpy()
Posted by Melih Emik 2 weeks, 5 days ago
On Mon, May 11, 2026 at 07:45:34PM +0300, Ilpo Järvinen wrote:
> As the sizeof information for image_type is available here, you should use
> two input strscpy() variant instead.

Thanks for the review.

I noticed this has already been addressed in linux-next by commit
0b9f109c318a ("platform/x86: dell_rbu: use strscpy in image_type_write"),
which also uses the two-argument strscpy() form.

Please ignore this patch.

Thanks,
Melih