History

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)/

Daniel Thompson committed 2 years ago

Commit 64d504c1 parent 80bd73c1 file · +18 −23Browse files

Showing the changes to kernel/debug (1 of 1 file). Show all

Unified · Split

kernel/debug/kdb/kdb_io.c+18−23
@@ -239,6 +239,7 @@ static char *kdb_read(char *buffer, size_t bufsize) · 238 unmodified lines
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;
@@ -246,8 +247,7 @@ static char *kdb_read(char *buffer, size_t bufsize) · 1 unmodified line
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 @@ poll_again: · 75 unmodified lines
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 @@ poll_again: · 5 unmodified lines
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 @@ poll_again: · 4 unmodified lines
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;

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t