Skip to content

Commit 80bd73c

Browse files
author
Daniel Thompson
committed
kdb: Replace double memcpy() with memmove() in kdb_read()
At several points in kdb_read() there are variants of the following code pattern (with offsets slightly altered): memcpy(tmpbuffer, cp, lastchar - cp); memcpy(cp-1, tmpbuffer, lastchar - cp); *(--lastchar) = '\0'; There is no need to use tmpbuffer here, since we can use memmove() instead so refactor in the obvious way. Additionally the strings that are being copied are already properly terminated so let's also change the code so that the library calls also move the terminator. Changing how the terminators are managed has no functional effect for now but might allow us to retire lastchar at a later point. lastchar, although stored as a pointer, is functionally equivalent to caching strlen(buffer). Reviewed-by: Douglas Anderson <dianders@chromium.org> Tested-by: Justin Stitt <justinstitt@google.com> Link: https://lore.kernel.org/r/20240424-kgdb_read_refactor-v3-6-f236dbe9828d@linaro.org Signed-off-by: Daniel Thompson <daniel.thompson@linaro.org>
1 parent c9b51dd commit 80bd73c

File tree

1 file changed

+7
-12
lines changed

1 file changed

+7
-12
lines changed

kernel/debug/kdb/kdb_io.c

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -269,12 +269,9 @@ static char *kdb_read(char *buffer, size_t bufsize)
269269
switch (key) {
270270
case 8: /* backspace */
271271
if (cp > buffer) {
272-
if (cp < lastchar) {
273-
memcpy(tmpbuffer, cp, lastchar - cp);
274-
memcpy(cp-1, tmpbuffer, lastchar - cp);
275-
}
276-
*(--lastchar) = '\0';
277-
--cp;
272+
memmove(cp-1, cp, lastchar - cp + 1);
273+
lastchar--;
274+
cp--;
278275
kdb_printf("\b%s ", cp);
279276
kdb_position_cursor(kdb_prompt_str, buffer, cp);
280277
}
@@ -291,9 +288,8 @@ static char *kdb_read(char *buffer, size_t bufsize)
291288
return buffer;
292289
case 4: /* Del */
293290
if (cp < lastchar) {
294-
memcpy(tmpbuffer, cp+1, lastchar - cp - 1);
295-
memcpy(cp, tmpbuffer, lastchar - cp - 1);
296-
*(--lastchar) = '\0';
291+
memmove(cp, cp+1, lastchar - cp);
292+
lastchar--;
297293
kdb_printf("%s ", cp);
298294
kdb_position_cursor(kdb_prompt_str, buffer, cp);
299295
}
@@ -398,9 +394,8 @@ static char *kdb_read(char *buffer, size_t bufsize)
398394
default:
399395
if (key >= 32 && lastchar < bufend) {
400396
if (cp < lastchar) {
401-
memcpy(tmpbuffer, cp, lastchar - cp);
402-
memcpy(cp+1, tmpbuffer, lastchar - cp);
403-
*++lastchar = '\0';
397+
memmove(cp+1, cp, lastchar - cp + 1);
398+
lastchar++;
404399
*cp = key;
405400
kdb_printf("%s", cp);
406401
++cp;

0 commit comments

Comments
 (0)