params: Replace deprecated strcpy() with strscpy()
PATCH 1/1 params: Replace deprecated strcpy() with strscpy()
strcpy() is deprecated; use strscpy() instead.
--- kernel/params.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
kernel/params.c +4 -3
@@ -513,13 +513,14 @@ EXPORT_SYMBOL(param_array_ops);int param_set_copystring (const char *val,const struct kernel_param *kp) {const struct kparam_string *kps = kp->str; +const size_t len =strnlen (val, kps->maxlen); -if (strnlen (val, kps->maxlen) == kps->maxlen) { +if (len == kps->maxlen) {pr_err ("%s: string doesn't fit in %u chars.\n" , kp->name, kps->maxlen-1 );return -ENOSPC ; } -strcpy (kps->string, val); +strscpy (kps->string, val, len +1 );return 0 ; }EXPORT_SYMBOL (param_set_copystring);
@@ -841,7 +842,7 @@ static void __init param_sysfs_builtin(void) dot =strchr (kp->name,'.' );if (!dot) {/* This happens for core_param() */ -strcpy (modname,"kernel" ); +strscpy (modname,"kernel" ); name_len =0 ; }else { name_len = dot - kp->name +1 ;
Signature · 1 line
2.50.1
Quoted 18 lines · Thorsten Blum
strcpy()is deprecated; usestrscpy()instead.Signed-off-by Thorsten Blum <[email protected]>--- kernel/params.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)kernel/params.c +1
-0@@ -513,13 +513,14 @@ EXPORT_SYMBOL(param_array_ops);int param_set_copystring (const char *val,const struct kernel_param *kp) {const struct kparam_string *kps = kp->str; +const size_t len =strnlen (val, kps->maxlen);
kernel/params.c EXPORT_SYMBOL(param_array_ops); · line 525
Quoted 10 lines
kernel/params.c +2
-2-if (strnlen (val, kps->maxlen) == kps->maxlen) { +if (len == kps->maxlen) {pr_err ("%s: string doesn't fit in %u chars.\n" , kp->name, kps->maxlen-1 );return -ENOSPC ; } -strcpy (kps->string, val); +strscpy (kps->string, val, len +1 );return 0 ; }
Since the code already calculated the length of val and that it fits into kps->string, is there any advantage (or disadvantage) to using strscpy() over memcpy()?
kernel/params.c static void __init param_sysfs_builtin(void) · line 848
Quoted 10 lines
kernel/params.c +1
-1EXPORT_SYMBOL (param_set_copystring);@@ -841,7 +842,7 @@ static void __init param_sysfs_builtin(void) dot =strchr (kp->name,'.' );if (!dot) {/* This happens for core_param() */ -strcpy (modname,"kernel" ); +strscpy (modname,"kernel" ); name_len =0 ; }else { name_len = dot - kp->name +1 ;
I think this can go through the modules tree. I've CC'd the mailing list.
Signature · 2 lines
Thanks, Petr
Hi Petr,
Quoted 3 lines · Petr Pavlu
Since the code already calculated the length of val and that it fits into
kps->string, is there any advantage (or disadvantage) to usingstrscpy()overmemcpy()?
strscpy() guarantees that the destination buffer 'kps->string' is always NUL-terminated, even if the source 'val' is not. memcpy() just copies the bytes as they are.
If it were guaranteed that 'val' is always NUL-terminated, memcpy() would be fine too, but since param_set_copystring() is exported, we probably can't make that assumption.
Quoted 2 lines
I think this can go through the modules tree. I've CC'd the mailing list.
Thanks,
Thorsten
Quoted 7 lines · Thorsten Blum
Hi Petr,
Quoted 3 lines · Petr Pavlu
Since the code already calculated the length of val and that it fits into
kps->string, is there any advantage (or disadvantage) to usingstrscpy()overmemcpy()?
Quoted 7 lines
strscpy()guarantees that the destination buffer 'kps->string' is always NUL-terminated, even if the source 'val' is not.memcpy()just copies the bytes as they are.If it were guaranteed that 'val' is always NUL-terminated,
memcpy()would be fine too, but sinceparam_set_copystring()is exported, we probably can't make that assumption.
The function param_set_copystring() checks using 'strnlen(val, kps->maxlen) == kps->maxlen' if val contains NUL in the first kps->maxlen bytes. It can use memcpy() instead of strscpy() to avoid repeating this work.
-- Petr
Quoted 10 lines · Petr Pavlu
Quoted 9 lines · Thorsten Blum
Quoted 3 lines · Petr Pavlu
Since the code already calculated the length of val and that it fits into
kps->string, is there any advantage (or disadvantage) to usingstrscpy()overmemcpy()?
strscpy()guarantees that the destination buffer 'kps->string' is always NUL-terminated, even if the source 'val' is not.memcpy()just copies the bytes as they are.
Quoted 8 lines
Quoted 3 lines
If it were guaranteed that 'val' is always NUL-terminated,
memcpy()would be fine too, but sinceparam_set_copystring()is exported, we probably can't make that assumption.The function
param_set_copystring()checks using 'strnlen(val, kps->maxlen)==kps->maxlen' if val contains NUL in the firstkps->maxlenbytes. It can usememcpy()instead ofstrscpy()to avoid repeating this work.
I see, and yes
memcpy(kps->string, val, len + 1);
would then be slightly more efficient because strscpy() would just recompute the length before calling memcpy() internally.
I'll submit a v2.
Thanks,
Thorsten