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
239 * and null byte */239 * and null byte */
240 char *lastchar;240 char *lastchar;
241 char *p_tmp;241 char *p_tmp;
242 char tmp;
242 static char tmpbuffer[CMD_BUFLEN];243 static char tmpbuffer[CMD_BUFLEN];
243 int len = strlen(buffer);244 int len = strlen(buffer);
244 int len_tmp;245 int len_tmp;
@@ -246,8 +247,7 @@ static char *kdb_read(char *buffer, size_t bufsize) · 1 unmodified line
246 int count;247 int count;
247 int i;248 int i;
248 int diag, dtab_count;249 int diag, dtab_count;
249 int key, buf_size, ret;250 int key, ret;
250
251 251
252 diag = kdbgetintenv("DTABCOUNT", &dtab_count);252 diag = kdbgetintenv("DTABCOUNT", &dtab_count);
253 if (diag)253 if (diag)
@@ -329,21 +329,16 @@ poll_again: · 75 unmodified lines
329 case 9: /* Tab */329 case 9: /* Tab */
330 if (tab < 2)330 if (tab < 2)
331 ++tab;331 ++tab;
332 p_tmp = buffer;332
333 while (*p_tmp == ' ')333 tmp = *cp;
334 p_tmp++;334 *cp = '\0';
335 if (p_tmp > cp)335 p_tmp = strrchr(buffer, ' ');
336 break;336 p_tmp = (p_tmp ? p_tmp + 1 : buffer);
337 memcpy(tmpbuffer, p_tmp, cp-p_tmp);337 strscpy(tmpbuffer, p_tmp, sizeof(tmpbuffer));
338 *(tmpbuffer + (cp-p_tmp)) = '\0';338 *cp = tmp;
339 p_tmp = strrchr(tmpbuffer, ' ');339
340 if (p_tmp)340 len = strlen(tmpbuffer);
341 ++p_tmp;341 count = kallsyms_symbol_complete(tmpbuffer, sizeof(tmpbuffer));
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);
347 if (tab == 2 && count > 0) {342 if (tab == 2 && count > 0) {
348 kdb_printf("\n%d symbols are found.", count);343 kdb_printf("\n%d symbols are found.", count);
349 if (count > dtab_count) {344 if (count > dtab_count) {
@@ -355,14 +350,14 @@ poll_again: · 5 unmodified lines
355 }350 }
356 kdb_printf("\n");351 kdb_printf("\n");
357 for (i = 0; i < count; i++) {352 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));
359 if (WARN_ON(!ret))354 if (WARN_ON(!ret))
360 break;355 break;
361 if (ret != -E2BIG)356 if (ret != -E2BIG)
362 kdb_printf("%s ", p_tmp);357 kdb_printf("%s ", tmpbuffer);
363 else358 else
364 kdb_printf("%s... ", p_tmp);359 kdb_printf("%s... ", tmpbuffer);
365 *(p_tmp + len) = '\0';360 tmpbuffer[len] = '\0';
366 }361 }
367 if (i >= dtab_count)362 if (i >= dtab_count)
368 kdb_printf("...");363 kdb_printf("...");
@@ -373,14 +368,14 @@ poll_again: · 4 unmodified lines
373 kdb_position_cursor(kdb_prompt_str, buffer, cp);368 kdb_position_cursor(kdb_prompt_str, buffer, cp);
374 } else if (tab != 2 && count > 0) {369 } else if (tab != 2 && count > 0) {
375 /* How many new characters do we want from tmpbuffer? */370 /* How many new characters do we want from tmpbuffer? */
376 len_tmp = strlen(p_tmp) - len;371 len_tmp = strlen(tmpbuffer) - len;
377 if (lastchar + len_tmp >= bufend)372 if (lastchar + len_tmp >= bufend)
378 len_tmp = bufend - lastchar;373 len_tmp = bufend - lastchar;
379 374
380 if (len_tmp) {375 if (len_tmp) {
381 /* + 1 ensures the '\0' is memmove'd */376 /* + 1 ensures the '\0' is memmove'd */
382 memmove(cp+len_tmp, cp, (lastchar-cp) + 1);377 memmove(cp+len_tmp, cp, (lastchar-cp) + 1);
383 memcpy(cp, p_tmp+len, len_tmp);378 memcpy(cp, tmpbuffer+len, len_tmp);
384 kdb_printf("%s", cp);379 kdb_printf("%s", cp);
385 cp += len_tmp;380 cp += len_tmp;
386 lastchar += len_tmp;381 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