diff mbox series

[bpf-next,v3,2/2] libbpf: Fix the possible memory leak caused by obj->kconfig

Message ID 1625798873-55442-3-git-send-email-chengshuyi@linux.alibaba.com
State Superseded
Headers show
Series libbpf: Introduce 'btf_custom_path' to 'bpf_obj_open_opts' | expand

Commit Message

Shuyi Cheng July 9, 2021, 2:47 a.m. UTC
When obj->kconfig is NULL, ERR_PTR(-ENOMEM) should not be returned
directly, err=-ENOMEM should be set, and then goto out.

Signed-off-by: Shuyi Cheng <chengshuyi@linux.alibaba.com>
---
 tools/lib/bpf/libbpf.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

Comments

Dan Carpenter July 10, 2021, 2:42 p.m. UTC | #1
On Fri, Jul 09, 2021 at 10:47:53AM +0800, Shuyi Cheng wrote:
> When obj->kconfig is NULL, ERR_PTR(-ENOMEM) should not be returned

> directly, err=-ENOMEM should be set, and then goto out.

> 


The commit message needs to say what the problem is that the patch is
fixing.  Here is a better commit message:

[PATCH bpf-next v3 2/2] libbpf: Fix the possible memory leak on error

If the strdup() fails then we need to call bpf_object__close(obj) to
avoid a resource leak.

Add a Fixes tag as well.

regards,
dan carpenter
Shuyi Cheng July 11, 2021, 1:27 a.m. UTC | #2
On 7/10/21 10:42 PM, Dan Carpenter wrote:
> On Fri, Jul 09, 2021 at 10:47:53AM +0800, Shuyi Cheng wrote:

>> When obj->kconfig is NULL, ERR_PTR(-ENOMEM) should not be returned

>> directly, err=-ENOMEM should be set, and then goto out.

>>

> 

> The commit message needs to say what the problem is that the patch is

> fixing.  Here is a better commit message:

> 

> [PATCH bpf-next v3 2/2] libbpf: Fix the possible memory leak on error

> 

> If the strdup() fails then we need to call bpf_object__close(obj) to

> avoid a resource leak.

> 

> Add a Fixes tag as well.


Agree, Thanks.

After Andrii reviews the patch, I will resend a new patch.

regards,
Shuyi

> 

> regards,

> dan carpenter

>
diff mbox series

Patch

diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 6702b7f..5e550e7 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -7626,8 +7626,10 @@  int bpf_program__load(struct bpf_program *prog, char *license, __u32 kern_ver)
 	kconfig = OPTS_GET(opts, kconfig, NULL);
 	if (kconfig) {
 		obj->kconfig = strdup(kconfig);
-		if (!obj->kconfig)
-			return ERR_PTR(-ENOMEM);
+		if (!obj->kconfig) {
+			err = -ENOMEM;
+			goto out;
+		}
 	}
 
 	err = bpf_object__elf_init(obj);