On Wed, Dec 13, 2023 at 11:51 PM Wentao Zhang <wentao.zhang@xxxxxxxxxxxxx> wrote: > > The function btf_str_by_offset may return NULL when used as an > input argument for btf_add_str in the context of btf_rewrite_str. > The added check ensures that both the input string (s) and the > BTF object (btf) are non-null before proceeding with the function > logic. If either is null, the function returns an error code > indicating an invalid argument. > > Found by our static analysis tool. > > Signed-off-by: Wentao Zhang <wentao.zhang@xxxxxxxxxxxxx> > --- > tools/lib/bpf/btf.c | 2 ++ > 1 file changed, 2 insertions(+) > > diff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c > index fd2309512978..a6a00bdc7151 100644 > --- a/tools/lib/bpf/btf.c > +++ b/tools/lib/bpf/btf.c > @@ -1612,6 +1612,8 @@ int btf__find_str(struct btf *btf, const char *s) > int btf__add_str(struct btf *btf, const char *s) > { > int off; > + if(!s || !btf) > + return libbpf_err(-EINVAL); > while for some old libbpf code we sometimes did NULL validation, generally speaking for all new code we assume that passed objects are non-NULL, unless otherwise explicitly pointed out in API for cases where NULL is actually meaningful (like no options are specified, etc). In this case, both btf and s are expected to be non-NULL by the API and NULL values for either btf or s are meaningless. So I don't think we need to check these conditions. > if (btf->base_btf) { > off = btf__find_str(btf->base_btf, s); > -- > 2.35.5 > >