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
216 int count;216 int count;
217 int i;217 int i;
218 int diag, dtab_count;218 int diag, dtab_count;
219 int key;219 int key, buf_size, ret;
220 220
221 221
222 diag = kdbgetintenv("DTABCOUNT", &dtab_count);222 diag = kdbgetintenv("DTABCOUNT", &dtab_count);
@@ -336,9 +336,8 @@ poll_again: · 113 unmodified lines
336 else336 else
337 p_tmp = tmpbuffer;337 p_tmp = tmpbuffer;
338 len = strlen(p_tmp);338 len = strlen(p_tmp);
339 count = kallsyms_symbol_complete(p_tmp,339 buf_size = sizeof(tmpbuffer) - (p_tmp - tmpbuffer);
340 sizeof(tmpbuffer) -340 count = kallsyms_symbol_complete(p_tmp, buf_size);
341 (p_tmp - tmpbuffer));
342 if (tab == 2 && count > 0) {341 if (tab == 2 && count > 0) {
343 kdb_printf("\n%d symbols are found.", count);342 kdb_printf("\n%d symbols are found.", count);
344 if (count > dtab_count) {343 if (count > dtab_count) {
@@ -350,9 +349,13 @@ poll_again: · 5 unmodified lines
350 }349 }
351 kdb_printf("\n");350 kdb_printf("\n");
352 for (i = 0; i < count; i++) {351 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))
354 break;354 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);
356 *(p_tmp + len) = '\0';359 *(p_tmp + len) = '\0';
357 }360 }
358 if (i >= dtab_count)361 if (i >= dtab_count)
kernel/debug/kdb/kdb_private.h+1−1
@@ -83,7 +83,7 @@ typedef struct __ksymtab { · 82 unmodified lines
83 unsigned long sym_start;83 unsigned long sym_start;
84 unsigned long sym_end;84 unsigned long sym_end;
85 } kdb_symtab_t;85 } kdb_symtab_t;
86extern int kallsyms_symbol_next(char *prefix_name, int flag);86extern int kallsyms_symbol_next(char *prefix_name, int flag, int buf_size);
87extern int kallsyms_symbol_complete(char *prefix_name, int max_len);87extern int kallsyms_symbol_complete(char *prefix_name, int max_len);
88 88
89/* Exported Symbols for kernel loadable modules to use. */89/* 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
221 * Parameters:221 * Parameters:
222 * prefix_name prefix of a symbol name to lookup222 * prefix_name prefix of a symbol name to lookup
223 * flag 0 means search from the head, 1 means continue search.223 * 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
224 * Returns:226 * Returns:
225 * 1 if a symbol matches the given prefix.227 * 1 if a symbol matches the given prefix.
226 * 0 if no string found228 * 0 if no string found
227 */229 */
228int kallsyms_symbol_next(char *prefix_name, int flag)230int kallsyms_symbol_next(char *prefix_name, int flag, int buf_size)
229{231{
230 int prefix_len = strlen(prefix_name);232 int prefix_len = strlen(prefix_name);
231 static loff_t pos;233 static loff_t pos;
@@ -235,10 +237,8 @@ int kallsyms_symbol_next(char *prefix_name, int flag) · 3 unmodified lines
235 pos = 0;237 pos = 0;
236 238
237 while ((name = kdb_walk_kallsyms(&pos))) {239 while ((name = kdb_walk_kallsyms(&pos))) {
238 if (strncmp(name, prefix_name, prefix_len) == 0) {240 if (!strncmp(name, prefix_name, prefix_len))
239 strncpy(prefix_name, name, strlen(name)+1);241 return strscpy(prefix_name, name, buf_size);
240 return 1;
241 }
242 }242 }
243 return 0;243 return 0;
244}244}