* [PATCH bpf-next v2] libbpf: Optimized return value in libbpf_strerror when errno is libbpf errno Xin Liu
@ 2022-12-09 11:05 Xin Liu
2022-12-09 23:12 ` Daniel Borkmann
0 siblings, 1 reply; 3+ messages in thread
From: Xin Liu @ 2022-12-09 11:05 UTC (permalink / raw)
To: andrii, ast, daniel, martin.lau, song, yhs, john.fastabend,
kpsingh, sdf, haoluo, jolsa
Cc: bpf, linux-kernel, yanan, wuchangye, xiesongyang, kongweibin2,
liuxin350, zhangmingyi5
This is a small improvement in libbpf_strerror. When libbpf_strerror
is used to obtain the system error description, if the length of the
buf is insufficient, libbpf_sterror returns ERANGE and sets errno to
ERANGE.
However, this processing is not performed when the error code
customized by libbpf is obtained. Make some minor improvements here,
return -ERANGE and set errno to ERANGE when buf is not enough for
custom description.
Signed-off-by: Xin Liu <liuxin350@huawei.com>
---
v2:
Check the return value of snprintf to determine whether the buffer is
too small.
v1:
https://lore.kernel.org/bpf/20221209084047.229525-1-liuxin350@huawei.com/T/#t
tools/lib/bpf/libbpf_errno.c | 15 +++++++++++----
1 file changed, 11 insertions(+), 4 deletions(-)
diff --git a/tools/lib/bpf/libbpf_errno.c b/tools/lib/bpf/libbpf_errno.c
index 96f67a772a1b..6240c7cb7472 100644
--- a/tools/lib/bpf/libbpf_errno.c
+++ b/tools/lib/bpf/libbpf_errno.c
@@ -39,14 +39,13 @@ static const char *libbpf_strerror_table[NR_ERRNO] = {
int libbpf_strerror(int err, char *buf, size_t size)
{
+ int ret;
if (!buf || !size)
return libbpf_err(-EINVAL);
err = err > 0 ? err : -err;
if (err < __LIBBPF_ERRNO__START) {
- int ret;
-
ret = strerror_r(err, buf, size);
buf[size - 1] = '\0';
return libbpf_err_errno(ret);
@@ -56,12 +55,20 @@ int libbpf_strerror(int err, char *buf, size_t size)
const char *msg;
msg = libbpf_strerror_table[ERRNO_OFFSET(err)];
- snprintf(buf, size, "%s", msg);
+ ret = snprintf(buf, size, "%s", msg);
buf[size - 1] = '\0';
+ if (ret < 0)
+ return libbpf_err_errno(ret);
+ if (ret >= size)
+ return libbpf_err(-ERANGE);
return 0;
}
- snprintf(buf, size, "Unknown libbpf error %d", err);
+ ret = snprintf(buf, size, "Unknown libbpf error %d", err);
buf[size - 1] = '\0';
+ if (ret < 0)
+ return libbpf_err_errno(ret);
+ if (ret >= size)
+ return libbpf_err(-ERANGE);
return libbpf_err(-ENOENT);
}
--
2.33.0
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: [PATCH bpf-next v2] libbpf: Optimized return value in libbpf_strerror when errno is libbpf errno Xin Liu
2022-12-09 11:05 [PATCH bpf-next v2] libbpf: Optimized return value in libbpf_strerror when errno is libbpf errno Xin Liu Xin Liu
@ 2022-12-09 23:12 ` Daniel Borkmann
2022-12-10 7:33 ` [PATCH bpf-next v2] libbpf: Optimized return value in libbpf_strerror when errno is libbpf errno Xin Liu
0 siblings, 1 reply; 3+ messages in thread
From: Daniel Borkmann @ 2022-12-09 23:12 UTC (permalink / raw)
To: Xin Liu, andrii, ast, martin.lau, song, yhs, john.fastabend,
kpsingh, sdf, haoluo, jolsa
Cc: bpf, linux-kernel, yanan, wuchangye, xiesongyang, kongweibin2,
zhangmingyi5
On 12/9/22 12:05 PM, Xin Liu wrote:
> This is a small improvement in libbpf_strerror. When libbpf_strerror
> is used to obtain the system error description, if the length of the
> buf is insufficient, libbpf_sterror returns ERANGE and sets errno to
> ERANGE.
>
> However, this processing is not performed when the error code
> customized by libbpf is obtained. Make some minor improvements here,
> return -ERANGE and set errno to ERANGE when buf is not enough for
> custom description.
nit: $subject line got corrupted?
> Signed-off-by: Xin Liu <liuxin350@huawei.com>
> ---
>
> v2:
> Check the return value of snprintf to determine whether the buffer is
> too small.
>
> v1:
> https://lore.kernel.org/bpf/20221209084047.229525-1-liuxin350@huawei.com/T/#t
>
> tools/lib/bpf/libbpf_errno.c | 15 +++++++++++----
> 1 file changed, 11 insertions(+), 4 deletions(-)
>
> diff --git a/tools/lib/bpf/libbpf_errno.c b/tools/lib/bpf/libbpf_errno.c
> index 96f67a772a1b..6240c7cb7472 100644
> --- a/tools/lib/bpf/libbpf_errno.c
> +++ b/tools/lib/bpf/libbpf_errno.c
> @@ -39,14 +39,13 @@ static const char *libbpf_strerror_table[NR_ERRNO] = {
>
> int libbpf_strerror(int err, char *buf, size_t size)
> {
> + int ret;
nit: newline after declaration
> if (!buf || !size)
> return libbpf_err(-EINVAL);
>
> err = err > 0 ? err : -err;
>
> if (err < __LIBBPF_ERRNO__START) {
> - int ret;
> -
> ret = strerror_r(err, buf, size);
> buf[size - 1] = '\0';
> return libbpf_err_errno(ret);
> @@ -56,12 +55,20 @@ int libbpf_strerror(int err, char *buf, size_t size)
> const char *msg;
>
> msg = libbpf_strerror_table[ERRNO_OFFSET(err)];
> - snprintf(buf, size, "%s", msg);
> + ret = snprintf(buf, size, "%s", msg);
> buf[size - 1] = '\0';
> + if (ret < 0)
> + return libbpf_err_errno(ret);
This would pass in ret == -1 and then eventually return 1 which
is misleading, no?
We have buf and msg non-NULL and a positive size, afaik, the only
case where you could get a negative error now is when you pass in
a buf with size exceeding INT_MAX..
> + if (ret >= size)
> + return libbpf_err(-ERANGE);
> return 0;
> }
>
> - snprintf(buf, size, "Unknown libbpf error %d", err);
> + ret = snprintf(buf, size, "Unknown libbpf error %d", err);
> buf[size - 1] = '\0';
> + if (ret < 0)
> + return libbpf_err_errno(ret);
> + if (ret >= size)
> + return libbpf_err(-ERANGE);
> return libbpf_err(-ENOENT);
> }
>
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: [PATCH bpf-next v2] libbpf: Optimized return value in libbpf_strerror when errno is libbpf errno
2022-12-09 23:12 ` Daniel Borkmann
@ 2022-12-10 7:33 ` Xin Liu
0 siblings, 0 replies; 3+ messages in thread
From: Xin Liu @ 2022-12-10 7:33 UTC (permalink / raw)
To: daniel
Cc: andrii, ast, bpf, haoluo, john.fastabend, jolsa, kongweibin2,
kpsingh, linux-kernel, liuxin350, martin.lau, sdf, song,
wuchangye, xiesongyang, yanan, yhs, zhangmingyi5
On Sat, 10 Dec 2022 00:12:58 AM Daniel Borkmann <daniel@iogearbox.net> wrote:
> On 12/9/22 12:05 PM, Xin Liu wrote:
> > This is a small improvement in libbpf_strerror. When libbpf_strerror
> > is used to obtain the system error description, if the length of the
> > buf is insufficient, libbpf_sterror returns ERANGE and sets errno to
> > ERANGE.
> >
> > However, this processing is not performed when the error code
> > customized by libbpf is obtained. Make some minor improvements here,
> > return -ERANGE and set errno to ERANGE when buf is not enough for
> > custom description.
>
> nit: $subject line got corrupted?
>
> > Signed-off-by: Xin Liu <liuxin350@huawei.com>
> > ---
> >
> > v2:
> > Check the return value of snprintf to determine whether the buffer is
> > too small.
> >
> > v1:
> > https://lore.kernel.org/bpf/20221209084047.229525-1-liuxin350@huawei.com/T/#t
> >
> > tools/lib/bpf/libbpf_errno.c | 15 +++++++++++----
> > 1 file changed, 11 insertions(+), 4 deletions(-)
> >
> > diff --git a/tools/lib/bpf/libbpf_errno.c b/tools/lib/bpf/libbpf_errno.c
> > index 96f67a772a1b..6240c7cb7472 100644
> > --- a/tools/lib/bpf/libbpf_errno.c
> > +++ b/tools/lib/bpf/libbpf_errno.c
> > @@ -39,14 +39,13 @@ static const char *libbpf_strerror_table[NR_ERRNO] = {
> >
> > int libbpf_strerror(int err, char *buf, size_t size)
> > {
> > + int ret;
>
> nit: newline after declaration
>
> > if (!buf || !size)
> > return libbpf_err(-EINVAL);
> >
> > err = err > 0 ? err : -err;
> >
> > if (err < __LIBBPF_ERRNO__START) {
> > - int ret;
> > -
> > ret = strerror_r(err, buf, size);
> > buf[size - 1] = '\0';
> > return libbpf_err_errno(ret);
> > @@ -56,12 +55,20 @@ int libbpf_strerror(int err, char *buf, size_t size)
> > const char *msg;
> >
> > msg = libbpf_strerror_table[ERRNO_OFFSET(err)];
> > - snprintf(buf, size, "%s", msg);
> > + ret = snprintf(buf, size, "%s", msg);
> > buf[size - 1] = '\0';
> > + if (ret < 0)
> > + return libbpf_err_errno(ret);
>
> This would pass in ret == -1 and then eventually return 1 which
> is misleading, no?
>
> We have buf and msg non-NULL and a positive size, afaik, the only
> case where you could get a negative error now is when you pass in
> a buf with size exceeding INT_MAX..
>
> > + if (ret >= size)
> > + return libbpf_err(-ERANGE);
> > return 0;
> > }
> >
> > - snprintf(buf, size, "Unknown libbpf error %d", err);
> > + ret = snprintf(buf, size, "Unknown libbpf error %d", err);
> > buf[size - 1] = '\0';
> > + if (ret < 0)
> > + return libbpf_err_errno(ret);
> > + if (ret >= size)
> > + return libbpf_err(-ERANGE);
> > return libbpf_err(-ENOENT);
> > }
> >
The logic of returning negative numbers is really unlikely here,
I'll add some comments and delete this logic, thanks to Daniel
for pointing out.
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2022-12-10 7:34 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-12-09 11:05 [PATCH bpf-next v2] libbpf: Optimized return value in libbpf_strerror when errno is libbpf errno Xin Liu Xin Liu
2022-12-09 23:12 ` Daniel Borkmann
2022-12-10 7:33 ` [PATCH bpf-next v2] libbpf: Optimized return value in libbpf_strerror when errno is libbpf errno Xin Liu
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®