All of lore.kernel.org
 help / color / mirror / Atom feed
From: Andrii Nakryiko <andrii.nakryiko@gmail.com>
To: sdf@google.com
Cc: Wang Yufen <wangyufen@huawei.com>,
	andrii@kernel.org, ast@kernel.org,  daniel@iogearbox.net,
	martin.lau@linux.dev, song@kernel.org, yhs@fb.com,
	 john.fastabend@gmail.com, kpsingh@kernel.org, haoluo@google.com,
	 jolsa@kernel.org, paul.walmsley@sifive.com, palmer@dabbelt.com,
	 aou@eecs.berkeley.edu, davem@davemloft.net, kuba@kernel.org,
	hawk@kernel.org,  nathan@kernel.org, ndesaulniers@google.com,
	trix@redhat.com,  bpf@vger.kernel.org,
	linux-kernel@vger.kernel.org, netdev@vger.kernel.org,
	 llvm@lists.linux.dev
Subject: Re: [bpf-next 2/2] libbpf: Add pathname_concat() helper
Date: Wed, 21 Sep 2022 17:42:53 -0700	[thread overview]
Message-ID: <CAEf4Bza_449Ku65rLFG391aS6_ec-rt-sWbspveZ_nhBKn2j8w@mail.gmail.com> (raw)
In-Reply-To: <Yxt07BE7TOX6dGh2@google.com>

On Fri, Sep 9, 2022 at 10:16 AM <sdf@google.com> wrote:
>
> On 09/09, Wang Yufen wrote:
> > Move snprintf and len check to common helper pathname_concat() to make the
> > code simpler.
>
> > Signed-off-by: Wang Yufen <wangyufen@huawei.com>
> > ---
> >   tools/lib/bpf/libbpf.c | 74
> > ++++++++++++++++++--------------------------------
> >   1 file changed, 27 insertions(+), 47 deletions(-)
>
> > diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> > index 5854b92..238a03e 100644
> > --- a/tools/lib/bpf/libbpf.c
> > +++ b/tools/lib/bpf/libbpf.c
> > @@ -2096,20 +2096,31 @@ static bool get_map_field_int(const char
> > *map_name, const struct btf *btf,
> >       return true;
> >   }
>
> > -static int build_map_pin_path(struct bpf_map *map, const char *path)
> > +static int pathname_concat(const char *path, const char *name, char *buf)
> >   {
> > -     char buf[PATH_MAX];
> >       int len;
>
> > -     if (!path)
> > -             path = "/sys/fs/bpf";
> > -
> > -     len = snprintf(buf, PATH_MAX, "%s/%s", path, bpf_map__name(map));
> > +     len = snprintf(buf, PATH_MAX, "%s/%s", path, name);
> >       if (len < 0)
> >               return -EINVAL;
> >       else if (len >= PATH_MAX)
> >               return -ENAMETOOLONG;
>
> > +     return 0;
> > +}
> > +
> > +static int build_map_pin_path(struct bpf_map *map, const char *path)
> > +{
> > +     char buf[PATH_MAX];
> > +     int err;
> > +
> > +     if (!path)
> > +             path = "/sys/fs/bpf";
> > +
> > +     err = pathname_concat(path, bpf_map__name(map), buf);
> > +     if (err)
> > +             return err;
> > +
> >       return bpf_map__set_pin_path(map, buf);
> >   }
>
> > @@ -7959,17 +7970,8 @@ int bpf_object__pin_maps(struct bpf_object *obj,
> > const char *path)
> >                       continue;
>
> >               if (path) {
> > -                     int len;
> > -
> > -                     len = snprintf(buf, PATH_MAX, "%s/%s", path,
> > -                                    bpf_map__name(map));
> > -                     if (len < 0) {
> > -                             err = -EINVAL;
> > -                             goto err_unpin_maps;
> > -                     } else if (len >= PATH_MAX) {
> > -                             err = -ENAMETOOLONG;
>
> [..]
>
> > +                     if (pathname_concat(path, bpf_map__name(map), buf))
> >                               goto err_unpin_maps;
> > -                     }
>
> You're breaking error reporting here and in a bunch of other places.
> Should be:
>
> err = pathname_concat();
> if (err)
>         goto err_unpin_maps;
>
> I have the same attitude towards this patch as the first one in the
> series: not worth it. Nothing is currently broken, the code as is relatively
> readable, this version is not much simpler, it just looks slightly different
> taste-wise..
>

It's a minor improvement, IMO, so I don't mind it (5 repetitions of
annoying error case handling seems worth streamlining). But selftests
just for this seems like an overkill.

> How about this: if you really want to push this kind of cleanup, send
> selftests that exercise all these error cases? :-)
>
>

[...]

  parent reply	other threads:[~2022-09-22  0:43 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-09  5:45 [bpf-next 1/2] libbpf: Add make_path_and_pin() helper for bpf_xxx__pin() Wang Yufen
2022-09-09  5:45 ` [bpf-next 2/2] libbpf: Add pathname_concat() helper Wang Yufen
2022-09-09 17:16   ` sdf
2022-09-13  2:55     ` wangyufen
2022-09-22  0:42     ` Andrii Nakryiko [this message]
2022-09-09 17:06 ` [bpf-next 1/2] libbpf: Add make_path_and_pin() helper for bpf_xxx__pin() sdf

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAEf4Bza_449Ku65rLFG391aS6_ec-rt-sWbspveZ_nhBKn2j8w@mail.gmail.com \
    --to=andrii.nakryiko@gmail.com \
    --cc=andrii@kernel.org \
    --cc=aou@eecs.berkeley.edu \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=davem@davemloft.net \
    --cc=haoluo@google.com \
    --cc=hawk@kernel.org \
    --cc=john.fastabend@gmail.com \
    --cc=jolsa@kernel.org \
    --cc=kpsingh@kernel.org \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=llvm@lists.linux.dev \
    --cc=martin.lau@linux.dev \
    --cc=nathan@kernel.org \
    --cc=ndesaulniers@google.com \
    --cc=netdev@vger.kernel.org \
    --cc=palmer@dabbelt.com \
    --cc=paul.walmsley@sifive.com \
    --cc=sdf@google.com \
    --cc=song@kernel.org \
    --cc=trix@redhat.com \
    --cc=wangyufen@huawei.com \
    --cc=yhs@fb.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.