kdb: Replace deprecated strcpy() with strscpy()
PATCH 1/1 kdb: Replace deprecated strcpy() with strscpy()
strcpy() is deprecated; use strscpy() instead and remove several manual NUL-terminations.
Since the destination buffers 'cmd_cur' and 'cmd_hist[cmd_head]' have the fixed length CMD_BUFLEN, strscpy() automatically determines their size using sizeof() when the size argument is omitted. This makes the explicit size arguments for the existing strscpy() calls unnecessary, remove them.
No functional changes intended.
--- kernel/debug/kdb/kdb_main.c | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-)
kernel/debug/kdb/kdb_main.c +14 -18
@@ -727,14 +727,10 @@ static int kdb_defcmd(int argc, const char **argv) mp->help =kdb_strdup (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' ; - } +if (mp->usage[0 ] =='"' ) +strscpy (mp->usage, argv[2 ] +1 ,strlen (argv[2 ]) -1 ); +if (mp->help[0 ] =='"' ) +strscpy (mp->help, argv[3 ] +1 ,strlen (argv[3 ]) -1 );INIT_LIST_HEAD (&kdb_macro->statements); defcmd_in_progress = true;
@@ -860,7 +856,7 @@ static void parse_grep(const char *str)kdb_printf ("search string too long\n" );return ; } -strcpy (kdb_grep_string, cp); +strscpy (kdb_grep_string, cp); kdb_grepping_flag++;return ; }
@@ -1076,12 +1072,12 @@ static int handle_ctrl_cmd(char *cmd)if (cmdptr != cmd_tail) cmdptr = (cmdptr +KDB_CMD_HISTORY_COUNT -1 ) %KDB_CMD_HISTORY_COUNT ; -strscpy (cmd_cur, cmd_hist[cmdptr],CMD_BUFLEN ); +strscpy (cmd_cur, cmd_hist[cmdptr]);return 1 ;case CTRL_N :if (cmdptr != cmd_head) cmdptr = (cmdptr+1 ) %KDB_CMD_HISTORY_COUNT ; -strscpy (cmd_cur, cmd_hist[cmdptr],CMD_BUFLEN ); +strscpy (cmd_cur, cmd_hist[cmdptr]);return 1 ; }return 0 ;
@@ -1285,19 +1281,19 @@ static int kdb_local(kdb_reason_t reason, int error, struct pt_regs *regs, cmdbuf =kdb_getstr (cmdbuf,CMD_BUFLEN , kdb_prompt_str);if (*cmdbuf !='\n' ) {if (*cmdbuf <32 ) { -if (cmdptr == cmd_head) { +if (cmdptr == cmd_head) +/* Copy the current command to the +* history and let strscpy() replace the +* last character with a NUL terminator. +*/ strscpy (cmd_hist[cmd_head], cmd_cur, -CMD_BUFLEN ); - *(cmd_hist[cmd_head] + -strlen (cmd_hist[cmd_head])-1 ) ='\0' ; - } +strlen (cmd_cur));if (!handle_ctrl_cmd (cmdbuf)) *(cmd_cur+strlen (cmd_cur)-1 ) ='\0' ; cmdbuf = cmd_cur; goto do_full_getstr; }else { -strscpy (cmd_hist[cmd_head], cmd_cur, -CMD_BUFLEN ); +strscpy (cmd_hist[cmd_head], cmd_cur); } cmd_head = (cmd_head+1 ) %KDB_CMD_HISTORY_COUNT ;
Signature · 1 line
2.50.1
Quoted 2 lines · Thorsten Blum
strcpy()is deprecated; usestrscpy()instead and remove several manual NUL-terminations.
Manual NULL terminations are good, why get rid of that?
Quoted 5 lines
Since the destination buffers '
cmd_cur' and 'cmd_hist[cmd_head]' have the fixed lengthCMD_BUFLEN,strscpy()automatically determines their size usingsizeof()when the size argument is omitted. This makes the explicit size arguments for the existingstrscpy()calls unnecessary, remove them.
But now you are dynamically calculating this?
Quoted 1 line
No functional changes intended.
How did you test this? Many of these types of changes are wrong, so you really really need to prove it is correct.
Quoted 15 lines
Signed-off-by Thorsten Blum <[email protected]>--- kernel/debug/kdb/kdb_main.c | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-)kernel/debug/kdb/kdb_main.c +0
-0@@ -727,14 +727,10 @@ static int kdb_defcmd(int argc, const char **argv) mp->help =kdb_strdup (argv[3 ],GFP_KDB );if (!mp->help) goto fail_help;
kernel/debug/kdb/kdb_main.c static int kdb_defcmd(int argc, const char **argv) · line 731
Quoted 10 lines
kernel/debug/kdb/kdb_main.c +2
-8-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' ; - } +if (mp->usage[0 ] =='"' ) +strscpy (mp->usage, argv[2 ] +1 ,strlen (argv[2 ]) -1 );
Now you are manually testing the length of argv[2], are you sure that's ok?
kernel/debug/kdb/kdb_main.c static void parse_grep(const char *str) · line 859
Quoted 11 lines
kernel/debug/kdb/kdb_main.c +3
-1+if (mp->help[0 ] =='"' ) +strscpy (mp->help, argv[3 ] +1 ,strlen (argv[3 ]) -1 );INIT_LIST_HEAD (&kdb_macro->statements); defcmd_in_progress = true;@@ -860,7 +856,7 @@ static void parse_grep(const char *str)kdb_printf ("search string too long\n" );return ; } -strcpy (kdb_grep_string, cp); +strscpy (kdb_grep_string, cp);
If this was just a search/replace, it would have been done already, so why is this ok?
kernel/debug/kdb/kdb_main.c static int handle_ctrl_cmd(char *cmd) · line 1075
Quoted 9 lines
kernel/debug/kdb/kdb_main.c +1
-1kdb_grepping_flag++;return ; }@@ -1076,12 +1072,12 @@ static int handle_ctrl_cmd(char *cmd)if (cmdptr != cmd_tail) cmdptr = (cmdptr +KDB_CMD_HISTORY_COUNT -1 ) %KDB_CMD_HISTORY_COUNT ; -strscpy (cmd_cur, cmd_hist[cmdptr],CMD_BUFLEN ); +strscpy (cmd_cur, cmd_hist[cmdptr]);
Same here. And other places...
thanks,
greg k-h
Quoted 51 lines · Greg Kroah-Hartman
Quoted 2 lines · Thorsten Blum
strcpy()is deprecated; usestrscpy()instead and remove several manual NUL-terminations.Manual NULL terminations are good, why get rid of that?
Quoted 5 lines
Since the destination buffers '
cmd_cur' and 'cmd_hist[cmd_head]' have the fixed lengthCMD_BUFLEN,strscpy()automatically determines their size usingsizeof()when the size argument is omitted. This makes the explicit size arguments for the existingstrscpy()calls unnecessary, remove them.But now you are dynamically calculating this?
Quoted 1 line
No functional changes intended.
How did you test this? Many of these types of changes are wrong, so you really really need to prove it is correct.
Quoted 15 lines
Signed-off-by Thorsten Blum <[email protected]>--- kernel/debug/kdb/kdb_main.c | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-)kernel/debug/kdb/kdb_main.c +0
-0@@ -727,14 +727,10 @@ static int kdb_defcmd(int argc, const char **argv) mp->help =kdb_strdup (argv[3 ],GFP_KDB );if (!mp->help) goto fail_help;kernel/debug/kdb/kdb_main.c static int kdb_defcmd(int argc, const char **argv) · line 731
Quoted 10 lines
kernel/debug/kdb/kdb_main.c +2
-8-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' ; - } +if (mp->usage[0 ] =='"' ) +strscpy (mp->usage, argv[2 ] +1 ,strlen (argv[2 ]) -1 );Now you are manually testing the length of argv[2], are you sure that's ok?
Quoted 3 lines
kernel/debug/kdb/kdb_main.c +2
-0+if (mp->help[0 ] =='"' ) +strscpy (mp->help, argv[3 ] +1 ,strlen (argv[3 ]) -1 );
Quoted 11 lines
kernel/debug/kdb/kdb_main.c static void parse_grep(const char *str) · line 859
Quoted 8 lines
kernel/debug/kdb/kdb_main.c +1
-1INIT_LIST_HEAD (&kdb_macro->statements); defcmd_in_progress = true;@@ -860,7 +856,7 @@ static void parse_grep(const char *str)kdb_printf ("search string too long\n" );return ; } -strcpy (kdb_grep_string, cp); +strscpy (kdb_grep_string, cp);If this was just a search/replace, it would have been done already, so why is this ok?
I missed that strscpy() can now handle 2 arguments like this, so yes, this should be ok.
BUT, you just checked the length above this line, which now isn't needed, right? So this function can get simpler?
Quoted 13 lines
kernel/debug/kdb/kdb_main.c static int handle_ctrl_cmd(char *cmd) · line 1075
Quoted 11 lines
kernel/debug/kdb/kdb_main.c +1
-1kdb_grepping_flag++;return ; }@@ -1076,12 +1072,12 @@ static int handle_ctrl_cmd(char *cmd)if (cmdptr != cmd_tail) cmdptr = (cmdptr +KDB_CMD_HISTORY_COUNT -1 ) %KDB_CMD_HISTORY_COUNT ; -strscpy (cmd_cur, cmd_hist[cmdptr],CMD_BUFLEN ); +strscpy (cmd_cur, cmd_hist[cmdptr]);Same here. And other places...
Sorry, this should also be ok, BUT it's really just doing the same exact thing, right? And, it's a different thing, so it should be a different patch (i.e. do not mix different logical things in the same patch, it confuses everyone. Well, me at least...)
thanks,
greg k-h
Hi Greg,
Quoted 9 lines · Greg Kroah-Hartman
Quoted 7 lines · Greg Kroah-Hartman
kernel/debug/kdb/kdb_main.c static void parse_grep(const char *str) · line 859
Quoted 3 lines · Thorsten Blum
[...]
kernel/debug/kdb/kdb_main.c +1
-1-strcpy (kdb_grep_string, cp); +strscpy (kdb_grep_string, cp);If this was just a search/replace, it would have been done already, so why is this ok?
Quoted 5 lines
I missed that
strscpy()can now handle 2 arguments like this, so yes, this should be ok.BUT, you just checked the length above this line, which now isn't needed, right? So this function can get simpler?
Yes, this could just be
memcpy(kdb_grep_string, cp, len + 1);
because we already know the length which strscpy() would just recompute before calling memcpy() internally. I'll submit a v2.
Quoted 7 lines
Quoted 4 lines
Review
Quoted 2 lines
(quoted diff) +1
-1- strscpy(cmd_cur, cmd_hist[cmdptr], CMD_BUFLEN); + strscpy(cmd_cur, cmd_hist[cmdptr]);Same here. And other places...
Sorry, this should also be ok, BUT it's really just doing the same exact thing, right?
Yes, it's the same because sizeof(cmd_cur) equals CMD_BUFLEN.
Thanks,
Thorsten