Skip to content

Commit 0c28a23

Browse files
tobluxdaniel-thompson
authored andcommitted
kdb: Replace deprecated strcpy() with helper function in kdb_defcmd()
strcpy() 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. Link: KSPP#88 Reviewed-by: Douglas Anderson <dianders@chromium.org> Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev> Signed-off-by: Daniel Thompson (RISCstar) <danielt@kernel.org>
1 parent 5b26f1a commit 0c28a23

3 files changed

Lines changed: 32 additions & 10 deletions

File tree

kernel/debug/kdb/kdb_main.c

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -721,20 +721,12 @@ static int kdb_defcmd(int argc, const char **argv)
721721
mp->name = kdb_strdup(argv[1], GFP_KDB);
722722
if (!mp->name)
723723
goto fail_name;
724-
mp->usage = kdb_strdup(argv[2], GFP_KDB);
724+
mp->usage = kdb_strdup_dequote(argv[2], GFP_KDB);
725725
if (!mp->usage)
726726
goto fail_usage;
727-
mp->help = kdb_strdup(argv[3], GFP_KDB);
727+
mp->help = kdb_strdup_dequote(argv[3], GFP_KDB);
728728
if (!mp->help)
729729
goto fail_help;
730-
if (mp->usage[0] == '"') {
731-
strcpy(mp->usage, argv[2]+1);
732-
mp->usage[strlen(mp->usage)-1] = '\0';
733-
}
734-
if (mp->help[0] == '"') {
735-
strcpy(mp->help, argv[3]+1);
736-
mp->help[strlen(mp->help)-1] = '\0';
737-
}
738730

739731
INIT_LIST_HEAD(&kdb_macro->statements);
740732
defcmd_in_progress = true;

kernel/debug/kdb/kdb_private.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ extern int kdbgetaddrarg(int, const char **, int*, unsigned long *,
110110
extern int kdbgetsymval(const char *, kdb_symtab_t *);
111111
extern int kdbnearsym(unsigned long, kdb_symtab_t *);
112112
extern char *kdb_strdup(const char *str, gfp_t type);
113+
extern char *kdb_strdup_dequote(const char *str, gfp_t type);
113114
extern void kdb_symbol_print(unsigned long, const kdb_symtab_t *, unsigned int);
114115

115116
/* Routine for debugging the debugger state. */

kernel/debug/kdb/kdb_support.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,35 @@ char *kdb_strdup(const char *str, gfp_t type)
255255
return s;
256256
}
257257

258+
/*
259+
* kdb_strdup_dequote - same as kdb_strdup(), but trims surrounding quotes from
260+
* the input string if present.
261+
* Remarks:
262+
* Quotes are only removed if there is both a leading and a trailing quote.
263+
*/
264+
char *kdb_strdup_dequote(const char *str, gfp_t type)
265+
{
266+
size_t len = strlen(str);
267+
char *s;
268+
269+
if (str[0] == '"' && len > 1 && str[len - 1] == '"') {
270+
/* trim both leading and trailing quotes */
271+
str++;
272+
len -= 2;
273+
}
274+
275+
len++; /* add space for NUL terminator */
276+
277+
s = kmalloc(len, type);
278+
if (!s)
279+
return NULL;
280+
281+
memcpy(s, str, len - 1);
282+
s[len - 1] = '\0';
283+
284+
return s;
285+
}
286+
258287
/*
259288
* kdb_getarea_size - Read an area of data. The kdb equivalent of
260289
* copy_from_user, with kdb messages for invalid addresses.

0 commit comments

Comments
 (0)