diff mbox series

[bpf-next,v2] libbpf: Optimized return value in libbpf_strerror when errno is libbpf errno Xin Liu

Message ID 20221209110502.231677-1-liuxin350@huawei.com (mailing list archive)
State Changes Requested
Delegated to: BPF
Headers show
Series [bpf-next,v2] libbpf: Optimized return value in libbpf_strerror when errno is libbpf errno Xin Liu | expand

Checks

Context Check Description
bpf/vmtest-bpf-next-PR success PR summary
bpf/vmtest-bpf-next-VM_Test-28 success Logs for test_progs_no_alu32_parallel on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-34 success Logs for test_verifier on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-35 success Logs for test_verifier on aarch64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-36 success Logs for test_verifier on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-37 success Logs for test_verifier on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-38 success Logs for test_verifier on x86_64 with llvm-16
netdev/tree_selection success Clearly marked for bpf-next
netdev/fixes_present success Fixes tag not required for -next series
netdev/subject_prefix success Link
netdev/cover_letter success Single patches do not need cover letters
netdev/patch_count success Link
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 0 this patch: 0
netdev/cc_maintainers success CCed 12 of 12 maintainers
netdev/build_clang success Errors and warnings before: 0 this patch: 0
netdev/module_param success Was 0 now: 0
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 0 this patch: 0
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 37 lines checked
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
bpf/vmtest-bpf-next-VM_Test-10 success Logs for test_maps on aarch64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-12 success Logs for test_maps on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-13 success Logs for test_maps on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-14 fail Logs for test_progs on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-15 success Logs for test_progs on aarch64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-17 success Logs for test_progs on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-18 success Logs for test_progs on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-19 success Logs for test_progs_no_alu32 on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-20 success Logs for test_progs_no_alu32 on aarch64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-22 success Logs for test_progs_no_alu32 on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-23 success Logs for test_progs_no_alu32 on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-24 success Logs for test_progs_no_alu32_parallel on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-25 success Logs for test_progs_no_alu32_parallel on aarch64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-26 success Logs for test_progs_no_alu32_parallel on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-27 success Logs for test_progs_no_alu32_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-29 success Logs for test_progs_parallel on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-30 success Logs for test_progs_parallel on aarch64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-32 success Logs for test_progs_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-33 success Logs for test_progs_parallel on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-21 success Logs for test_progs_no_alu32 on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-31 success Logs for test_progs_parallel on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-16 success Logs for test_progs on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-11 success Logs for test_maps on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-1 success Logs for ${{ matrix.test }} on ${{ matrix.arch }} with ${{ matrix.toolchain }}
bpf/vmtest-bpf-next-VM_Test-2 success Logs for ShellCheck
bpf/vmtest-bpf-next-VM_Test-3 success Logs for build for aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-4 success Logs for build for aarch64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-5 fail Logs for build for s390x with gcc
bpf/vmtest-bpf-next-VM_Test-6 success Logs for build for x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-7 success Logs for build for x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-8 success Logs for llvm-toolchain
bpf/vmtest-bpf-next-VM_Test-9 success Logs for set-matrix

Commit Message

Xin Liu Dec. 9, 2022, 11:05 a.m. UTC
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(-)

Comments

Daniel Borkmann Dec. 9, 2022, 11:12 p.m. UTC | #1
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);
>   }
>
Xin Liu Dec. 10, 2022, 7:33 a.m. UTC | #2
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.
diff mbox series

Patch

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);
 }