Skip to content

Commit

Permalink
selftests/bpf: Fix file descriptor leak in load_kallsyms()
Browse files Browse the repository at this point in the history
Currently, if sym_cnt > 0, it just returns and does not close file, fix it.

Signed-off-by: Yuntao Wang <[email protected]>
Signed-off-by: Andrii Nakryiko <[email protected]>
Link: https://lore.kernel.org/bpf/[email protected]
  • Loading branch information
ytcoode authored and anakryiko committed Apr 5, 2022
1 parent 042152c commit 2d0df01
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions tools/testing/selftests/bpf/trace_helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,23 @@ static int ksym_cmp(const void *p1, const void *p2)

int load_kallsyms(void)
{
FILE *f = fopen("/proc/kallsyms", "r");
FILE *f;
char func[256], buf[256];
char symbol;
void *addr;
int i = 0;

if (!f)
return -ENOENT;

/*
* This is called/used from multiplace places,
* load symbols just once.
*/
if (sym_cnt)
return 0;

f = fopen("/proc/kallsyms", "r");
if (!f)
return -ENOENT;

while (fgets(buf, sizeof(buf), f)) {
if (sscanf(buf, "%p %c %s", &addr, &symbol, func) != 3)
break;
Expand Down

0 comments on commit 2d0df01

Please sign in to comment.