Skip to content

Commit b384150

Browse files
authored
libbpf-tools/memleak: Fix to handle failure for invalid arguments (#5147)
Argument parsing errors are not detected for -p, -o, and -T due to the use of atoi. So, fixed these to handle invalid arguments by using strtol instead. Before: # ./memleak_new -p haha using default object: libc.so.6 using page size: 4096 tracing kernel: false Tracing outstanding memory allocs... Hit Ctrl-C to end # ./memleak -o 99999999999999999999999999999999999999999999999 using default object: libc.so.6 using page size: 4096 tracing kernel: true Tracing outstanding memory allocs... Hit Ctrl-C to end After: # ./memleak_new -p haha error arg:p haha Usage: memleak_new [OPTION...] Try `memleak_new --help' or `memleak_new --usage' for more information. # ./memleak_new -o 99999999999999999999999999999999999999999999999 error arg:o 99999999999999999999999999999999999999999999999 Usage: memleak_new [OPTION...] Try `memleak_new --help' or `memleak_new --usage' for more information. # ./memleak_new -o 9999999999999999 invalid AGE_MS: 9999999999999999 Usage: memleak_new [OPTION...] Try `memleak_new --help' or `memleak_new --usage' for more information.
1 parent 0985801 commit b384150

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

libbpf-tools/memleak.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ static struct env {
3535
bool trace_all;
3636
bool show_allocs;
3737
bool combined_only;
38-
int min_age_ns;
38+
long min_age_ns;
3939
uint64_t sample_rate;
4040
int top_stacks;
4141
size_t min_size;
@@ -508,10 +508,11 @@ long argp_parse_long(int key, const char *arg, struct argp_state *state)
508508
error_t argp_parse_arg(int key, char *arg, struct argp_state *state)
509509
{
510510
static int pos_args = 0;
511+
long age_ms;
511512

512513
switch (key) {
513514
case 'p':
514-
env.pid = atoi(arg);
515+
env.pid = argp_parse_long(key, arg, state);
515516
break;
516517
case 't':
517518
env.trace_all = true;
@@ -520,7 +521,12 @@ error_t argp_parse_arg(int key, char *arg, struct argp_state *state)
520521
env.show_allocs = true;
521522
break;
522523
case 'o':
523-
env.min_age_ns = 1e6 * atoi(arg);
524+
age_ms = argp_parse_long(key, arg, state);
525+
if (age_ms > (LONG_MAX / 1e6) || age_ms < (LONG_MIN / 1e6)) {
526+
fprintf(stderr, "invalid AGE_MS: %s\n", arg);
527+
argp_usage(state);
528+
}
529+
env.min_age_ns = age_ms * 1e6;
524530
break;
525531
case 'c':
526532
strncpy(env.command, arg, sizeof(env.command) - 1);
@@ -538,7 +544,7 @@ error_t argp_parse_arg(int key, char *arg, struct argp_state *state)
538544
strncpy(env.symbols_prefix, arg, sizeof(env.symbols_prefix) - 1);
539545
break;
540546
case 'T':
541-
env.top_stacks = atoi(arg);
547+
env.top_stacks = argp_parse_long(key, arg, state);
542548
break;
543549
case 'z':
544550
env.min_size = argp_parse_long(key, arg, state);

0 commit comments

Comments
 (0)