Commit d1b4574a authored by Toke Høiland-Jørgensen's avatar Toke Høiland-Jørgensen Committed by Alexei Starovoitov

libbpf: Fix error handling in bpf_map__reuse_fd()

bpf_map__reuse_fd() was calling close() in the error path before returning
an error value based on errno. However, close can change errno, so that can
lead to potentially misleading error messages. Instead, explicitly store
errno in the err variable before each goto.
Signed-off-by: default avatarToke Høiland-Jørgensen <toke@redhat.com>
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
Acked-by: default avatarAndrii Nakryiko <andriin@fb.com>
Link: https://lore.kernel.org/bpf/157269297769.394725.12634985106772698611.stgit@toke.dk
parent 78db77fa
...@@ -1917,16 +1917,22 @@ int bpf_map__reuse_fd(struct bpf_map *map, int fd) ...@@ -1917,16 +1917,22 @@ int bpf_map__reuse_fd(struct bpf_map *map, int fd)
return -errno; return -errno;
new_fd = open("/", O_RDONLY | O_CLOEXEC); new_fd = open("/", O_RDONLY | O_CLOEXEC);
if (new_fd < 0) if (new_fd < 0) {
err = -errno;
goto err_free_new_name; goto err_free_new_name;
}
new_fd = dup3(fd, new_fd, O_CLOEXEC); new_fd = dup3(fd, new_fd, O_CLOEXEC);
if (new_fd < 0) if (new_fd < 0) {
err = -errno;
goto err_close_new_fd; goto err_close_new_fd;
}
err = zclose(map->fd); err = zclose(map->fd);
if (err) if (err) {
err = -errno;
goto err_close_new_fd; goto err_close_new_fd;
}
free(map->name); free(map->name);
map->fd = new_fd; map->fd = new_fd;
...@@ -1945,7 +1951,7 @@ int bpf_map__reuse_fd(struct bpf_map *map, int fd) ...@@ -1945,7 +1951,7 @@ int bpf_map__reuse_fd(struct bpf_map *map, int fd)
close(new_fd); close(new_fd);
err_free_new_name: err_free_new_name:
free(new_name); free(new_name);
return -errno; return err;
} }
int bpf_map__resize(struct bpf_map *map, __u32 max_entries) int bpf_map__resize(struct bpf_map *map, __u32 max_entries)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment