X86 platform drivers
 help / color / mirror / Atom feed
* [PATCH linux-next] x86/platform/uv: use strscpy to instead of strncpy()
@ 2022-12-03  6:24 yang.yang29
  2022-12-03 12:15 ` Andy Shevchenko
  0 siblings, 1 reply; 3+ messages in thread
From: yang.yang29 @ 2022-12-03  6:24 UTC (permalink / raw)
  To: steve.wahl
  Cc: mike.travis, dimitri.sivanich, russ.anderson, dvhart, andy, tglx,
	mingo, bp, dave.hansen, x86, hpa, platform-driver-x86,
	linux-kernel, xu.panda, yang.yang29

From: Xu Panda <xu.panda@zte.com.cn>

The implementation of strscpy() is more robust and safer.
That's now the recommended way to copy NUL terminated strings.

Signed-off-by: Xu Panda <xu.panda@zte.com.cn>
Signed-off-by: Yang Yang <yang.yang29@zte.com>
---
 arch/x86/platform/uv/uv_nmi.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/arch/x86/platform/uv/uv_nmi.c b/arch/x86/platform/uv/uv_nmi.c
index a60af0230e27..1b0c8062c731 100644
--- a/arch/x86/platform/uv/uv_nmi.c
+++ b/arch/x86/platform/uv/uv_nmi.c
@@ -205,8 +205,7 @@ static int param_set_action(const char *val, const struct kernel_param *kp)
 	char arg[ACTION_LEN], *p;

 	/* (remove possible '\n') */
-	strncpy(arg, val, ACTION_LEN - 1);
-	arg[ACTION_LEN - 1] = '\0';
+	strscpy(arg, val, ACTION_LEN - 1);
 	p = strchr(arg, '\n');
 	if (p)
 		*p = '\0';
@@ -959,7 +958,7 @@ static int uv_handle_nmi(unsigned int reason, struct pt_regs *regs)

 		/* Unexpected return, revert action to "dump" */
 		if (master)
-			strncpy(uv_nmi_action, "dump", strlen(uv_nmi_action));
+			strscpy(uv_nmi_action, "dump", strlen(uv_nmi_action));
 	}

 	/* Pause as all CPU's enter the NMI handler */
-- 
2.15.2

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH linux-next] x86/platform/uv: use strscpy to instead of strncpy()
  2022-12-03  6:24 [PATCH linux-next] x86/platform/uv: use strscpy to instead of strncpy() yang.yang29
@ 2022-12-03 12:15 ` Andy Shevchenko
  2022-12-03 12:19   ` Andy Shevchenko
  0 siblings, 1 reply; 3+ messages in thread
From: Andy Shevchenko @ 2022-12-03 12:15 UTC (permalink / raw)
  To: yang.yang29
  Cc: steve.wahl, mike.travis, dimitri.sivanich, russ.anderson, dvhart,
	andy, tglx, mingo, bp, dave.hansen, x86, hpa, platform-driver-x86,
	linux-kernel, xu.panda

On Sat, Dec 3, 2022 at 8:24 AM <yang.yang29@zte.com.cn> wrote:

> The implementation of strscpy() is more robust and safer.
> That's now the recommended way to copy NUL terminated strings.

...

>         /* (remove possible '\n') */
> -       strncpy(arg, val, ACTION_LEN - 1);
> -       arg[ACTION_LEN - 1] = '\0';
> +       strscpy(arg, val, ACTION_LEN - 1);

Should be ACTION_LEN here, no?

>         p = strchr(arg, '\n');
>         if (p)
>                 *p = '\0';

Wouldn't be better to refactor this

p = strnchrnul(val, ACTION_LEN, '\n');
strscpy(arg, val, p - val);

?

-- 
With Best Regards,
Andy Shevchenko

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH linux-next] x86/platform/uv: use strscpy to instead of strncpy()
  2022-12-03 12:15 ` Andy Shevchenko
@ 2022-12-03 12:19   ` Andy Shevchenko
  0 siblings, 0 replies; 3+ messages in thread
From: Andy Shevchenko @ 2022-12-03 12:19 UTC (permalink / raw)
  To: yang.yang29
  Cc: steve.wahl, mike.travis, dimitri.sivanich, russ.anderson, dvhart,
	andy, tglx, mingo, bp, dave.hansen, x86, hpa, platform-driver-x86,
	linux-kernel, xu.panda

On Sat, Dec 3, 2022 at 2:15 PM Andy Shevchenko
<andy.shevchenko@gmail.com> wrote:
> On Sat, Dec 3, 2022 at 8:24 AM <yang.yang29@zte.com.cn> wrote:

...

> >         /* (remove possible '\n') */
> > -       strncpy(arg, val, ACTION_LEN - 1);
> > -       arg[ACTION_LEN - 1] = '\0';
> > +       strscpy(arg, val, ACTION_LEN - 1);
>
> Should be ACTION_LEN here, no?
>
> >         p = strchr(arg, '\n');
> >         if (p)
> >                 *p = '\0';
>
> Wouldn't be better to refactor this
>
> p = strnchrnul(val, ACTION_LEN, '\n');
> strscpy(arg, val, p - val);

Or even drop p completely

strscpy(arg, val, strnchrnul(val, ACTION_LEN, '\n') - val);

-- 
With Best Regards,
Andy Shevchenko

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-12-03 12:19 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-12-03  6:24 [PATCH linux-next] x86/platform/uv: use strscpy to instead of strncpy() yang.yang29
2022-12-03 12:15 ` Andy Shevchenko
2022-12-03 12:19   ` Andy Shevchenko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox