History

kdb: Use strscpy with destination buffer size

gcc 8.1.0 warns with: kernel/debug/kdb/kdb_support.c: In function ‘kallsyms_symbol_next’: kernel/debug/kdb/kdb_support.c:239:4: warning: ‘strncpy’ specified bound depends on the length of the source argument [-Wstringop-overflow=] strncpy(prefix_name, name, strlen(name)+1); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ kernel/debug/kdb/kdb_support.c:239:31: note: length computed here Use strscpy() with the destination buffer size, and use ellipses when displaying truncated symbols. v2: Use strscpy()

Prarit Bhargava committed 8 years ago

Commit c2b94c71 parent 568fb6f3 files · +15 −12Browse files

Showing the changes to kernel/debug (3 of 3 files). Show all

Unified · Split

3 files
  1. kernel/debug/kdb/kdb_io.c +9 −6
  2. kernel/debug/kdb/kdb_private.h +1 −1
  3. kernel/debug/kdb/kdb_support.c +5 −5
kernel/debug/kdb/kdb_io.c+9−6
@@ -216,7 +216,7 @@ static char *kdb_read(char *buffer, size_t bufsize) · 215 unmodified lines
216216 int count;
217217 int i;
218218 int diag, dtab_count;
219− int key;
219+ int key, buf_size, ret;
220220
221221
222222 diag = kdbgetintenv("DTABCOUNT", &dtab_count);
@@ -336,9 +336,8 @@ poll_again: · 113 unmodified lines
336336 else
337337 p_tmp = tmpbuffer;
338338 len = strlen(p_tmp);
339− count = kallsyms_symbol_complete(p_tmp,
340− sizeof(tmpbuffer) -
341− (p_tmp - tmpbuffer));
339+ buf_size = sizeof(tmpbuffer) - (p_tmp - tmpbuffer);
340+ count = kallsyms_symbol_complete(p_tmp, buf_size);
342341 if (tab == 2 && count > 0) {
343342 kdb_printf("\n%d symbols are found.", count);
344343 if (count > dtab_count) {
@@ -350,9 +349,13 @@ poll_again: · 5 unmodified lines
350349 }
351350 kdb_printf("\n");
352351 for (i = 0; i < count; i++) {
353− if (WARN_ON(!kallsyms_symbol_next(p_tmp, i)))
352+ ret = kallsyms_symbol_next(p_tmp, i, buf_size);
353+ if (WARN_ON(!ret))
354354 break;
355− kdb_printf("%s ", p_tmp);
355+ if (ret != -E2BIG)
356+ kdb_printf("%s ", p_tmp);
357+ else
358+ kdb_printf("%s... ", p_tmp);
356359 *(p_tmp + len) = '\0';
357360 }
358361 if (i >= dtab_count)
kernel/debug/kdb/kdb_private.h+1−1
@@ -83,7 +83,7 @@ typedef struct __ksymtab { · 82 unmodified lines
8383 unsigned long sym_start;
8484 unsigned long sym_end;
8585 } kdb_symtab_t;
86−extern int kallsyms_symbol_next(char *prefix_name, int flag);
86+extern int kallsyms_symbol_next(char *prefix_name, int flag, int buf_size);
8787 extern int kallsyms_symbol_complete(char *prefix_name, int max_len);
8888
8989 /* Exported Symbols for kernel loadable modules to use. */
kernel/debug/kdb/kdb_support.c+5−5
@@ -221,11 +221,13 @@ int kallsyms_symbol_complete(char *prefix_name, int max_len) · 220 unmodified lines
221221 * Parameters:
222222 * prefix_name prefix of a symbol name to lookup
223223 * flag 0 means search from the head, 1 means continue search.
224+ * buf_size maximum length that can be written to prefix_name
225+ * buffer
224226 * Returns:
225227 * 1 if a symbol matches the given prefix.
226228 * 0 if no string found
227229 */
228−int kallsyms_symbol_next(char *prefix_name, int flag)
230+int kallsyms_symbol_next(char *prefix_name, int flag, int buf_size)
229231 {
230232 int prefix_len = strlen(prefix_name);
231233 static loff_t pos;
@@ -235,10 +237,8 @@ int kallsyms_symbol_next(char *prefix_name, int flag) · 3 unmodified lines
235237 pos = 0;
236238
237239 while ((name = kdb_walk_kallsyms(&pos))) {
238− if (strncmp(name, prefix_name, prefix_len) == 0) {
239− strncpy(prefix_name, name, strlen(name)+1);
240− return 1;
241− }
240+ if (!strncmp(name, prefix_name, prefix_len))
241+ return strscpy(prefix_name, name, buf_size);
242242 }
243243 return 0;
244244 }