mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Daniel Borkmann <daniel@iogearbox.net>
To: Wang Yufen <wangyufen@huawei.com>,
	andrii@kernel.org, ast@kernel.org, martin.lau@linux.dev,
	song@kernel.org, yhs@fb.com, john.fastabend@gmail.com,
	kpsingh@kernel.org, sdf@google.com, 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
Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org,
	netdev@vger.kernel.org, llvm@lists.linux.dev
Subject: Re: [bpf-next v2 1/2] libbpf: Add pathname_concat() helper
Date: Fri, 16 Sep 2022 17:53:37 +0200	[thread overview]
Message-ID: <8011ae39-d6ba-3dfa-07f1-1a548fd24560@iogearbox.net> (raw)
In-Reply-To: <1663313820-29918-1-git-send-email-wangyufen@huawei.com>

On 9/16/22 9:36 AM, 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 | 76 +++++++++++++++++++-------------------------------
>   1 file changed, 29 insertions(+), 47 deletions(-)
> 
> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> index 3ad1392..7ab977c 100644
> --- a/tools/lib/bpf/libbpf.c
> +++ b/tools/lib/bpf/libbpf.c
> @@ -2096,19 +2096,30 @@ static bool get_map_field_int(const char *map_name, const struct btf *btf,
>   	return true;
>   }
>   
> +static int pathname_concat(const char *path, const char *name, char *buf)
> +{
> +	int len;
> +
> +	len = snprintf(buf, PATH_MAX, "%s/%s", path, name);
> +	if (len < 0)
> +		return -EINVAL;
> +	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 len;
> +	int err;
>   
>   	if (!path)
>   		path = "/sys/fs/bpf";
>   
> -	len = snprintf(buf, PATH_MAX, "%s/%s", path, bpf_map__name(map));
> -	if (len < 0)
> -		return -EINVAL;
> -	else if (len >= PATH_MAX)
> -		return -ENAMETOOLONG;
> +	err = pathname_concat(path, bpf_map__name(map), buf);

Small nit, but would be good to not make the assumption that buf is always
PATH_MAX so lets add size_t len to pathname_concat().

> +	if (err)
> +		return err;
>   
>   	return bpf_map__set_pin_path(map, buf);
>   }

      parent reply	other threads:[~2022-09-16 15:54 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-16  7:36 Wang Yufen
2022-09-16  7:37 ` [bpf-next v2 2/2] selftests/bpf: Add testcases for pinning to errpath Wang Yufen
2022-09-16 15:53 ` Daniel Borkmann [this message]

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=8011ae39-d6ba-3dfa-07f1-1a548fd24560@iogearbox.net \
    --to=daniel@iogearbox.net \
    --cc=andrii@kernel.org \
    --cc=aou@eecs.berkeley.edu \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®