selftests/bpf: Fix file descriptor leak in load_kallsyms()

Currently, if sym_cnt > 0, it just returns and does not close file, fix it.

Signed-off-by: Yuntao Wang <ytcoode@gmail.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20220405145711.49543-1-ytcoode@gmail.com
This commit is contained in:
Yuntao Wang 2022-04-05 22:57:11 +08:00 committed by Andrii Nakryiko
parent 042152c27c
commit 2d0df01974

View File

@ -25,15 +25,12 @@ static int ksym_cmp(const void *p1, const void *p2)
int load_kallsyms(void) int load_kallsyms(void)
{ {
FILE *f = fopen("/proc/kallsyms", "r"); FILE *f;
char func[256], buf[256]; char func[256], buf[256];
char symbol; char symbol;
void *addr; void *addr;
int i = 0; int i = 0;
if (!f)
return -ENOENT;
/* /*
* This is called/used from multiplace places, * This is called/used from multiplace places,
* load symbols just once. * load symbols just once.
@ -41,6 +38,10 @@ int load_kallsyms(void)
if (sym_cnt) if (sym_cnt)
return 0; return 0;
f = fopen("/proc/kallsyms", "r");
if (!f)
return -ENOENT;
while (fgets(buf, sizeof(buf), f)) { while (fgets(buf, sizeof(buf), f)) {
if (sscanf(buf, "%p %c %s", &addr, &symbol, func) != 3) if (sscanf(buf, "%p %c %s", &addr, &symbol, func) != 3)
break; break;