Re: [PATCH bpf-next v2 1/4] libbpf: Fix error handling in bpf_map__reuse_fd()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Thu, Oct 24, 2019 at 6:11 AM Toke Høiland-Jørgensen <toke@xxxxxxxxxx> wrote:
>
> From: Toke Høiland-Jørgensen <toke@xxxxxxxxxx>
>
> 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: Toke Høiland-Jørgensen <toke@xxxxxxxxxx>
> ---

Thanks!

Acked-by: Andrii Nakryiko <andriin@xxxxxx>

>  tools/lib/bpf/libbpf.c |   14 ++++++++++----
>  1 file changed, 10 insertions(+), 4 deletions(-)
>
> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> index cccfd9355134..a2a7d074ac48 100644
> --- a/tools/lib/bpf/libbpf.c
> +++ b/tools/lib/bpf/libbpf.c
> @@ -1918,16 +1918,22 @@ int bpf_map__reuse_fd(struct bpf_map *map, int fd)
>                 return -errno;
>
>         new_fd = open("/", O_RDONLY | O_CLOEXEC);
> -       if (new_fd < 0)
> +       if (new_fd < 0) {
> +               err = -errno;
>                 goto err_free_new_name;
> +       }
>
>         new_fd = dup3(fd, new_fd, O_CLOEXEC);
> -       if (new_fd < 0)
> +       if (new_fd < 0) {
> +               err = -errno;
>                 goto err_close_new_fd;
> +       }
>
>         err = zclose(map->fd);
> -       if (err)
> +       if (err) {
> +               err = -errno;
>                 goto err_close_new_fd;
> +       }
>         free(map->name);
>
>         map->fd = new_fd;
> @@ -1946,7 +1952,7 @@ int bpf_map__reuse_fd(struct bpf_map *map, int fd)
>         close(new_fd);
>  err_free_new_name:
>         free(new_name);
> -       return -errno;
> +       return err;
>  }
>
>  int bpf_map__resize(struct bpf_map *map, __u32 max_entries)
>




[Index of Archives]     [Linux Samsung SoC]     [Linux Rockchip SoC]     [Linux Actions SoC]     [Linux for Synopsys ARC Processors]     [Linux NFS]     [Linux NILFS]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]


  Powered by Linux