]> www.infradead.org Git - users/dwmw2/linux.git/commitdiff
bpftool: Fix error check when calling hashmap__new()
authorMauricio Vásquez <mauricio@kinvolk.io>
Fri, 7 Jan 2022 15:26:20 +0000 (10:26 -0500)
committerAndrii Nakryiko <andrii@kernel.org>
Thu, 13 Jan 2022 01:01:36 +0000 (17:01 -0800)
hashmap__new() encodes errors with ERR_PTR(), hence it's not valid to
check the returned pointer against NULL and IS_ERR() has to be used
instead.

libbpf_get_error() can't be used in this case as hashmap__new() is not
part of the public libbpf API and it'll continue using ERR_PTR() after
libbpf 1.0.

Fixes: 8f184732b60b ("bpftool: Switch to libbpf's hashmap for pinned paths of BPF objects")
Fixes: 2828d0d75b73 ("bpftool: Switch to libbpf's hashmap for programs/maps in BTF listing")
Fixes: d6699f8e0f83 ("bpftool: Switch to libbpf's hashmap for PIDs/names references")
Signed-off-by: Mauricio Vásquez <mauricio@kinvolk.io>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Reviewed-by: Quentin Monnet <quentin@isovalent.com>
Acked-by: Song Liu <songliubraving@fb.com>
Link: https://lore.kernel.org/bpf/20220107152620.192327-2-mauricio@kinvolk.io
tools/bpf/bpftool/btf.c
tools/bpf/bpftool/link.c
tools/bpf/bpftool/map.c
tools/bpf/bpftool/pids.c
tools/bpf/bpftool/prog.c

index 59833125ac0a13a55f8c54e5a52b3082424000f8..a2c665beda87c2862404a347c2ddffa71e4a48fe 100644 (file)
@@ -902,7 +902,7 @@ static int do_show(int argc, char **argv)
                                      equal_fn_for_key_as_id, NULL);
        btf_map_table = hashmap__new(hash_fn_for_key_as_id,
                                     equal_fn_for_key_as_id, NULL);
-       if (!btf_prog_table || !btf_map_table) {
+       if (IS_ERR(btf_prog_table) || IS_ERR(btf_map_table)) {
                hashmap__free(btf_prog_table);
                hashmap__free(btf_map_table);
                if (fd >= 0)
index 2c258db0d3521a1224c6f2e479d302eaf8fcfeef..97dec81950e5dead2f5eef76a514f7beb18f7ee0 100644 (file)
@@ -2,6 +2,7 @@
 /* Copyright (C) 2020 Facebook */
 
 #include <errno.h>
+#include <linux/err.h>
 #include <net/if.h>
 #include <stdio.h>
 #include <unistd.h>
@@ -306,7 +307,7 @@ static int do_show(int argc, char **argv)
        if (show_pinned) {
                link_table = hashmap__new(hash_fn_for_key_as_id,
                                          equal_fn_for_key_as_id, NULL);
-               if (!link_table) {
+               if (IS_ERR(link_table)) {
                        p_err("failed to create hashmap for pinned paths");
                        return -1;
                }
index cc530a2298124bf6b588484bcb951830c7e54f05..c66a3c979b7a7cf43ef6d1348fe9d2db9996997f 100644 (file)
@@ -699,7 +699,7 @@ static int do_show(int argc, char **argv)
        if (show_pinned) {
                map_table = hashmap__new(hash_fn_for_key_as_id,
                                         equal_fn_for_key_as_id, NULL);
-               if (!map_table) {
+               if (IS_ERR(map_table)) {
                        p_err("failed to create hashmap for pinned paths");
                        return -1;
                }
index 56b598eee043a94d89d31344e4c2113ffc430d4a..7c384d10e95f83d5dd27e2170867c98717ece58c 100644 (file)
@@ -1,6 +1,7 @@
 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
 /* Copyright (C) 2020 Facebook */
 #include <errno.h>
+#include <linux/err.h>
 #include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -101,7 +102,7 @@ int build_obj_refs_table(struct hashmap **map, enum bpf_obj_type type)
        libbpf_print_fn_t default_print;
 
        *map = hashmap__new(hash_fn_for_key_as_id, equal_fn_for_key_as_id, NULL);
-       if (!*map) {
+       if (IS_ERR(*map)) {
                p_err("failed to create hashmap for PID references");
                return -1;
        }
index 2a21d50516bc46ae06947ef6c35e04194685d7c6..33ca834d5f510ae4176e99908165df079cd32e62 100644 (file)
@@ -641,7 +641,7 @@ static int do_show(int argc, char **argv)
        if (show_pinned) {
                prog_table = hashmap__new(hash_fn_for_key_as_id,
                                          equal_fn_for_key_as_id, NULL);
-               if (!prog_table) {
+               if (IS_ERR(prog_table)) {
                        p_err("failed to create hashmap for pinned paths");
                        return -1;
                }