kdb: Replace deprecated strcpy() with memcpy() in kdb_strdup()
PATCH v2 1/4 kdb: Replace deprecated strcpy() with memcpy() in kdb_strdup()
strcpy() is deprecated; use memcpy() instead.
--- kernel/debug/kdb/kdb_support.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
kernel/debug/kdb/kdb_support.c +4 -2
@@ -23,6 +23,7 @@#include <linux/uaccess.h> #include <linux/kdb.h> #include <linux/slab.h> +#include <linux/string.h> #include <linux/ctype.h> #include "kdb_private.h"
@@ -246,11 +247,12 @@ void kdb_symbol_print(unsigned long addr, const kdb_symtab_t *symtab_p,*/ char *kdb_strdup (const char *str,gfp_t type) { -int n =strlen (str)+1 ; +size_t n =strlen (str) +1 ;char *s =kmalloc (n, type);if (!s)return NULL ; -return strcpy (s, str); +memcpy (s, str, n); +return s; }/*
Signature · 1 line
2.50.1
PATCH v2 2/4 kdb: Replace deprecated strcpy() with memmove() in vkdb_printf()
strcpy() is deprecated and its behavior is undefined when the source and destination buffers overlap. Use memmove() instead to avoid any undefined behavior.
Adjust comments for clarity.
--- kernel/debug/kdb/kdb_io.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-)
kernel/debug/kdb/kdb_io.c +8 -6
@@ -714,8 +714,8 @@ int vkdb_printf(enum kdb_msgsrc src, const char *fmt, va_list ap)* it, depending on the results of the search. */ cp++;/* to byte after the newline */ - replaced_byte = *cp;/* remember what/where it was */ - cphold = cp; + replaced_byte = *cp;/* remember what it was */ + cphold = cp;/* remember where it was */ *cp ='\0' ;/* end the string for our search */ /*
@@ -732,8 +732,9 @@ int vkdb_printf(enum kdb_msgsrc src, const char *fmt, va_list ap)* Shift the buffer left. */ *cphold = replaced_byte; -strcpy (kdb_buffer, cphold); - len =strlen (kdb_buffer); + len =strlen (cphold); +/* Use memmove() because the buffers overlap */ +memmove (kdb_buffer, cphold, len +1 ); next_avail = kdb_buffer + len; size_avail =sizeof (kdb_buffer) - len; goto kdb_print_out;
@@ -872,8 +873,9 @@ int vkdb_printf(enum kdb_msgsrc src, const char *fmt, va_list ap)*/ if (kdb_grepping_flag && !suspend_grep) { *cphold = replaced_byte; -strcpy (kdb_buffer, cphold); - len =strlen (kdb_buffer); + len =strlen (cphold); +/* Use memmove() because the buffers overlap */ +memmove (kdb_buffer, cphold, len +1 ); next_avail = kdb_buffer + len; size_avail =sizeof (kdb_buffer) - len; }
Signature · 1 line
2.50.1
PATCH v2 3/4 kdb: Replace deprecated strcpy() with memcpy() in parse_grep()
In reply to Thorsten Blumstrcpy() is deprecated; use memcpy() instead.
We can safely use memcpy() because we already know the length of the source string 'cp' and that it is guaranteed to be NUL-terminated within the first KDB_GREP_STRLEN bytes.
--- kernel/debug/kdb/kdb_main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/debug/kdb/kdb_main.c +1 -1
@@ -860,7 +860,7 @@ static void parse_grep(const char *str)kdb_printf ("search string too long\n" );return ; } -strcpy (kdb_grep_string, cp); +memcpy (kdb_grep_string, cp, len +1 ); kdb_grepping_flag++;return ; }
Signature · 1 line
2.50.1
PATCH v2 4/4 kdb: Replace deprecated strcpy() with helper function in kdb_defcmd()
In reply to Thorsten Blumstrcpy() is deprecated; use the new helper function kdb_strdup_dequote() instead. In addition to string duplication similar to kdb_strdup(), it also trims surrounding quotes from the input string if present.
kdb_strdup_dequote() also checks for a trailing quote in the input string which was previously not checked.
---
Changes in v2:
- Copy 'len - 1' bytes using memcpy() as suggested by Doug
- Link to v1: https://lore.kernel.org/lkml/[email protected]/
--- kernel/debug/kdb/kdb_main.c | 12 ++---------- kernel/debug/kdb/kdb_private.h | 1 + kernel/debug/kdb/kdb_support.c | 29 +++++++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 10 deletions(-)
kernel/debug/kdb/kdb_main.c +2 -10
@@ -721,20 +721,12 @@ static int kdb_defcmd(int argc, const char **argv) mp->name =kdb_strdup (argv[1 ],GFP_KDB );if (!mp->name) goto fail_name; - mp->usage =kdb_strdup (argv[2 ],GFP_KDB ); + mp->usage =kdb_strdup_dequote (argv[2 ],GFP_KDB );if (!mp->usage) goto fail_usage; - mp->help =kdb_strdup (argv[3 ],GFP_KDB ); + mp->help =kdb_strdup_dequote (argv[3 ],GFP_KDB );if (!mp->help) goto fail_help; -if (mp->usage[0 ] =='"' ) { -strcpy (mp->usage, argv[2 ]+1 ); - mp->usage[strlen (mp->usage)-1 ] ='\0' ; - } -if (mp->help[0 ] =='"' ) { -strcpy (mp->help, argv[3 ]+1 ); - mp->help[strlen (mp->help)-1 ] ='\0' ; - }INIT_LIST_HEAD (&kdb_macro->statements); defcmd_in_progress = true;
kernel/debug/kdb/kdb_private.h +1 -0
@@ -110,6 +110,7 @@ extern int kdbgetaddrarg(int, const char **, int*, unsigned long *,extern int kdbgetsymval (const char *,kdb_symtab_t *);extern int kdbnearsym (unsigned long ,kdb_symtab_t *);extern char *kdb_strdup (const char *str,gfp_t type); +extern char *kdb_strdup_dequote (const char *str,gfp_t type);extern void kdb_symbol_print (unsigned long ,const kdb_symtab_t *,unsigned int );/* Routine for debugging the debugger state. */
kernel/debug/kdb/kdb_support.c +29 -0
@@ -255,6 +255,35 @@ char *kdb_strdup(const char *str, gfp_t type)return s; } +/* +* kdb_strdup_dequote - same as kdb_strdup(), but trims surrounding quotes from +* the input string if present. +* Remarks: +* Quotes are only removed if there is both a leading and a trailing quote. +*/ +char *kdb_strdup_dequote (const char *str,gfp_t type) +{ +size_t len =strlen (str); +char *s; + +if (str[0 ] =='"' && len >1 && str[len -1 ] =='"' ) { +/* trim both leading and trailing quotes */ + str++; + len -=2 ; + } + + len++;/* add space for NUL terminator */ + + s =kmalloc (len, type); +if (!s) +return NULL ; + +memcpy (s, str, len -1 ); + s[len -1 ] ='\0' ; + +return s; +} +/* * kdb_getarea_size - Read an area of data. The kdb equivalent of * copy_from_user, with kdb messages for invalid addresses.
Signature · 1 line
2.50.1
Re: [PATCH v2 4/4] kdb: Replace deprecated strcpy() with helper function in kdb_defcmd()
Hi Daniel,
Quoted 11 lines · Thorsten Blum
strcpy()is deprecated; use the new helper functionkdb_strdup_dequote()instead. In addition to string duplication similar tokdb_strdup(), it also trims surrounding quotes from the input string if present.
kdb_strdup_dequote()also checks for a trailing quote in the input string which was previously not checked.Reviewed-by Douglas Anderson <[email protected]>Signed-off-by Thorsten Blum <[email protected]>---
Could you give this series another look? All four patches have been reviewed by Doug and it still applies to -next.
Thanks,
Thorsten
Quoted 3 lines · Thorsten Blum
strcpy()is deprecated; usememcpy()instead.
Applied, thanks!
[1/4] kdb: Replace deprecated strcpy() with memcpy() in kdb_strdup()
commit: d4be3238d9e5f4841e5385cba3d81268c00d9e7d
[2/4] kdb: Replace deprecated strcpy() with memmove() in vkdb_printf()
commit: 8790cc2940bf9f5ec4d7458b0ea7f94a8acb094f
[3/4] kdb: Replace deprecated strcpy() with memcpy() in parse_grep()
commit: 5b26f1a3146454a24dbcb8b1cdae5d507f7432e6
[4/4] kdb: Replace deprecated strcpy() with helper function in kdb_defcmd()
commit: 0c28a23722e03695ae1cbbadde3dd32d75c1cb7fBest regards,
Signature · 1 line
Daniel Thompson (RISCstar) <[email protected]>