From 54632eb360c560ebef2f173be1a4a4625d540744 Mon Sep 17 00:00:00 2001 From: Sharmi Date: Tue, 23 Jul 2024 11:32:28 -0700 Subject: [PATCH] Set errno for bpf_object__find_map_by_name (#3712) * Initial Commit * Initialize errno * Set errno for bpf_object__find_map_by_name * Added Doxygen comment --- include/bpf/libbpf.h | 2 ++ libs/api/libbpf_map.cpp | 2 +- tests/unit/libbpf_test.cpp | 6 ++++++ 3 files changed, 9 insertions(+), 1 deletion(-) 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);