2020-11-06 08:51:33

by 王擎

[permalink] [raw]
Subject: [PATCH] trace: Fix passing zero to 'PTR_ERR' warning

Fix smatch warning.

Signed-off-by: Wang Qing <[email protected]>
---
kernel/trace/bpf_trace.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c
index 4517c8b..2cb9c45
--- a/kernel/trace/bpf_trace.c
+++ b/kernel/trace/bpf_trace.c
@@ -1198,7 +1198,7 @@ static int bpf_btf_printf_prepare(struct btf_ptr *ptr, u32 btf_ptr_size,
*btf = bpf_get_btf_vmlinux();

if (IS_ERR_OR_NULL(*btf))
- return PTR_ERR(*btf);
+ return PTR_ERR_OR_ZERO(*btf);

if (ptr->type_id > 0)
*btf_id = ptr->type_id;
--
2.7.4


2020-11-06 20:43:05

by Andrii Nakryiko

[permalink] [raw]
Subject: Re: [PATCH] trace: Fix passing zero to 'PTR_ERR' warning

On Fri, Nov 6, 2020 at 12:49 AM Wang Qing <[email protected]> wrote:
>
> Fix smatch warning.
>
> Signed-off-by: Wang Qing <[email protected]>
> ---
> kernel/trace/bpf_trace.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c
> index 4517c8b..2cb9c45
> --- a/kernel/trace/bpf_trace.c
> +++ b/kernel/trace/bpf_trace.c
> @@ -1198,7 +1198,7 @@ static int bpf_btf_printf_prepare(struct btf_ptr *ptr, u32 btf_ptr_size,
> *btf = bpf_get_btf_vmlinux();
>
> if (IS_ERR_OR_NULL(*btf))
> - return PTR_ERR(*btf);
> + return PTR_ERR_OR_ZERO(*btf);

Either way returns zero for error? Which is the actual bug, so I think
the proper fix is:

return *btf ? PTR_ERR(*btf) : -EINVAL;

Or something like that.

>
> if (ptr->type_id > 0)
> *btf_id = ptr->type_id;
> --
> 2.7.4
>