diff --git a/include/bpf/libbpf.h b/include/bpf/libbpf.h index eb0d8aa684..90890060f7 100644 --- a/include/bpf/libbpf.h +++ b/include/bpf/libbpf.h @@ -247,6 +247,8 @@ bpf_object__close(struct bpf_object* object); * @param[in] name The name to look for. * * @returns The map found, or NULL if none. + * + * @exception ENOENT The map was not found. */ struct bpf_map* bpf_object__find_map_by_name(const struct bpf_object* obj, const char* name); diff --git a/libs/api/libbpf_map.cpp b/libs/api/libbpf_map.cpp index 9788f91a4a..2748c9d0f9 100644 --- a/libs/api/libbpf_map.cpp +++ b/libs/api/libbpf_map.cpp @@ -241,7 +241,7 @@ bpf_object__find_map_by_name(const struct bpf_object* obj, const char* name) return pos; } } - return NULL; + return (struct bpf_map*)libbpf_err_ptr(-ENOENT); } int diff --git a/tests/unit/libbpf_test.cpp b/tests/unit/libbpf_test.cpp index 26a757a94c..3d75eaa93f 100644 --- a/tests/unit/libbpf_test.cpp +++ b/tests/unit/libbpf_test.cpp @@ -480,9 +480,15 @@ TEST_CASE("libbpf program", "[libbpf]") struct bpf_program* program = bpf_object__find_program_by_name(object, "test_program_entry"); REQUIRE(program != nullptr); + errno = 0; REQUIRE(bpf_object__find_program_by_name(object, "not_a_valid_name") == NULL); REQUIRE(errno == ENOENT); + // Testing invalid map name. + errno = 0; + REQUIRE(bpf_object__find_map_by_name(object, "not_a_valid_map") == NULL); + REQUIRE(errno == ENOENT); + name = bpf_program__section_name(program); REQUIRE(strcmp(name, "sample_ext") == 0);