Skip to content

Commit 64d504c

Browse files
author
Daniel Thompson
committed
kdb: Simplify management of tmpbuffer in kdb_read()
The current approach to filling tmpbuffer with completion candidates is confusing, with the buffer management being especially hard to reason about. That's because it doesn't copy the completion canidate into tmpbuffer, instead of copies a whole bunch of other nonsense and then runs the completion search from the middle of tmpbuffer! Change this to copy nothing but the completion candidate into tmpbuffer. Pretty much everything else in this patch is renaming to reflect the above change: s/p_tmp/tmpbuffer/ s/buf_size/sizeof(tmpbuffer)/ 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-7-f236dbe9828d@linaro.org Signed-off-by: Daniel Thompson <daniel.thompson@linaro.org>
1 parent 80bd73c commit 64d504c

File tree

1 file changed

+18
-23
lines changed

1 file changed

+18
-23
lines changed

kernel/debug/kdb/kdb_io.c

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -239,15 +239,15 @@ static char *kdb_read(char *buffer, size_t bufsize)
239239
* and null byte */
240240
char *lastchar;
241241
char *p_tmp;
242+
char tmp;
242243
static char tmpbuffer[CMD_BUFLEN];
243244
int len = strlen(buffer);
244245
int len_tmp;
245246
int tab = 0;
246247
int count;
247248
int i;
248249
int diag, dtab_count;
249-
int key, buf_size, ret;
250-
250+
int key, ret;
251251

252252
diag = kdbgetintenv("DTABCOUNT", &dtab_count);
253253
if (diag)
@@ -329,21 +329,16 @@ static char *kdb_read(char *buffer, size_t bufsize)
329329
case 9: /* Tab */
330330
if (tab < 2)
331331
++tab;
332-
p_tmp = buffer;
333-
while (*p_tmp == ' ')
334-
p_tmp++;
335-
if (p_tmp > cp)
336-
break;
337-
memcpy(tmpbuffer, p_tmp, cp-p_tmp);
338-
*(tmpbuffer + (cp-p_tmp)) = '\0';
339-
p_tmp = strrchr(tmpbuffer, ' ');
340-
if (p_tmp)
341-
++p_tmp;
342-
else
343-
p_tmp = tmpbuffer;
344-
len = strlen(p_tmp);
345-
buf_size = sizeof(tmpbuffer) - (p_tmp - tmpbuffer);
346-
count = kallsyms_symbol_complete(p_tmp, buf_size);
332+
333+
tmp = *cp;
334+
*cp = '\0';
335+
p_tmp = strrchr(buffer, ' ');
336+
p_tmp = (p_tmp ? p_tmp + 1 : buffer);
337+
strscpy(tmpbuffer, p_tmp, sizeof(tmpbuffer));
338+
*cp = tmp;
339+
340+
len = strlen(tmpbuffer);
341+
count = kallsyms_symbol_complete(tmpbuffer, sizeof(tmpbuffer));
347342
if (tab == 2 && count > 0) {
348343
kdb_printf("\n%d symbols are found.", count);
349344
if (count > dtab_count) {
@@ -355,14 +350,14 @@ static char *kdb_read(char *buffer, size_t bufsize)
355350
}
356351
kdb_printf("\n");
357352
for (i = 0; i < count; i++) {
358-
ret = kallsyms_symbol_next(p_tmp, i, buf_size);
353+
ret = kallsyms_symbol_next(tmpbuffer, i, sizeof(tmpbuffer));
359354
if (WARN_ON(!ret))
360355
break;
361356
if (ret != -E2BIG)
362-
kdb_printf("%s ", p_tmp);
357+
kdb_printf("%s ", tmpbuffer);
363358
else
364-
kdb_printf("%s... ", p_tmp);
365-
*(p_tmp + len) = '\0';
359+
kdb_printf("%s... ", tmpbuffer);
360+
tmpbuffer[len] = '\0';
366361
}
367362
if (i >= dtab_count)
368363
kdb_printf("...");
@@ -373,14 +368,14 @@ static char *kdb_read(char *buffer, size_t bufsize)
373368
kdb_position_cursor(kdb_prompt_str, buffer, cp);
374369
} else if (tab != 2 && count > 0) {
375370
/* How many new characters do we want from tmpbuffer? */
376-
len_tmp = strlen(p_tmp) - len;
371+
len_tmp = strlen(tmpbuffer) - len;
377372
if (lastchar + len_tmp >= bufend)
378373
len_tmp = bufend - lastchar;
379374

380375
if (len_tmp) {
381376
/* + 1 ensures the '\0' is memmove'd */
382377
memmove(cp+len_tmp, cp, (lastchar-cp) + 1);
383-
memcpy(cp, p_tmp+len, len_tmp);
378+
memcpy(cp, tmpbuffer+len, len_tmp);
384379
kdb_printf("%s", cp);
385380
cp += len_tmp;
386381
lastchar += len_tmp;

0 commit comments

Comments
 (0)