diff mbox series

[bpf-next,1/2] libbpf: Use IS_ERR_OR_NULL() in hashmap__free()

Message ID 20220107152620.192327-1-mauricio@kinvolk.io (mailing list archive)
State Accepted
Commit d793c2eb5dbc015bfb91b63d417049e065d1b33c
Delegated to: BPF
Headers show
Series [bpf-next,1/2] libbpf: Use IS_ERR_OR_NULL() in hashmap__free() | expand

Checks

Context Check Description
bpf/vmtest-bpf-next-PR pending PR summary
netdev/tree_selection success Clearly marked for bpf-next
netdev/fixes_present success Fixes tag not required for -next series
netdev/subject_prefix success Link
netdev/cover_letter success Single patches do not need cover letters
netdev/patch_count success Link
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 0 this patch: 0
netdev/cc_maintainers warning 5 maintainers not CCed: kpsingh@kernel.org john.fastabend@gmail.com kafai@fb.com songliubraving@fb.com yhs@fb.com
netdev/build_clang success Errors and warnings before: 0 this patch: 0
netdev/module_param success Was 0 now: 0
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 0 this patch: 0
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 11 lines checked
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
bpf/vmtest-bpf-next pending VM_Test

Commit Message

Mauricio Vásquez Jan. 7, 2022, 3:26 p.m. UTC
hashmap__new() uses ERR_PTR() to return an error so it's better to
use IS_ERR_OR_NULL() in order to check the pointer before calling
free(). This will prevent freeing an invalid pointer if somebody calls
hashmap__free() with the result of a failed hashmap__new() call.

Signed-off-by: Mauricio Vásquez <mauricio@kinvolk.io>
---
 tools/lib/bpf/hashmap.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

Comments

Song Liu Jan. 7, 2022, 5:40 p.m. UTC | #1
On Fri, Jan 7, 2022 at 7:26 AM Mauricio Vásquez <mauricio@kinvolk.io> wrote:
>
> hashmap__new() uses ERR_PTR() to return an error so it's better to
> use IS_ERR_OR_NULL() in order to check the pointer before calling
> free(). This will prevent freeing an invalid pointer if somebody calls
> hashmap__free() with the result of a failed hashmap__new() call.
>
> Signed-off-by: Mauricio Vásquez <mauricio@kinvolk.io>

Acked-by: Song Liu <songliubraving@fb.com>
patchwork-bot+netdevbpf@kernel.org Jan. 7, 2022, 10 p.m. UTC | #2
Hello:

This series was applied to bpf/bpf-next.git (master)
by Andrii Nakryiko <andrii@kernel.org>:

On Fri,  7 Jan 2022 10:26:19 -0500 you wrote:
> hashmap__new() uses ERR_PTR() to return an error so it's better to
> use IS_ERR_OR_NULL() in order to check the pointer before calling
> free(). This will prevent freeing an invalid pointer if somebody calls
> hashmap__free() with the result of a failed hashmap__new() call.
> 
> Signed-off-by: Mauricio Vásquez <mauricio@kinvolk.io>
> 
> [...]

Here is the summary with links:
  - [bpf-next,1/2] libbpf: Use IS_ERR_OR_NULL() in hashmap__free()
    https://git.kernel.org/bpf/bpf-next/c/d793c2eb5dbc
  - [bpf-next,2/2] bpftool: Fix error check when calling hashmap__new()
    https://git.kernel.org/bpf/bpf-next/c/2318517920d1

You are awesome, thank you!
diff mbox series

Patch

diff --git a/tools/lib/bpf/hashmap.c b/tools/lib/bpf/hashmap.c
index 3c20b126d60d..aeb09c288716 100644
--- a/tools/lib/bpf/hashmap.c
+++ b/tools/lib/bpf/hashmap.c
@@ -75,7 +75,7 @@  void hashmap__clear(struct hashmap *map)
 
 void hashmap__free(struct hashmap *map)
 {
-	if (!map)
+	if (IS_ERR_OR_NULL(map))
 		return;
 
 	hashmap__clear(map);
@@ -238,4 +238,3 @@  bool hashmap__delete(struct hashmap *map, const void *key,
 
 	return true;
 }
-