tools: libbpf: handle NULL program gracefully in bpf_program__nth_fd()
authorJakub Kicinski <jakub.kicinski@netronome.com>
Thu, 26 Jul 2018 21:32:18 +0000 (14:32 -0700)
committerDaniel Borkmann <daniel@iogearbox.net>
Fri, 27 Jul 2018 05:18:44 +0000 (07:18 +0200)
bpf_map__fd() handles NULL map gracefully and returns -EINVAL.
bpf_program__fd() and bpf_program__nth_fd() crash in this case.
Make the behaviour more consistent by validating prog pointer
as well.

Signed-off-by: Jakub Kicinski <jakub.kicinski@netronome.com>
Reviewed-by: Quentin Monnet <quentin.monnet@netronome.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
tools/lib/bpf/libbpf.c

index 955f8eafbf41e516810662b2c42e4556d800192f..afa9860db755d904a12cd8a4f7ff640c834691b9 100644 (file)
@@ -1991,6 +1991,9 @@ int bpf_program__nth_fd(struct bpf_program *prog, int n)
 {
        int fd;
 
+       if (!prog)
+               return -EINVAL;
+
        if (n >= prog->instances.nr || n < 0) {
                pr_warning("Can't get the %dth fd from program %s: only %d instances\n",
                           n, prog->section_name, prog->instances.nr);